# How to use \ref to print the value of a counter the way it was originally produced?

I want to number the lines in a table with roman numerals, and then when I cross-reference them have the reference printed in the same way.

For example,

\documentclass{article}
\begin{document}
\newcounter{foo}
\newcommand{\rfoo}{\refstepcounter{foo}(\roman{foo})}

\begin{tabular}{r|l}

\hline

\rfoo\label{f1} & First line \\ \hline

\rfoo\label{f2} & Second line \\ \hline

\hline
\end{tabular}

\

The first line is \ref{f1}.  The second line is \ref{f2}.

\end{document}


gives

But what I want is:

• See my possible answer. I will update with an automatic row numbering code later on – user31729 Jul 29 '15 at 19:30
• Small tip: \newcounter should be done before \begin{document} – user31729 Jul 29 '15 at 19:36

Each counter has a fellow macro called \the..., say counter foo will have \thefoo. This \the... macro does by default the output of the counter value with arabic numbers.

\thefoo is used also for the label as it is written to the .aux file. If format (i), is requested, this has to be written into the .aux file and it's to be used in \thefoo.

So

\renewcommand{\thefoo}{(\roman{foo})}


is the name of the game ;-)

\documentclass{article}

\newcounter{foo}
\renewcommand{\thefoo}{(\roman{foo})}
\newcommand{\rfoo}{\refstepcounter{foo}\thefoo}

\begin{document}
\begin{tabular}{r|l}

\hline

\rfoo\label{f1} & First line \\ \hline

\rfoo\label{f2} & Second line \\ \hline

\hline
\end{tabular}

The first line is \ref{f1}.  The second line is \ref{f2}.

\end{document}


Here the automatic row numbering version

\documentclass{article}

\usepackage{array}

\newcounter{foo}
\renewcommand{\thefoo}{(\roman{foo})}

% Define
\newcolumntype{R}{>{\refstepcounter{foo}\thefoo\arraybackslash}r}

\begin{document}

\begin{tabular}{R|l}

\hline

\label{f1} & First line \\ \hline

\label{f2} & Second line \\ \hline

& ... line \\ \hline

\label{f4}  & ... line \\ \hline

\hline
\end{tabular}

\

The first line is \ref{f1}.  The second line is \ref{f2}.

And in line \ref{f4} you can see that

\end{document}


• No comments please on the design of the table ;-) – user31729 Jul 29 '15 at 19:40
• Thanks Christian! But how do I get \ref{f1} to print the relevant value of the new counter "foo" that I've introduced. I've used a second new counter that's interfering. – Ruby Jul 29 '15 at 19:51
• @DeliaRuby: What do you mean by relevant value of foo? The real arabic number? – user31729 Jul 29 '15 at 19:53
• Sorry I wasn't clear. This is related to the "more than one footnote in a table" question that I asked yesterday. Someone gave me a great way to get \footnotemark and \footnotetext to work right when I wanted more than one footnote in a table. It involved introducing another new counter. So now, when I want to use "\ref{f1}" to get "(i)" it doesn't use the format used in "thefoo", but rather that for the other counter ("dummycounter"). Should I put up the mwe? – Ruby Jul 29 '15 at 20:11
• @DeliaRuby: I was the someone :-P And you accepted and (unaccepted :-() my answer. Wait, I will look about it ... – user31729 Jul 29 '15 at 20:13

\label write \theX of the last counter X, so update this accordingly:

\documentclass{article}

\newcounter{foo}
\renewcommand{\thefoo}{(\roman{foo})}
\newcommand{\rfoo}{\refstepcounter{foo}\thefoo}

\begin{document}

\begin{tabular}{r|l}
\hline
\rfoo\label{f1} & First line \\
\rfoo\label{f2} & Second line \\
\hline
\end{tabular}

The first line is~\ref{f1}. The second line is~\ref{f2}.

\end{document}


As reference, see Understanding how references and labels work.