# How can I hide a figure but keep its numbering?

I have a document with many figures; I want to print a short version of it with some figures hidden, but I would like that the global numbering of figures remain the same, and that labels still works. Here is a MWE:

\documentclass{article}

\usepackage{graphicx}

\begin{document}

Figure \ref{figure1} shows \ldots

\begin{figure}[ht]
\centering
\includegraphics[width=5cm]{figure1.jpg}
\caption{\label{figure1}}
\end{figure}

Figure \ref{figure2} shows \ldots

\begin{figure}[ht]
\centering
\includegraphics[width=5cm]{figure2.jpg}
\caption{\label{figure2}}
\end{figure}

\end{document}


Then I would like to hide figure 1 from PDF output, but label figure1 should still work, and the second picture should still be numbered as 2. Is it possible?

I tried with the comment environment but it does not work.

• if i correctly understood your question, than adding % before includegraphics is what you looking for. for example \begin{figure}[ht] \centering % \includegraphics[width=5cm]{figure1.jpg} \caption{\label{figure1}} \end{figure} Aug 30 '18 at 16:01
• Yes, but my goal is to have one version of my document, and by switching one option in the preamble, it would turn on/off some less important figures.
– JPG
Aug 30 '18 at 16:04
• @JPG: Do you always have \label inside \caption? Aug 30 '18 at 16:18
• @Werner: Yes, always. And sometimes I have subfigures with labels for each one.
– JPG
Aug 30 '18 at 16:20
• What should happen to the \caption? Should it still go to the LoF? Aug 30 '18 at 21:45

It would be best to put these possibly-invisible figures inside their own environment. That way you can control how they are managed. Below I suggest something like conditionalfigure together with \hidefigurestrue to hide them, or \hidefiguresfalse (the default) to keep them in the document.

\documentclass{article}

\usepackage{graphicx,environ}

\newsavebox{\figsavebox}% Box to capture figure content

\newif\ifhidefigures % Conditional to hide figures or keep them in the document

\NewEnviron{conditionalfigure}[1][ht]{%
\ifhidefigures
% Hide this figure
\let\oldlabel\label
\renewcommand{\label}[1]{\gdef\labelname{##1}}% Store label name
\renewcommand{\caption}[1]{##1}% Make \caption just print its argument
\begin{lrbox}{\figsavebox}
\BODY % Capture enture figure body
\end{lrbox}
\refstepcounter{figure}\oldlabel{\labelname}% Step counter with reference and mark with label
\else
\begin{figure}[#1]
\BODY
\end{figure}
\fi
}

\begin{document}

\hidefigurestrue % Remove conditional figures from document
%\hidefiguresfalse % Keep conditional figures in document (default)

Figure \ref{figure1} shows \ldots

\begin{conditionalfigure}[ht]
\centering
\includegraphics[width=5cm]{example-image-a}
\caption{\label{figure1}}
\end{conditionalfigure}

Figure \ref{figure2} shows \ldots

\begin{figure}[ht]
\centering
\includegraphics[width=5cm]{example-image-b}
\caption{\label{figure2}}
\end{figure}

\end{document}


Capturing the entire conditionalfigure contents ensures that it doesn't impede with the placement of floats within the document, otherwise the "invisible" float may still take up space above/below it.

• It works well and is easy to implement. Thanks.
– JPG
Aug 30 '18 at 17:12
• Why \gdef the \labelname? Wouldn't a \def be enough in case of \NewEnviron? Aug 30 '18 at 20:21
• @Skillmon: Within the lrbox environment where \label is executed the first time, a \def would render \labelname undefined when using \oldlabel{\labelname}. \gdef makes it available for use outside of the lrbox group. Aug 30 '18 at 20:23
• @Werner forgot about the lrbox. Aug 30 '18 at 20:23

Is this what you are looking for? Using resizebox and minipage you can make the includegraphics and caption have zero height.

\documentclass{article}
\usepackage{graphicx}

\begin{document}

Figure \ref{figure1} shows \ldots

\begin{figure}[ht]
\centering
\resizebox{!}{0cm}{\begin{minipage}{\textwidth}
\includegraphics[width=5cm]{example-image-a}
\caption{}
\label{figure1}
\end{minipage}}
\end{figure}

Figure \ref{figure2} shows \ldots

\begin{figure}[ht]
\centering
\includegraphics[width=5cm]{example-image-b}
\caption{}
\label{figure2}
\end{figure}

\end{document}

• Why \resizebox? You could as well just put the stuff in \box0 and never output it. Aug 30 '18 at 16:00
• Yes, your output is what I want, but it doesn't work on my computer, figure 1 is still visible with bad alignment.
– JPG
Aug 30 '18 at 16:00
• @Skillmon I've not heard of \box0 before. How would you use it in this context?
– Milo
Aug 30 '18 at 16:03
• I managed to obtain your output by changing \resizebox{!}{0cm} to \resizebox{!}{0.000001cm}; strange that it doesn't work with 0cm...
– JPG
Aug 30 '18 at 16:19
• Something like \setbox0\hbox{...}. The 0 box register is often used for temporary stuff (e.g. by \smash and the like). Stuff which is typed in a \setbox is fully evaluated and stored in the specified box register, but not output (you can then output it by \unhbox in case of a \hbox, or \unvbox for \vbox; there are other methods of outputting them, the simplest being \box, all of the aforementioned require you to specify a box register, e.g. 0 or the name of a \newbox or \newsavebox, the latter being the LaTeX macro, the former TeX). Aug 30 '18 at 20:16

If the caption should still produce an entry in the LoF, one could use the following (with kind help of @DavidCarlisle on the \immediate\write part). The only problem is that if two floats happen to be on the same page and one of them is hidden, the order of the entries in the LoF might get swapped.

\documentclass[]{article}

\usepackage[]{graphicx}
\newif\ifhidefigures

\makeatletter
\newenvironment{conditionalfigure}[1][]
{%
\ifhidefigures
\let\zz\write
\protected\def\write{\immediate\zz}%
\def\conditionalfigure@start
{%
\def\@captype{figure}\setbox0\vbox\bgroup\color@setgroup
}%
\def\conditionalfigure@finish
{%
\color@endgroup\egroup
}%
\else
\if\relax\detokenize{#1}\relax
\def\conditionalfigure@start
{\begin{figure}}%
\else
\def\conditionalfigure@start
{\begin{figure}[#1]}%
\fi
\def\conditionalfigure@finish
{\end{figure}}%
\fi
\conditionalfigure@start
}
{%
\conditionalfigure@finish
}
\makeatother

\begin{document}
\listoffigures
Figure \ref{figure1} shows \ldots

\begin{conditionalfigure}
\centering
\includegraphics[width=5cm]{example-image-duck}
\caption{Figure 1\label{figure1}}
\end{conditionalfigure}

Figure \ref{figure2} shows \ldots

\hidefigurestrue
\begin{conditionalfigure}
\centering
\includegraphics[width=5cm]{example-image-b}
\caption{Figure 2\label{figure2}}
\end{conditionalfigure}
\end{document}