9

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}
0

3 Answers 3

11

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: example

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

6
  • Thanks! :^) That works nicely. It seems \label has to go immediately after \refstepcounter in order to work.?
    – Rachel
    Commented May 8, 2012 at 7:44
  • @Rachel, yes. :)
    – nickpapior
    Commented May 8, 2012 at 8:06
  • Anybody attempted to apply this solution within LyX? Commented Jun 25, 2012 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... :)
    – nickpapior
    Commented Jun 25, 2012 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. Commented Jun 25, 2012 at 21:41
2

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}
5
  • 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
    Commented May 8, 2012 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! :)
    – nickpapior
    Commented May 8, 2012 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
    Commented May 8, 2012 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.
    – nickpapior
    Commented May 8, 2012 at 10:57
  • @zeroth Your arguments convinced me. You are right!
    – Holle
    Commented May 8, 2012 at 14:54
0

I have following additional requirements

  • named labels (and not autogeneratd ones) for each row, because I sometimes exchange rows.
  • allow first column to be centered.
  • cleveref support.
  • similar appearance as the algorithmic environment.

I couldn't get cleveref to work, therefore, I'm using autoref.

Therefore, I came up with that solution:

\documentclass{article}

\usepackage{array}
\usepackage{hyperref}

\newcounter{rowcount}
\setcounter{rowcount}{0}
\newcommand{\rowcountautorefname}{Line}

\begin{document}

\begin{tabular}{@{{\makebox[1.3em][r]{\footnotesize\the\numexpr\value{rowcount}+1\relax:}}\hspace*{1.5\tabcolsep}}>{\refstepcounter{rowcount}}c}
line 1 \\
long line 2 \\
\label{line3} longer line 3
\end{tabular}

\autoref{line3} shows the longest line.

\end{document}

Based on https://tex.stackexchange.com/a/58139/9075 and https://tex.stackexchange.com/a/54892/9075.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .