3

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

enter image description here

But what I want is:

enter image description here

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

2 Answers2

3

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}

enter image description here

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}

enter image description here

  • No comments please on the design of the table ;-) –  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? –  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 ... –  Jul 29 '15 at 20:13
  • @DeliaRuby: I don't get what you want to achieve now. The dummycounter in that answer isn't really shown at all, so there is no need to output it –  Jul 29 '15 at 20:19
  • Oh, so you were! :) This is all in the same table. The problem is that when I try to reference the line number (i) using \ref{f1}, what I get is "3", where that was the number of times that "dummycounter" had been incremented. – Ruby Jul 29 '15 at 20:30
  • @DeliaRuby: Ah, now I get you ;-) You must use the label for the linenumber, say \label{f1} in the first column always, since \label does always look after the last counter being increased with \refstepcounter. If you use \label in the \footnotemark column, it will use the wrong counter then! –  Jul 29 '15 at 20:33
2

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

enter image description here

\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.

Werner
  • 603,163