Introduction

Once I was writing papers on Overleaf, I met an error about the numbering of tables in Latex. When referring several different tables, I've gotton the same numbering. After trying some alternatives of sequencing of the elements inside a table, I've found out that the order of caption and label can't be exchanged.

Table in Latex

Tables are common elements in academic reports and scientific research papers. Latex provides a wide range of customization of tables, such changing sizes, combining cells, modifying the colors etc. The following code snapshot illustrates how to construct a basic table in Latex.
\begin{table}[h!]
\centering
\caption{Table 1.}
\label{tab:1}
\begin{tabular}{|c|c|c|}
\hline
 cell1 & cell2 & cell3 \\
\hline
 cell4 & cell5 & cell6 \\  
\hline
 cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{table}
This is a simple working example of a table from Tables. \(h\) means the table will be placed here approximately. \(!\) indicates overriding the internal environment settings. The above is the correct way of using label afer caption and thus the table can be referred in the text.
\begin{table}[h!]
\centering
\label{tab:2}
\caption{Table 2.}
\begin{tabular}{|c|c|c|}
\hline
 cell1 & cell2 & cell3 \\ 
\hline
 cell4 & cell5 & cell6 \\  
\hline
 cell7 & cell8 & cell9 \\
\hline
\end{tabular}
\end{table}
And for this example, label comes before caption. In the following Latex code, you can see three items inside the list have referred three table respectively.
\begin{enumerate}
    \item Table \ref{tab:1} should be Table 1.
    \item Table \ref{tab:2} should be Table 2.
    \item Table \ref{tab:3} should be Table 3.
\end{enumerate}

After compilation, you can see that the reference of the second table is missing while the first and third table is referred nicely. This project can be accessed here.

Table Test Project in Overleaf

Conclusion

Sometimes the bug in Latex is not easy to find out, partly because the rules of Latex compiler is not very clear to public and partly because the documentation of Latex contains too many details. As described in this article, the order of caption and label can cause the failure of the reference of tables and this doesn't show any compile error or other strange messages. Editting articles with Latex is just like coding other programming languages to me and such language characteristic can only be found in practices.

History

Reference

  1. Tables
  2. Lists
  3. Table Test