8

The cleveref package does a very nice job of sorting the labels of the many references for you (whatever they are) in their natural order.
However, I was stumbled by a special case when I wanted to refer to parts of a figure that had already letters like A, B, C, D, etc. generated by external program like Photoshop or a similar software.
What I want is to refer to these parts of the figure (which are not marked by LaTeX as subfigures or subfloats) and still be consistent with the rest of my latex document using the multiple references to figures by cleveref package.

To illustrate this, you would type this in LaTeX when you typically want to refer to multiple subfigures using the cleveref package:

\cref{fig:lab1:1, fig:lab1:3, fig:lab1:2}  

this would yield the desired typesetting result (normal behavior):

figs. 1.1a, 1.1b and 1.1c

Please note that cleveref sorted out the order of the subfigues, plus it added the type of the reference, fig. in this case, and more importantly added s because they were more than one reference. Also note that the reference number (1.1 in this case) was repeated with the different subfigure letters.
Now back to the special occasion, the figure which was already annotated using an external program with (A,B,C,D). It should be obvious that I cannot use multiple references the usual way, since I don't have separate labels for each part of the figure, I only have one label for the whole figure but not for its parts.
Therefore, as a workaround, I tended to use the following code to mimic the output of cleveref package had separate labels for the parts of the figure been available:

\cref{fig:lab1}a, \ref{fig:lab1}b and \ref{fig:lab1}c  

BUT the output of this code was suboptimal and didn't reproduce the s to denote many figures as it should be. More importantly, the output is inconsistent with the rest of the document:

fig. 1.1a, 1.1b and 1.1c

What is needed:

  1. One alternative is to remove the s story from all of the document, i.e., customizing the cleveref package so that not to add the s wherever multiple references are called out. I would be grateful if you could show me how to do this in code.
  2. To fix this inconsistency issue using some other superior solution that I had no idea of (like usual).
  • How about just breaking down your picture into multiple parts? That would make more sense. – jub0bs Dec 10 '13 at 10:50
  • Maybe not quite suitable, as I have to imagine the figure with its annotations, but why not simply type “ parts A, B and C of \cref{fig:1ab1}”? – Bernard Dec 10 '13 at 10:52
  • well, this would not be an elegant easy solution, the figure was already done with Photoshop as I described, with many parts, it would be easier to change the behavior of x-referencing I guess. – doctorate Dec 10 '13 at 10:54
  • @Bernard, but that would break the rules of cleveref and x-referencing in the document. As I said, I want to keep x-referencing consistent throughout the whole document. – doctorate Dec 10 '13 at 10:57
8

Here is a solution that gives you the possibility to add labels for your subfigures without a subcaption using the subcaption package. That's actually the way I'm used to handle subfigures because I do not like explicit subcaption but prefer to refer to the single pictures in the main caption.

\documentclass{article}

\usepackage{mwe} % only needed for the images
\usepackage{subcaption}

\usepackage{cleveref}

% Capitals for the subfigures
\DeclareCaptionSubType[Alph]{figure}

\begin{document}
\begin{figure}
    \centering
    {\includegraphics[width=.48\linewidth]{example-image-a}
        \phantomsubcaption\label{fig:a}}
    \hfil
    {\includegraphics[width=.48\linewidth]{example-image-b}
        \phantomsubcaption\label{fig:b}}
    \caption{Some illustrative examples: (\subref{fig:a}) an image with an \emph{A}
                and (\subref{fig:b}) an image with a \emph{B}}
\end{figure}

\Cref{fig:b} is next to \cref{fig:a} and we can see \cref{fig:b,fig:a}.
\end{document}

The \phantomsubcaption does not create a subcaption but an point you can create label for. So you can easily references all the subfigures without a real subcaption.

enter image description here

Edit: You can also use \phantomsubcaption with only one or even without any image:

\begin{figure}
    \centering
    \includegraphics[width=.48\linewidth]{example-image-16x9}
    {\phantomsubcaption\label{fig:a}}
    {\phantomsubcaption\label{fig:b}}
    \caption{Some illustrative examples: (\subref{fig:a}) an image with an \emph{A}
                and (\subref{fig:b}) an image with a \emph{B}}
\end{figure}

enter image description here

  • but you assume that I have two separate subfigues which I don't. One question, though, can one issue more than one \phantomsubcaption\label{fig:a} macro with a, b, c, etc. within one figure float in order to refer to them later by \cref? That would be an elegant alternative to seek. If so, pls update your answer (there should be one \includegraphics macro in your MWE to mimic the case). – doctorate Dec 11 '13 at 6:53
  • @doctorate, see my edit. – quinmars Dec 11 '13 at 12:22
  • 1
    +1 to you for \phantomsubcaption, and also +1 to the subcaption authors for it! – yo' Dec 11 '13 at 12:29
  • very nice answer, \phantomsubcaption is a new discovery for me. But you answered another question I had of how to capitalize the subcaptions. I cannot upvote twice for one answer. – doctorate Dec 11 '13 at 14:48
3

You ask for a way to suppress the plural "s" whenever the \cref macro outputs multiple cross-references to figures. One such way involves the \crefname macro. For instance, you could issue the instruction

\crefname{figure}{fig.}{fig.}

so that "fig." shows up regardless of whether a single cross-reference or multiple cross-references are generated via \cref.

  • so is the third argument for the multiple? – doctorate Dec 10 '13 at 21:20
  • @doctorate - that's right: the first argument is the name the counter, the second argument states the singular form of the noun to be prefixed to the (single) cross-referenced item, and the third gives the plural form of the noun. – Mico Dec 10 '13 at 21:38

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.