3

I'm trying to build a latex environment that generates a table, and in that table there may be figures that need to be referenced. I absolutely need the number in the text to compile to the reference number. Ideally this would also have the hyperlink to the figure, but that's secondary. (If l have to define a separate counter that's fine).

%% simple.sty
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{hyperref}
\RequirePackage{graphicx}
\RequirePackage[table]{xcolor}

\newenvironment{statementtable}{
  \newcommand{\statement}[2]{
    \multicolumn{2}{|c|}{\cellcolor{white} ##1} \\ \hline
    \multicolumn{2}{|c|}{\cellcolor{gray!25} \parbox{6in}{##2}} \\ \hline
  }
  \newcommand{\statementfigure}[3]{
    \stepcounter{figure}
    \phantomsection ##3 \label{##1} \\
    Figure \arabic{figure}: ##2
  }  
  \begin{center}
    \begin{tabular}{|l|l|} \hline
}{
    \end{tabular}
  \end{center}

}

The tex file is as such

%% simple.tex
\documentclass{article}
\usepackage{simple}

\begin{document}
\begin{statementtable}
  \statement{statement}{lorem ipsem see \ref{fig:foo} and \ref{fig:bar}}
  \statement{figures}{\statementfigure{fig:foo}{foo}{\includegraphics{potato.png}} \\
    \statementfigure{fig:bar}{bar}{\includegraphics{potato.png}}}
\end{statementtable}

\end{document}

And the output from xelatex simple.tex is missing the actual numbers. How do I get \ref to include the value of a counter at a label?

current output

5

At first glance, changing \stepcounter{figure} to \refstepcounter{figure} seems like it should suffice to fix the problem. For sure, the outputs of the \ref directives are no longer empty once you make that change.

However, some additional sleuthing reveals that hyperref is under the impression that what's being referenced are entities of type section rather than of type figure. Rather than keep re-inventing the wheel, so to say, I suggest you start using some of the machinery of the well-debugged caption package -- specifically, its \captionof and \captionsetup macros.

Incidentally, I wouldn't use a fixed width, such as 6in, for the \parbox. Instead, let LaTeX calculate the width such that no overflow into the right-hand margin occurs.

enter image description here

\documentclass{article}

\usepackage[demo]{graphicx} % remove 'demo' option in real document
\usepackage{caption} % for "\captionof" and "\captionsetup" macros
\usepackage{array}   % for "\extrarowheight" length parameter

%%% --- contents of simple.sty --- %%%
\RequirePackage{graphicx}
\RequirePackage[table]{xcolor}
\RequirePackage{hyperref}
\hypersetup{colorlinks,allcolors=blue} % optional
\usepackage[nameinlink,capitalize,noabbrev]{cleveref} % optional

\newenvironment{statementtable}{%
  \begingroup 
  \setlength\extrarowheight{2pt} % for a slightly more open "look"
  \captionsetup{singlelinecheck=false,
                justification=raggedright,
                skip=0pt}
  \newcommand{\statement}[2]{%
    \multicolumn{2}{|c|}{\cellcolor{white} ##1} \\ 
    \hline
    \multicolumn{2}{|c|}{\cellcolor{gray!25}%
      \parbox{\dimexpr\textwidth-2\tabcolsep\relax}{##2}} \\ 
    \hline
  }
  \newcommand{\statementfigure}[3]{%
    \phantomsection ##3 
    \captionof{figure}{##2} \label{##1}
  }  
  \centering
  \begin{tabular}{|l|l|} 
  \hline
  }{%
  \end{tabular}
  \endgroup
}
%%% --- end contents of simple.sty --- %%%

\begin{document}
\begin{statementtable}
  \statement{statement}{See \cref{fig:foo,fig:bar}.}
  \statement{figures}{%
    \statementfigure{fig:foo}{foo}{\includegraphics{potato.png}}
    \statementfigure{fig:bar}{bar}{\includegraphics{potato.png}}}
\end{statementtable}

\end{document}
  • 3
    I really like that you told the OP of the first answer first how they could improve their answer, and gave them time to do that, before adding your ("competing") answer! – Schrödinger's cat Dec 11 '19 at 22:21
  • @Schrödinger'scat - Thanks! – Mico Dec 11 '19 at 22:23
  • That seems a lot less kludgy. One note, the \phantomsection l'd had previously is no longer necessary. (there's an edit in the pipe to fix that). – Elliot Dec 12 '19 at 14:28
4

You need to use \refstepcounter instead of \stepcounter.

  • 1
    Changing \stepcounter to \refstepcounter is a step in the right direction, but it's not the full solution. E.g., if you replace \ref with \autoref, the chosen label is "section", not "figure". – Mico Dec 11 '19 at 21:18

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.