# How to use something like \label outside the table environment?

I would like to ask whether there is any way to use a \label-like command outside a longtable in order to reference this table in latex? It would be also helpful if the label of the table could be provided as \label{table:var}, where var is a variable storing the label itself.

E.g., my table is as follows:

\documentclass{article}

\usepackage{longtable}

\begin{document}

\begin{longtable}{c|c|c|c}
\caption{\capName}
A & B & A & B \\ \hline
\end{longtable}

\end{document}


where the name of the caption is given by a new command \capName.

I would like to reference this table in the text. The table is re-generated several times; therefore, I would not place anything after \begin{longtable} and before \end{longtable}.

• Put the \label outside tabular and inside table? – user156344 Mar 5 at 9:44
• My question has been edited. – Roli Mar 5 at 9:55
• the request for an example was asking to make a complete document, as Ulrike did in her answer. – David Carlisle Mar 5 at 11:04

You can create custom labels by defining locally \@currentlabel based e.g. on \thetable:

\documentclass{article}

\usepackage{longtable}

\begin{document}

\begin{longtable}{c|c|c|c}
A & B & A & B \\ \hline
\end{longtable}{\makeatletter\def\@currentlabel{\thetable}\label{tab:A}}

\begin{longtable}{c|c|c|c}
A & B & A & B \\ \hline
\end{longtable}{\makeatletter\def\@currentlabel{\thetable}\label{tab:B}}

Table \ref{tab:A}, Table \ref{tab:B}

\end{document}

• Thank you! If I understand correcly, this works only in the case that there are no lines between \end{longtable} and {\makeatletter...}. Is this true? – Roli Mar 5 at 10:08
• No. As long a \thetable gives the right result the placement doesn't matter much for \ref. You could even place the code before the longtable, but than you would need to decrease the counter first. But be aware that as the label is outside the longtable, links from hyperref and \pageref is not reliable. – Ulrike Fischer Mar 5 at 10:14
• Oh, it is very nice and useful for me! Thank you again! – Roli Mar 5 at 10:18
• @Roli... If the answer is good enough for you... You could accept it (the green button next to it) and may be upvote it (if seems usefull to you) – koleygr Mar 5 at 10:19
• Don't ask new questions in comments, open a new question - and add this time a better minimal example. – Ulrike Fischer Mar 5 at 10:27