2

I have a figure with three subfigures shown in the code sample below. How can I change the reference to a subfigure so that I have Figure 1 (left), Figure 1 (center), and Figure 1 (right) instead of figure 1.1, 1.2, and 1.3? Also, in the caption I would like to it to have a general caption, then in the same paragraph have (left) left's caption (center) center's caption (right) right's caption.

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure*}
\begin{minipage}[b]{0.33\linewidth}
\label{fig:left}
\includegraphics[width=2.5in,height=2.5in]{img1}
\end{minipage}
\begin{minipage}[b]{0.33\linewidth}
\includegraphics[width=2.5in,height=2.5in]{img2}
\label{fig:center}
\end{minipage}
\begin{minipage}[b]{0.33\linewidth}
\includegraphics[width=2.5in,height=2.5in]{img3}
\label{fig:right}
\end{minipage}
\caption{
    Blah blah blah.  
    \ref{fig:left} blah blah.   
    \ref{fig:center} blah blah.   
    \ref{fig:right} blah blah.
}
\label{fig:all}
\end{figure*}
\end{document}
4
  • Your code snippet doesn't include any captions for each of the figures, yet you say you want Figure 1 (left), Figure 1 (center) and Figure 1 (right). Is this correct... you want these captions? Are you using hyperref? Why don't you just type \caption{Blah blah blah. (left) blah blah. (center) blah blah. (right) blah blah.}?
    – Werner
    Sep 18, 2012 at 5:07
  • I'd simply write the caption as \caption{Blah blah. Left: blahleft. Center: blahcenter. Right: blahright.}
    – egreg
    Sep 18, 2012 at 15:52
  • That's basically how I've done it right now, but it would be nice to be able to reference the left/center/right pictures within a paragraph. Sep 18, 2012 at 18:06
  • I've sometimes done this by using \label for the overall figure, which presents the three subfigures as (a), (b), and (c). Then, when I reference the left figure in the text, I would use \ref{fg:figlabel}a. Correspondingly, \ref{fg:figlabel}b for the middle and \ref{fg:figlabel}c for the right figure. Feb 22, 2013 at 14:32

2 Answers 2

2

In what follows, a new numbering system called \lcr is defined (which works the same way as \arabic, \alph, etc. do) that associates "left" to "1", "centre" to "2", and "right" to "3".

The prettyref package is then used to customise the cross-referencing to either of the three "subfigures". Note that \phantomsubcaption must be used in order to create an anchor for \ref.

\documentclass{article}

\makeatletter
\def\lcr#1{\expandafter\@lcr\csname c@#1\endcsname}% LaTeX
\def\@lcr#1{%
  \ifnum#1=0%
    \@ctrerr% 
  \else\ifnum#1=1%
      left%
    \else\ifnum#1=2%
       center%
     \else\ifnum#1=3%
        right%
      \else
        \@ctrerr%
      \fi
    \fi
  \fi
}
\makeatother

\usepackage{subcaption}

\DeclareCaptionSubType*{figure}
\renewcommand\thesubfigure{\thefigure~(\lcr{subfigure})}

\usepackage{prettyref}
\newrefformat{subfiglcr}{Figure~\ref{#1}}

\usepackage{tikz}
%\usepackage{graphicx}

\begin{document}

\begin{figure*}
    \begin{minipage}[b]{0.3\linewidth}
        \centering
        \begin{tikzpicture}
            \path[draw=red,fill=red!20] (0,0) rectangle (4,4);
        \end{tikzpicture}
        \phantomsubcaption \label{subfiglcr:left}
    \end{minipage}
    \hfill
    \begin{minipage}[b]{0.3\linewidth}
        \centering
        \begin{tikzpicture}
            \path[draw=green,fill=green!20] (0,0) rectangle (4,4);
        \end{tikzpicture}
        \phantomsubcaption \label{subfiglcr:center}
    \end{minipage}
    \hfill
    \begin{minipage}[b]{0.3\linewidth}
        \centering
        \begin{tikzpicture}
            \path[draw=blue,fill=blue!20] (0,0) rectangle (4,4);
        \end{tikzpicture}
        \phantomsubcaption \label{subfiglcr:right}
    \end{minipage}
    \caption{Three squares. Left: red. Center: green. Right: blue.}
    \label{fig:all}
\end{figure*}

\prettyref{subfiglcr:left} shows a typical example of red square;
\prettyref{subfiglcr:center} shows a particularly interesting specimen of green square,
while \prettyref{subfiglcr:right} shows a common and rather unremarkable blue square.

\end{document}

enter image description here

In this example, the tikz package is used instead of the graphicx package (used by the OP), in order to generate some placeholder pictures.

0

In case you want to make the change specific only to a particular Figure, you can modify @jub0bs answer and use: \renewcommand{\thesubfigure}{\lcr{subfigure}} in the begging of the figure

\begin{figure}

\renewcommand{\thesubfigure}{\lcr{subfigure}}
\begin{minipage}[b]{.33\linewidth}

    \centering\large A
    \subcaption{A subfigure}\label{fig:1a}
\end{minipage}%
\begin{minipage}[b]{.33\linewidth}
    \centering\large B
    \subcaption{Another subfigure}\label{fig:1b}
\end{minipage}%
\begin{minipage}[b]{.33\linewidth}
    \centering\large C
    \subcaption{Yet another}\label{fig:1c}
 \end{minipage}
   \caption{A figure}\label{fig:1}
 \end{figure}

 Figure~\ref{fig:1a}

Full code:

\documentclass{article}

\makeatletter
\def\lcr#1{\expandafter\@lcr\csname c@#1\endcsname}% LaTeX
\def\@lcr#1{%
  \ifnum#1=0%
    \@ctrerr% 
  \else\ifnum#1=1%
      left%
    \else\ifnum#1=2%
       center%
     \else\ifnum#1=3%
        right%
      \else
        \@ctrerr%
      \fi
    \fi
  \fi
}
\makeatother

\usepackage{subcaption}

\DeclareCaptionSubType*{figure}
\renewcommand\thesubfigure{\thefigure~(\lcr{subfigure})}

\usepackage{prettyref}
\newrefformat{subfiglcr}{Figure~\ref{#1}}

\usepackage{tikz}
%\usepackage{graphicx}

\begin{document}
\begin{figure}

\renewcommand{\thesubfigure}{\lcr{subfigure}}
\begin{minipage}[b]{.33\linewidth}

    \centering\large A
    \subcaption{A subfigure}\label{fig:1a}
\end{minipage}%
\begin{minipage}[b]{.33\linewidth}
    \centering\large B
    \subcaption{Another subfigure}\label{fig:1b}
\end{minipage}%
\begin{minipage}[b]{.33\linewidth}
    \centering\large C
    \subcaption{Yet another}\label{fig:1c}
 \end{minipage}
   \caption{A figure}\label{fig:1}
 \end{figure}

 Figure~\ref{fig:1a}

\end{document}
2
  • Please make your code compilable by adding the documentlcass as well as the relevant packages that are needed for your code to successfully compile. Did you use \lcr from jub0bs' answer? It might be worth mentioning that as well.
    – leandriis
    May 23, 2020 at 11:53
  • Yeah, it is a copy/ paste
    – layser
    May 26, 2020 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .