1

Using pdfLaTeX. I have created a new command

\newcommand{\singlefigure}[3]{
  \begin{figure}
  \label{fig:#2}
  \centering
  \caption{#3}
  \includegraphics[trim={0cm 0cm 0cm 0cm},clip,height=0.2\textheight]{./fig/#1}
  \end{figure}
}

having it in mind to speed up the coding of figures. Actually it suits me pretty well, at first since

\singlefigure{directory/file.jpg}{mylabel}{My caption} 

gets me immediately where I want.


Likewise, I have created another shorthand to reference a figure

\newcommand{\fig}[1]{Figure~\ref{fig:#1}}  

which works well except when I am referencing a figure created with \singlefigure above, say \fig{mylabel}.


The result is

enter image description here

Apparently I created a situation in which the label of the first command is not visible to the second one. Is there a remedy, correction or workaround for this glitch?


Similar posts:

  • The first time that you compile a document with a new \ref command you will get ??. When you compile the document a second time the reference appears. If this is not your problem then rather than posting code fragments please post a minimal working example, which compiles, so that people can replicate your problem. Btw, rather then defining\fig you might want to use \autoref from hyperref or \cref from cleveref. –  Sep 07 '20 at 05:58
  • 1
    The \label must always come after the \caption. – Phelype Oleinik Sep 07 '20 at 10:17
  • @PhelypeOleinik Thanks for the tip. This is indeed the answer, and I am glad that this post is now linked to that. – XavierStuvw Sep 07 '20 at 12:35

0 Answers0