Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

A couple of times I've had trouble using labels on minipage. I use minipage to create subfigures a) and b), each with their own \subcaption and \label, the whole figure also got a \caption with a \label. Then I want to use \cref to refer to the individual subfigures/subcaption. However when I use \cref with the minipage labels, it refers to the section the figure is placed.

My code looks like this:

\begin{figure}[h!]
\begin{minipage}{0.5\textwidth}
    \def\svgwidth{0.9\columnwidth}
    \input{filepath}
    \subcaption{\textsl{Subfigure 1 text}}
    \label{fig:sub:subfigure1}
\end{minipage}
\begin{minipage}{0.5\textwidth}
    \def\svgwidth{0.9\columnwidth}
    \input{filepath}
    \subcaption{\textsl{Subfigure 2 text.}}
    \label{fig:sub:subfigure2}
\end{minipage}
\caption{\textsl{Figure text.}}
\label{fig:whole_figure}
\end{figure}

Then I use \cref{fig:sub:subfigure1}, which gives me 'Section 2.3´, instead of 'Figure 4.a´ .

Also it should be noted that I use the memoir class, also I use the .pdf_tex format as figures.

I tried to read a bit in the \cref manual, but couldn't understand how to solve my problem, I also did some googling with no results. Can you help me solve this problem?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

For a LaTeX cross-reference to some numbered "object" to work properly, the \label command that's associated with the object generally also has to increment the counter variable that's associated with the object via the \refstepcounter mechanism. Since minipage environments are not numbered automatically in this manner, you're finding that the \cref command ends up generating a cross-reference to the object that was most recently incremented via \refstepcounter -- in this case, a section or subsection object -- at the time the minipage's \label command was issued.

Fortunately, there's a solution which works very well with the cleveref package and which works both with the memoir class and the "standard" LaTeX document classes (article, report, and book): Load the subcaption package and use its subfigure environment instead of minipage. A subfigure environment, for all practical purposes, is the same as a minipage environment. The only important difference is that a subfigure environment is automatically associated with a counter variable that's incremented via \refstepcounter. Hence, cross-references to subfigure environments will work exactly as expected..

Incidentally, if you want all captions to be typeset automatically in slanted-roman text, you can tell LaTeX to do so by providing the instruction \captionsetup{textfont = sl}. (The \captionsetup macro is provided by the caption package, which is loaded automatically by the subcaption package). Doing so globally, rather than providing \textsl instructions separately in each and every caption, is not only convenient but will also simplify your life considerably in the future in case you decide to use a different font shape.

enter image description here

\documentclass{article}
\usepackage{subcaption}
   \captionsetup{textfont = sl} % use slanted font shape automatically for all captions
\usepackage[noabbrev]{cleveref}
\begin{document}
\setcounter{figure}{3} % just for this example
\begin{figure}[h!]
\begin{subfigure}{0.5\textwidth}
    \def\svgwidth{0.9\columnwidth}
    %\input{filepath}
    \caption{Subfigure 1 text}
    \label{fig:sub:subfigure1}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
    \def\svgwidth{0.9\columnwidth}
    %\input{filepath}
    \caption{Subfigure 2 text.}
    \label{fig:sub:subfigure2}
\end{subfigure}
\caption{Figure text.}
\label{fig:whole_figure}
\end{figure}
\cref{fig:sub:subfigure1}
\end{document}

Addendum: To get cross-references to subfigures formatted as "4.a" instead of "4a", you could add the following instructions in the preamble (after loading subfigure and before loading cleveref):

\makeatletter
\renewcommand{\p@subfigure}{\thefigure.}
\makeatletter

The macro \p@subfigure serves to insert a formatted "prefix" in cross-references to items that involve subfigure counters. Here, the "prefix" consists of the figure number followed by a "." (dot).

share|improve this answer

You need to place the \label inside the \subcaption, then it works. You can set the font of all captions and subcaptions to slanted by adding

\captiontitlefont{\slshape}
\subcaptionfont{\slshape}

As an alternative to the minipages, you can use \subbottom, see below code. I replaced the \inputs with \rule{3cm}{1cm} which creates a black rectangle.

\documentclass[a4paper]{memoir}
\newsubfloat{figure}
\captiontitlefont{\slshape}
\subcaptionfont{\slshape}

\usepackage{cleveref}

\begin{document}
\cref{fig:sub:subfigure1}

\cref{fig:sub:subfigure2}

\cref{fig:whole_figure}

\begin{figure}[h!]
  \begin{minipage}{0.5\textwidth}
    \centering
    \rule{3cm}{1cm}
    \subcaption{\textsl{Subfigure 1 text}\label{fig:sub:subfigure1}}
\end{minipage}
\begin{minipage}{0.5\textwidth}
  \centering
    \rule{3cm}{1cm}
    \subcaption{\textsl{Subfigure 2 text.}\label{fig:sub:subfigure2}}
\end{minipage}
\caption{\textsl{Figure text.}}
\label{fig:whole_figure}
\end{figure}

\begin{figure}[h!]
  \centering
  \subbottom[Subfigure 1 caption]{\rule{3cm}{1cm}\label{sub1}}
  \hspace{1cm}
  \subbottom[Subfigure 2 caption]{\rule{3cm}{1cm}\label{sube}}
\caption{Figure text.}
\label{fig:secondfig}
\end{figure}

\end{document}

enter image description here

share|improve this answer

When making captions and subcaptions (globally) slanted, while using the memoir package, one should use \subcaptionfont{\slshape} (as stated by Torbjørn T.) and \captiontitlefont{\slshape} in the preamble. This will make both the caption and subcaption slanted as opposed to Torbjørn's example where \textsl{} is manually set in each caption.

(This should probably be a comment but my reputation is too low. Wonder why I'm allowed to answer but not comment while being a n00b?)

share|improve this answer
    
Thanks, I didn't really notice that the main caption was set slanted as well. Silly of me. Edit: Oh, and welcome to TeX.SX! –  Torbjørn T. May 11 at 12:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.