2

I am using a macro to define certain labels inside a tabular environment, however when I refer to these labels later the displayed text is the section number instead of the desired label.

What's the easiest way to achieve the desired result? (See code below for the precise situation.)

One solution which I don't like: passing the label name into the "FOOFOO" macro. I don't necessarily want to give labels to all my FOOFOOs

\documentclass{article}

\newcounter{foo}
\renewcommand{\thefoo}{F\arabic{foo}}
\newcommand{\FF}{
  \refstepcounter{foo}\textbf{\thefoo}
}

\newenvironment{footab}{\begin{tabular}{c|c|c}\hline}{\end{tabular}}

\newcommand{\FOOFOO}[2]{%
\FF & \textbf{#1} & #2 \\\hline%
}

\begin{document}

\section{My section}
\label{sec:my-section}

\begin{footab}
\FOOFOO{One}{First example}\label{foo:first}
\FOOFOO{Two}{Second example \label{foo:second}}
\end{footab}

Now I write some stuff referring to \ref{foo:first} (should be F1) and
\ref{foo:second} (should be F2).

\end{document}

The output looks like this:

enter image description here

4

When the cell containing \refstepcounter ends, the value of \@currentlabel is lost and the previous one is restored.

You can set the label as optional argument to \FOOFOO, instead.

\documentclass{article}

\newcounter{foo}
\renewcommand{\thefoo}{F\arabic{foo}}
\newcommand{\FF}{%
  \refstepcounter{foo}\textbf{\thefoo}%
}

\newenvironment{footab}{\begin{tabular}{c|c|c}\hline}{\end{tabular}}

\newcommand{\FOOFOO}[3][]{%
  \FF#1 & \textbf{#2} & #3 \\\hline
}

\begin{document}

\section{My section}
\label{sec:my-section}

\begin{footab}
\FOOFOO[\label{foo:first}]{One}{First example}
\FOOFOO[\label{foo:second}]{Two}{Second example}
\FOOFOO{Three}{Third example}
\end{footab}

Now I write some stuff referring to \ref{foo:first} (should be F1) and
\ref{foo:second} (should be F2).

\end{document}

enter image description here

Be more careful with % at end of lines; you had one that wasn't necessary and missed two, instead.

  • Thanks for this answer. Could you elaborate on what you mean about the "% at the end of lines"? (Or point me to somewhere which gives details?) – Ross Duncan Jun 16 '17 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.