# how to use subfigimg macro and \label

I'm using the macro subfigimg provided in this post and it is working great. My problem however is that I would like to \ref to one of the subfigures (which use subfigimg macro) and i have no idea how to do it.

To give an exemple, I would like to do something like that:

\documentclass{revtex4-1} % http://ctan.org/pkg/ltxutil
\usepackage{graphicx} % http://ctan.org/pkg/graphicx
\newcommand{\subfigimg}[3][,]{%
\setbox1=\hbox{\includegraphics[#1]{#3}}% Store image in box
\leavevmode\rlap{\usebox1}% Print image
\rlap{\hspace*{10pt}\raisebox{\dimexpr\ht1-2\baselineskip}{#2}}% Print label
\phantom{\usebox1}% Insert appropriate spcing
}
\begin{document}
\begin{figure}[H]
\centering
\begin{tabular}{p{0.5\linewidth}@{}p{0.5\linewidth}}
\subfigimg[width=\linewidth]{(a)}{example-image-a}
\label{fig:1} &
\subfigimg[width=\linewidth]{(b)}{example-image-b}
\label{fig:2}
\end{tabular}
\caption{bla bla bla}
\end{figure}
\end{document}


Which of course does not work and result in ?? instead of a number. is there a way to work around it? or add option to label in the macro?

I'm really new to tex and to this forum, so please forgive me for being a noob and if I made any mistake.

thanks, liron

• make small but complete document beginning with \documentclass{...} and ending with \end{document}. – Zarko Aug 23 '17 at 14:47
• Is there any reason for not using the subcaption or subfig package? – user31729 Aug 23 '17 at 15:10
• like I said I'm a noob so i probably missed a lot of options. I'm not limiting myself to anything. the final result should be something that: 1. let me put the caption on the figure (which is why i use subfigimg). 2. will let me \ref to any of the subfigimg. If you have any other idea I will be happy to hear. – Liron Levy Aug 23 '17 at 15:18
• @Christian Hupfer I have tried the subcaption pack and it almost does the job. If i use \subcaption{} it is working well, but as i do not want to see the the exra caption i tried using \subcaption*{} but then I get the same problem again. (getting ?? instead of x.a link) – Liron Levy Aug 23 '17 at 15:38

I build something that is a) a complete MWE and b) does what you want.

Now the labels are displayed like "1.a" and the letters are changed to small letters. If you want to change the formatting of the subfigimg labels change the \renewcommand*{\thesubfigs} to your liking, note that this only changes the way the labels are displayed, not those inside the images, for that change the \alph{subfigs} in the definition of \@subfigimg.

The command \subfigimg is available as a starred and an unstarred version. Use the starred version if you use \caption in the figure environment prior to your \subfigimg and the unstarred one else. This is necessary because we have to increase the c@figure-counter temporarily in order to get correct labels if the \caption is used after \subfigimg.

If you don't need a label on a \subfigimg just leave the last argument empty.

I added the possibility to change the position of the label with two additional arguments. They are to be inserted in between the 2 mandatory arguments. The first is inserted with <horizontal> (so <> as delimiters) and changes the horizontal offset, the second [vertical] changes the vertical offset.

\documentclass[preview,border=4mm]{standalone}

\usepackage{graphicx}
\usepackage{xparse}

\newcounter{subfigs}[figure]
\renewcommand*{\thesubfigs}{\thefigure.\alph{subfigs}}

\makeatletter
\NewDocumentCommand{\subfigimg}{s O{} m D<>{10pt} O{2\baselineskip} m}{%
\IfBooleanTF{#1}%
{\@subfigimg{#2}{#3}{#6}{0}{#4}{#5}}%
{\@subfigimg{#2}{#3}{#6}{1}{#4}{#5}}%
}
\newcommand*{\@subfigimg}[6]{%
\bgroup%
\refstepcounter{subfigs}%
\ifx\relax#3\relax\else%
\label{#3}%
\fi%
\setbox1=\hbox{\includegraphics[#1]{#2}}% Store image in box
\leavevmode\rlap{\usebox1}% Print image
\rlap{\hspace*{#5}\raisebox{\dimexpr\ht1-#6\relax}{(\alph{subfigs})}}% Print label
\phantom{\usebox1}% Insert appropriate spcing
\egroup%
}

\makeatother

\begin{document}
\begin{figure}
\centering
\begin{tabular}{p{0.5\linewidth}@{}p{0.5\linewidth}}
\subfigimg[width=\linewidth]{example-image-c}{fig:s11}
&
\subfigimg[width=\linewidth]{example-image-b}{fig:s12}
\end{tabular}
\caption{bla bla bla}
\label{fig:1}
\end{figure}
See subfigures \ref{fig:s11} and \ref{fig:s12} of figure~\ref{fig:1}
\begin{figure}
\centering
\caption{bla bla bla}
\label{fig:2}
\begin{tabular}{p{0.5\linewidth}@{}p{0.5\linewidth}}
\subfigimg*[width=\linewidth]{example-image-c}<20pt>[4\baselineskip]{fig:s21}
&
\subfigimg*[width=\linewidth]{example-image-b}{fig:s22}
\end{tabular}
\end{figure}
See subfigures \ref{fig:s21} and \ref{fig:s22} of figure~\ref{fig:2}
\end{document}


Note that the subfigs counter is only reset after the figure counter is increased or when you manually reset it with something like \setcounter{subfigs}{0}.

• ooh wow that is great, anyway to do it so that it will reference \red{fig:s1} as 1.a ? (like he does with subfigure) and just switch the capital letters to small ones? or is it too much to ask? thanks a lot... – Liron Levy Aug 23 '17 at 15:06
• @LironLevy the problem is, that the figure counter is increased by the \caption-command. Do you always use \caption after the \subfigimg? – Skillmon Aug 23 '17 at 15:29
• yes i always use \caption after each figure (like in the example) – Liron Levy Aug 23 '17 at 15:36
• @LironLevy please see my edit. – Skillmon Aug 23 '17 at 15:44
• @Mico yes there is. I posted my initial answer before the OP posted a complete MWE, so I took the standalone class out of convenience. – Skillmon Aug 23 '17 at 16:32

Here's a solution that employs the widely-used subcaption package, which provides LaTeX environments called subfigure and subtable. The package also provides lots of options for configuring the formats of all captions.

\documentclass{revtex4-1}
\usepackage{graphicx,subcaption}
\makeatletter
\renewcommand{\p@subfigure}{\thefigure.} % note the '.'
\makeatletter

\begin{document}

\begin{figure}

\begin{subfigure}{0.475\textwidth}
\includegraphics[width=\linewidth]{example-image-a}
\caption{First subfigure}\label{fig:small_a}
\end{subfigure}\hfill
\begin{subfigure}{0.475\textwidth}
\includegraphics[width=\linewidth]{example-image-b}
\caption{Second subfigure}\label{fig:small_b}
\end{subfigure}

\caption{bla bla bla} \label{fig:big}
\end{figure}

\noindent
Some cross-references to Figure \ref{fig:big} and to Subfigures \ref{fig:small_a} and \ref{fig:small_b}.
\end{document}
`
• thanks, but i used \subfigimg macro as i want the caption to be on top of the figure. which was what caused my problem. sorry i wasn't more clear. But @Skillmon answer just solve the problem. – Liron Levy Aug 23 '17 at 16:28