Reference table rows by automatic counter
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.
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}