4

In this example, I would like to remove the text displayed by the command \subcaption{\label{left} Left half} (as well as \subcaption{\label{right} Right half}). However I would still need the labels left and right to work in the text.

The reason for this is that I have a complex array of subfigures that I create as a single image file, and which includes the labels "a", "b", etc in the image itself.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{example-image-a}

\begin{minipage}{0.25\textwidth}
\subcaption{\label{left} Left half}
\end{minipage}
\begin{minipage}{0.25\textwidth}
\subcaption{\label{right} Right half}
\end{minipage}

\caption{\label{figure} Overall caption}
\end{figure}

Figure~\ref{figure} has \ref{left} on the left, and \ref{right} on the right,
also known as \subref{left} and \subref{right}.

\end{document}
Mico
  • 506,678
astabada
  • 206
  • Compliments on an outstanding minimum working example (MWE)! – Mico Mar 06 '17 at 06:51
  • 1
    @Mico Although this might cost me the upvotes, I have to admit my MWE is adapted (viz. 99% copied) from another question on this very site (the question was related but not the same). – astabada Mar 06 '17 at 12:13
  • Nothing wrong with adapting existing code! :-) – Mico Mar 06 '17 at 13:08

2 Answers2

5

I suggest you (a) use subfigure environments instead of minipage environments, (b) assign zero width to the subfigure environments, and -- most importantly -- (c) use \refstepcounter directives instead of caption directives to increment the counter named subfigure without generating any text. The \label directives should remain in place, of course.

Doing so will print out the overall caption below the composite graph and generate the following sentence:

Figure 1 has 1a on the left and 1b on the right, also known as a and b.

In case you're wondering what's going on here: LaTeX's \caption directive not only typesets its argument as the caption text (duh), it also invokes a \refstepcounter instruction to increment the counter (called subfigure) associated with the current float. It's this counter that the \label instruction needs to find and latch on to.

It's also worth mentioning that the subcaption package actually provides a dedicated macro called \phantomsubcaption for exactly your use case, viz., not wanting to produce a subcaption-related text while preserving the ability to create cross-references to the subfloat(s). \phantomsubcaption (essentially) performs the instruction \refstepcounter{subfigure} for you. Use \phantomsubcaption in lieu of \refstepcounter{subfigure} if you prefer. In fact, if you wish to use \phantomsubcaption, it's not actually necessary to set up zero-width subfigure environments; just enclose the \phantomsubcaption and \label statements in a TeX group, like so:

    {\phantomsubcaption\label{left} \phantomsubcaption\label{right}}

A full MWE:

\documentclass{article}
\usepackage{graphicx,subcaption}

\begin{document}

\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{example-image-a}%
% We now insert 2 zero-width 'subfigure' environments 
% to the **right** of the graph. By encasing the
% '\refstepcounter{...}\label{...}' statements inside
% 'subfigure' environments, the subsequent '\subref' 
% instructions will generate the expected output.
\begin{subfigure}{0\textwidth}
\refstepcounter{subfigure}\label{left}
\end{subfigure}%
\begin{subfigure}{0\textwidth}
\refstepcounter{subfigure}\label{right}
\end{subfigure}

\caption{Overall caption}\label{figure} 
\end{figure}

Figure~\ref{figure} has \ref{left} on the left and \ref{right} on the right,
also known as \subref{left} and \subref{right}.

\end{document}
Mico
  • 506,678
  • Isn't subfigure deprecated ? see here, for example, but I've seen that on other places. Not a rant, I used to use it a lot, but I'm confused now... – kebs Mar 06 '17 at 08:41
  • 2
    @kebs - Are you maybe confusing the package called subfigure -- which is indeed deprecated -- with the environment called subfigure, which is provided by the subcaption package? Observe that neither my code nor that of the OP loads the deprecated subfigure package. – Mico Mar 06 '17 at 08:52
  • I added my answer because I hadn't seen your mention of \phantomsubcaption. Nice with an explanation (so +1), but why didn't you use \phantomsubcaption in the code? I for one would never go the trouble of the zero-width subfigures if I knew of \phantomsubcaption, so why highlight that method? – Torbjørn T. Mar 06 '17 at 12:31
  • @Mico oops, correct, I indeed thought it was about the package, thanks. – kebs Mar 06 '17 at 13:04
  • @TorbjørnT. - I had a hunch the OP might like receiving an explanation of "what goes on under the hood". :-) Actually, I thought it might be interesting to elaborate a bit on the deeper connection between \caption on the one hand and \label on the other by way of \refstepcounter (which, in turn, though not mentioned explicitly in my answer, invokes \@currentlabel). I admit it may have been too much of a good thing. I'll go ahead and add a paragraph about how to use \phantomcaption without setting up subfigure environments. – Mico Mar 06 '17 at 13:26
  • 1
    I have no problem with opening the hood, but I think it might be better to first say "use \phantomsubcaption like this: <code example>" (something like what I did in my answer), and then go on to say "to understand what is going on, ... <second code example>". (Not necessarily with those exact words of course.) – Torbjørn T. Mar 06 '17 at 13:26
  • @TorbjørnT. - I'll be more to point next time... :-) – Mico Mar 06 '17 at 13:28
5

This is what \phantomsubcaption is for. Note that the phantom subcaptions has to be in a group of their own, hence the pair of braces around them.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{example-image-a}

{\phantomsubcaption\label{left}
\phantomsubcaption\label{right}}

\caption{\label{figure} Overall caption}
\end{figure}

Figure~\ref{figure} has \ref{left} on the left, and \ref{right} on the right,
also known as \subref{left} and \subref{right}.

\end{document}

enter image description here

Torbjørn T.
  • 206,688
  • Thanks but I accepted @Mico's answer b/c it gives more context. – astabada Mar 06 '17 at 12:10
  • 1
    @astabada You haven't accepted any answers yet. Upvoting (clicking the up-arrow) means "this answer was useful", accepting (clicking the checkmark below the arrows) usually indicates that this is the solution used in the end. That said, I think I'll delete my answer, because I see Mico mentioned \phantomsubcaption in his answer as well, before I added mine. (It's just a bit of a mystery to me why he chose to use the really cumbersome way with the \refstepcounter when he knew of \phantomsubcaption.) – Torbjørn T. Mar 06 '17 at 12:27