# Problems with referencing in Emacs/RefTeX

I have problems referencing a table (see Example below) using GNU Emacs 26.1 (build 1, x86_64-w64-mingw32) on Windows Pro X64 and AUCTeX with RefTeX. If I set a label, and reference it, the reference will not appear in the text (either ?? or nothing at all). This can be seen in the buffer *RefTeX Select*

          tbl:tabco2
.                 NO MATCH FOR CONTEXT REGEXP


Any idea what is going wrong (older labels in the file this appeared still work properly).

\documentclass{article}
\usepackage{tabularx}

\begin{document}

\begin{table}[!htbp]
\centering
\begin{tabular}{lr}
Energy commodity & t CO2/TJ \\
Light fuel oil & 73.70 \\
Heavy fuel oil & 77.00 \\
\end{tabular}
\label{tbl:tabco2}
\caption{CO$_2$ factors of energy commodities.}
\end{table}

This is table \ref{tbl:tabco2}

\end{document}


The \label should be placed after the \caption, for it's the latter which generates the number \label refers to (see Why does an environment's label have to appear after the caption?). As a matter of fact, \label will always refer to the previous generated reference number in the document. RefTeX also applies this logic, in searching for the context to display: It starts after \label and searches backward for the regular expressions defined in the variable reftex-default-context-regexps. In sum, just put your \label after the \caption, for it to have something to reference, that's what both RefTeX and latex expect.

\documentclass{article}
\usepackage{tabularx}

\begin{document}

\begin{table}[!htbp]
\centering
\begin{tabular}{lr}
Energy commodity & t CO2/TJ \\
Light fuel oil & 73.70 \\
Heavy fuel oil & 77.00 \\
\end{tabular}
\caption{CO$_2$ factors of energy commodities.}
\label{tbl:tabco2}
\end{table}

This is table \ref{tbl:tabco2}

\end{document}


• I edited your answer slightly as I didn't want to write a new one. I hope this is OK for you. – Arash Esbati Feb 15 at 13:57
• @ArashEsbati Thank you for your edit, it's a welcome addition, and it clears up how RefTeX uses such information. But I should add still something to complement, because the original code also produced a latex error, it is not only RefTeX which looses the reference. – gusbrs Feb 15 at 14:22