# Reference table rows by automatic counter

Problem

I have defined an environment to automatically number example sentences throughout my document. It uses the tabular environment and automatically numbers the first column of each row using a custom counter. I have reasons for not using lists or the theorem counter.

I want to label and reference a table row so that the number associated with the row is displayed. Using \label and \ref does not work. Is there some way to feed a function two args, a key and the current value of my counter, and later use the key to recall the other arg? Or is there another way to do what I want? Not breaking the labels and references for the rest of the document is ideal but not necessary.

(Non-)Working Example

\documentclass{article}
\usepackage{array} %don't know why this is needed; get csname error otherwise

% automatically number examples throughout document
\newcounter{exno}
\newenvironment{examples}
{
\begin{flushleft}
\begin{tabular}{>{(\stepcounter{exno}\theexno)}rl}
}
{
\end{tabular}
\end{flushleft}
}

\begin{document}

\section{Foo}
Here are some numbered example sentences.
\begin{examples}
&Content of row 1 \label{row1} \\
&Content of row 2 \label{row2}
\end{examples}
Now I want to get a 1 here: \ref{row1}. I want to get a 2 here: \ref{row2}.

\end{document}

-

In order for you to reference the counter like that you need to use \refstepcounter. This steps the counter, as well as telling LaTeX to use the supplied counter for the following reference.

Then you need to replace the \label's. They need to address directly to the counter. Therefore your example would be:

\documentclass{article}
\usepackage{array} %don't know why this is needed; get csname error otherwise

% automatically number examples throughout document
\newcounter{exno}
\newenvironment{examples}
{
\begin{flushleft}
\begin{tabular}{>{(\refstepcounter{exno}\theexno)}rl}
}
{
\end{tabular}
\end{flushleft}
}

\begin{document}

\section{Foo}
Here are some numbered example sentences.
\begin{examples}
\label{row1} &Content of row 1 \\
\label{row2} &Content of row 2
\end{examples}
Now I want to get a 1 here: \ref{row1}. I want to get a 2 here: \ref{row2}.

\end{document}


This will produce:

Added: This also works by linking to the row by loading hyperref package.

-
Thanks! :^) That works nicely. It seems \label has to go immediately after \refstepcounter in order to work.? –  Rachel May 8 '12 at 7:44
@Rachel, yes. :) –  zeroth May 8 '12 at 8:06
Anybody attempted to apply this solution within LyX? –  Nikos Alexandris Jun 25 '12 at 11:20
@NikosAlexandris in LyX you are still capable of adding custom code... So this should be no problem. I can't see why it should not work... :) –  zeroth Jun 25 '12 at 16:45
@zeroth I was not clear, apologies. The problem is not putting the above code-example within an ERT. I rather try to get things done without defining a long table structure + contents by hand. For example, putting a LyX-table inside an "examples"-like environment. –  Nikos Alexandris Jun 25 '12 at 21:41

This is very similar to zeroth's solution, but if one put the label definition in the \newenvironment it is more comfortable. So you don't need to create a label for each row manually.

\documentclass{article}
\usepackage{array} %don't know why this is needed; get csname error otherwise

% automatically number examples throughout document
\newcounter{exno}
\newenvironment{examples}
{
\begin{flushleft}
\begin{tabular}{>{(\refstepcounter{exno}\theexno\label{row:\theexno}) }rl}
}
{
\end{tabular}
\end{flushleft}
}

\begin{document}

\section{Foo}
Here are some numbered example sentences.
\begin{examples}
&Content of row 1 \\
&Content of row 2
\end{examples}
Now I want to get a 1 here: \ref{row:1}. I want to get a 2 here: \ref{row:2}.

\end{document}


Edit: After zeroth's comment I edit my post to show how it works with more than one example. Just use an other counter for the example-environments. Or give each environment an unique name (as argument) and use it instead of the example-counter. Than it is possible to reference to the example and to the table rows. I think a lot of LaTeX environments works this way.

\documentclass{article}
\usepackage{array} %don't know why this is needed; get csname error otherwise

% automatically number examples throughout document

%link the RowNumberCounter to the ExampleNumberCounter
\newcounter{ExampleNumber}
\newcounter{RowNumber}[ExampleNumber]

\newenvironment{examples}
{
\stepcounter{ExampleNumber}
%
\begin{flushleft}%
\begin{tabular}{>{%
(\refstepcounter{RowNumber}\theRowNumber%
\label{Example:\theExampleNumber:Row:\theRowNumber}) }rl}%
}
{%
\end{tabular}%
\end{flushleft}%
}%

\begin{document}

\section{Foo}
Here are some numbered example sentences.
\begin{examples}
&Content of row 1 \\
&Content of row 2
\end{examples}
Now I want to get a 1 here: \ref{Example:1:Row:1}. I want to get a 2 here: \ref{Example:1:Row:2}.

\begin{examples}
&Content of row 1 \\
&Content of row 2\\
&Content of row 3 \\
&Content of row 4
\end{examples}
Now I want to get a 1 here: \ref{Example:2:Row:1}. I want to get a 2 here: \ref{Example:2:Row:2}.
Now I want to get a 3 here: \ref{Example:2:Row:3}. I want to get a 4 here: \ref{Example:2:Row:4}.
\end{document}

-
Yes, I have considered this alteration. :^) It may be useful sometime, but it means that the key changes with the numbering, so you have to change your arguments to \ref as well. –  Rachel May 8 '12 at 7:54
This is dangerous if having several example environments. Then you have the same labels for different tables. Not recommended! Of course you could do with an extra argument of the environment which creates the label prefix, however, be aware! :) –  zeroth May 8 '12 at 8:10
@zeroth : I can't see any problems. I edit my post to show how it works with more than one example. –  Holle May 8 '12 at 10:51
@Holle I still think that your approach of having a counter in the label like that is bad practice. What if you change chapters so that tables are interchanged? Instead do \newenvironment{...}[1]{...\label{#1:\theRowNumber}}{...}. Furthermore, what if you added a row in the middle. Now you have possible wrong refs in the text. It is a matter of taste, I just would never do it like that. –  zeroth May 8 '12 at 10:57
@zeroth Your arguments convinced me. You are right! –  Holle May 8 '12 at 14:54