Monday, January 24, 2011

The awesome weirdness of latex... On \begin{table} cross references

In Latex2e, always remember to put \label in the \caption command or just after it. Else latex might get the cross-references wrong!

Example: In a recent paper, I created this table: 

 \begin{table}[t]  
   {\small  
     \caption[]{my caption}  
     \begin{tabular}{|c|cl|}  
       ...  
     \end{tabular}  
   }  
 \vspace{-4mm}  
 \label{tab:agg}  
 \end{table}  

\end{table}

And, I referenced it in the document as \ref{tab:agg}. Even though it was the first table, the compiled version read "Table 4"!! After cracking my head against this by resetting counters and creating new counters for the table, I found that the real problem was the placement of the \label. Once placed like this below, the cross-references work just fine:


 \begin{table}         
   \caption{my caption}      
    \label{tab:label}            
 \end{table}         
  or  
 \begin{table}  
   \caption{my caption%  
    \label{tab:label}}  
 \end{table}  




No comments:

Post a Comment