4

I want to remove a figure and its caption from a LaTeX file, but I want to keep the references to it in the text.

So far I kept the empty figure environment with a caption and a label. The problem is, I can not make the caption vanish without destroying the references.

I tried \caption*{} as well as

\setbox0=\vbox{\caption{somecaption}}

as was suggested here.

However, as soon as the caption "Fig. 1" disappears, also my reference to the label disappears.

\documentclass[superscriptaddress,reprint,amsmath,amssymb,aps,floatfix]{revtex4-1}
\usepackage{graphicx}
\begin{document}

I want to reference Fig.~\ref{fig1}.  \\

\begin{figure}
    %\includegraphics{image.pdf}
    \caption{}
    \label{fig1}
\end{figure}

\end{document}

It would be nice if someone could help me, as I didn't find anything about this myself.

  • Just to be clear: you don't want the picture nor its caption but do want be able to use reference to it? – NBur Sep 28 '18 at 7:40
  • The labelling in this case is a bit hard to do right reliably. The problem is that the label will only get written to the aux if the box it is used in is typeset. This is done so that the reference will lead to the correct page if it floats. – Skillmon Sep 28 '18 at 7:46
  • Also: Do you want the caption be listed in the list of figures? – Skillmon Sep 28 '18 at 7:46
  • @Nbur exactly :-) – Mechanix Sep 28 '18 at 7:50
  • @Skillmon the list of figures doesn't matter to me – Mechanix Sep 28 '18 at 7:58
8
\documentclass[superscriptaddress,reprint,amsmath,amssymb,aps,floatfix]{revtex4-1}
\usepackage{graphicx}
\begin{document}

I want to reference Fig.~\ref{fig1}. % never use  \\ at end of paragraph


\refstepcounter{figure}\label{fig1}


\end{document}
  • Awesome! That does the trick! Thanks a lot! – Mechanix Sep 28 '18 at 8:00
4

Possible solutions I can think of currently:

You could move the caption out of the page and \smash it so it does have no height. The space occupied by the figure environment would still be taken but you'd still use the floating mechanism of LaTeX so that your reference would point to the correct page and you won't have to deal with correct numbering. This could be done with:

\newcommand\captionaway[1]
  {%
    \setbox0\hbox{\vbox{#1}}%
    \null
    \hspace*{2\paperwidth}%
    \smash{\usebox0}%
  }

Then you'd place your \caption and the \label command in the argument of \captionaway. The caption would still show up in the list of figures.

Another possible solution is to only \refstepcounter the figure counter and place a label. E.g. with:

\makeatletter
\newcommand\onlylabel[2][\@captype]
  {\refstepcounter{#1}\label{#2}}
\makeatother

This would give you a macro \onlylabel which, when used inside of a float (so figure or table) would place a label without a \caption. If you use it outside of a float you could use it like \onlylabel[figure]{fig:label}. If you use it outside of a float you'd have to watch out that you place it at the correct location if you have other floats of the same kind. Best would be to place it at the end of the previous float, to get numbering right (because if it were a float it would have been on the float stack for at least as long as the previous float isn't shipped).

Complete MWE using both methods:

\documentclass[superscriptaddress,reprint,amsmath,amssymb,aps,floatfix]
  {revtex4-1}
\usepackage{graphicx}

\newcommand\captionaway[1]
  {%
    \setbox0\hbox{\vbox{#1}}%
    \null
    \hspace*{2\paperwidth}%
    \smash{\usebox0}%
  }
\makeatletter
\newcommand\onlylabel[2][\@captype]
  {\refstepcounter{#1}\label{#2}}
\makeatother

\begin{document}

I want to reference Fig.~\ref{fig1}.  \\

\begin{figure}
    %\includegraphics{image.pdf}
  \captionaway
    {%
      \caption{This caption}%
      \label{fig1}%
    }%
  \onlylabel{fig:hidden}
\end{figure}

I also reference Fig.~\ref{fig:hidden}. Note that Fig.~\ref{fig1} does use some
space as the \texttt{figure} environment was actually used and needs some space.

\end{document}
3

I made a command so that just adding a star you can switch.

\documentclass[superscriptaddress,reprint,amsmath,amssymb,aps,floatfix]{revtex4-1}
\usepackage{graphicx}
\usepackage{cleveref}
\usepackage{xparse}
\NewDocumentCommand{\myFig}{smmm}{%
    \IfBooleanTF{#1}{%
        \refstepcounter{figure}\label{#4}
    }{\begin{figure}
            #2
            \caption{#3}
            \label{#4}
        \end{figure}}
    }

\begin{document}

    I want to reference Fig.~\ref{label1}, \cref{label2}.

    \myFig{\includegraphics[width=\linewidth]{example-image-a.pdf}}{Caption1}{label1}
    \myFig*{\includegraphics{example-image-a.pdf}}{Caption2}{label2}
\end{document}
  • Problems would occur if there was another float of the same kind prior to it that is moved to the next page, then you'd get incorrect numbers with this approach (the figure being placed later in the sources gets the smaller number). Also you should test for an empty fourth argument if one doesn't want a \label, imho. – Skillmon Sep 28 '18 at 8:01
  • @Skillmon good point, haven't thought of it… – NBur Sep 28 '18 at 8:06
  • thanks a lot! Now i have more solutions than problems :-) – Mechanix Sep 28 '18 at 8:31

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.