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.

I am writing my master thesis using Memoir. I want all subfigures to have their labels in the top left corner. I have so far done this with tikz, but I would like to find a more elegant solution as my current solution has flaws.

I would like to be able to automatically reference figures in the text, without having to manually specify the subfigure. This means each figure needs to have a label. Yet if I label the subfigure, said label invariably appears underneath the figure, and I am not sure how to move it, change its color and so on.

Anyway, an mwe and images of current and desired solution:

\documentclass{memoir}

\usepackage{tikz,siunitx,mwe}
\newsubfloat{figure}

\newcommand{\scalebarimg}[6]{
  \begin{tikzpicture}
    \draw node[name=micrograph] {\includegraphics[width=#2\textwidth]{#1}}; %I fetch the image
    \draw[ultra thick,#6] (micrograph.south west)++(0.03*0#2\textwidth,0.035*0#2\textwidth)--++(#2*#3\textwidth,0)node[above,midway]{#4 \si{\micro\meter}}; %I draw the scalebar
    \draw  (micrograph.north west)  node[anchor=north west,yshift=-1,#6]{\textbf{\small{(#5)}}}; %I draw the image label
  \end{tikzpicture}
}

\begin{document}
\begin{figure}
\centering
\subbottom[\label{fig:one}]{
    \scalebarimg{example-image}{.45}{0.33933}{50}{a}{black}
}\subbottom[\label{fig:two}]{
    \scalebarimg{example-image}{.45}{0.33933}{50}{b}{white}
}
\caption{Figures (a) and (b) should not be doubly labeled.}
\end{figure}



See Figure~\ref{fig:one} and \subcaptionref{fig:two}.

\end{document}

which yields
enter image description here
but I want
enter image description here

Is there a way of letting LaTeX position the label where I want it to relative the top left corner of the image? Or at least hide the caption for each individual subfigure while still labelling it?

share|improve this question
    
Not that I know of, You may have to do this manually, for example using overpic and a counter defined to reset each time figure is changed. –  daleif Apr 10 at 11:14
    
I presume you would still need the ability to \ref the subfigures? Or can that be dispensed with? –  Steven B. Segletes Apr 10 at 11:49
    
Yes, I would like to keep the ability to use both \ref and \subcaptionref –  Mr. V Apr 10 at 12:19

2 Answers 2

up vote 5 down vote accepted

I would use the subcaption package and \phantomsubcaption (or \phantomcaption if you decide to use e.g., subfigure environment). Note that you need version 1.1 of the subcaption package. Below is your adapted MME:

\documentclass{memoir}

\usepackage{tikz,siunitx,mwe}
\usepackage{subcaption}
\newsubfloat{figure}

\newcommand{\scalebarimg}[6]{
  \begin{tikzpicture}
    \draw node[name=micrograph] {\includegraphics[width=#2\textwidth]{#1}}; %I fetch the image
    \draw[ultra thick,#6] (micrograph.south west)++(0.03*0#2\textwidth,0.035*0#2\textwidth)--++(#2*#3\textwidth,0)node[above,midway]
    {#4 \si{\micro\meter}}; %I draw the scalebar
    \draw  (micrograph.north west)  node[anchor=north west,yshift=-1,#6]{\textbf{\small{#5}}}; %I draw the image label
  \end{tikzpicture}
}

\begin{document}
\begin{figure}
   {\phantomsubcaption\label{fig:one}
   \scalebarimg{example-image}{.45}{0.33933}{50}{\subref{fig:one}}{black}}
   {\phantomsubcaption\label{fig:two}
   \scalebarimg{example-image}{.45}{0.33933}{50}{\subref{fig:two}}{white}}
\caption{Figures (a) and (b) should not be doubly labeled.}
\end{figure}

See Figure~\ref{fig:one} and \subcaptionref{fig:two}.

\end{document}

Result of compilation

I surrounded each subfigure with just {} (needed to separate figure from subfigure) but you can put them for example in minipage, subfigure etc. If you want to customise label, see subcaption documentation.

share|improve this answer

I see a nice answer has appeared, but since I was working on this one, I'll present it also. The \tripleinset macro has 8 parameters plus an optional parameter. They are, in order:

"label" - optional parameter, for later use of \ref. The subfigs add an a, b, and c to this label

\labelhoffset - the horizontal inset of the label. "0in" would actually be centered over the left margin, but that is shifted to prevent that.

\labevhoffset - for the vertical inset of the label. "0in" would actually be centered over the top margin, but that is shifted to prevent that.

\interfiggap - the horizontal gap between figures

\figa - left most figure

\figb - center figure

\figc - right most figure

\figurelableoffset - the gap between lowest subfigure and caption

"caption" - the caption

\documentclass{memoir}
\usepackage{hyperref}
\usepackage[usestackEOL]{stackengine}
\usepackage{subcaption}
\usepackage{graphicx}
\newlength\gapa
\newlength\gapb
\newlength\gapc
\newlength\labelhoffset
\newlength\labelvoffset
\newlength\figlabeloffset
\newlength\vskiplength
\newlength\interfiggap
\newlength\totalheight
\newcommand\tripleinset[9][]{%
  \labelhoffset=#2\relax% cannot exceed 1/2 the figure width or figures shift
  \labelvoffset=#3\relax
  \figlabeloffset=#8\relax
  \interfiggap=#4\relax
  \def\figa{#5}
  \def\figb{#6}
  \def\figc{#7}
  \ifdim\labelvoffset<.5\baselineskip\labelvoffset=.5\baselineskip\relax\fi
  \setbox0=\hbox{\figa}\setlength\gapa{\dimexpr\wd0+\interfiggap-2\labelhoffset\relax}
  \totalheight=\ht0\relax
  \setbox1=\hbox{\figb}\setlength\gapb{\dimexpr\wd1+\interfiggap-2\labelhoffset\relax}
  \ifdim\ht1>\totalheight\totalheight=\ht1\relax\fi
  \setbox2=\hbox{\figc}\setlength\gapc{\dimexpr\wd2-2\labelhoffset\relax}
  \ifdim\ht2>\totalheight\totalheight=\ht2\relax\fi
  \vskiplength=\dimexpr\totalheight-\labelvoffset-\baselineskip+\figlabeloffset\relax
  \savestack{\capfiga}{%
    \subcaptionbox{\label{#1a}}[2\labelhoffset]{\smash{\belowbaseline[\baselineskip-\labelvoffset]{\figa}}}}
  \savestack{\capfigb}{%
    \subcaptionbox{\label{#1b}}[2\labelhoffset]{\smash{\belowbaseline[\baselineskip-\labelvoffset]{\figb}}}}
  \savestack{\capfigc}{%
    \subcaptionbox{\label{#1c}}[2\labelhoffset]{\smash{\belowbaseline[\baselineskip-\labelvoffset]{\figc}}}}
  \vspace{\labelvoffset}
  \capfiga\hspace{\gapa}\capfigb\hspace{\gapb}\capfigc\hspace{\gapc}\vspace{\vskiplength}%
  \caption{#9\label{#1}}
}
\begin{document}
\begin{figure}[htbp]
\centering
\tripleinset[fg:]{0.15in}{0.15in}{0.3in}
  {\includegraphics[width=1in,height=1in]{example-image}}
  {\includegraphics[width=1in,height=1in]{example-image}}
  {\includegraphics[width=1in,height=1in]{example-image}}
  {0.5\baselineskip}{This is my figure}
\end{figure}
\noindent In figure \ref{fg:}, \ref{fg:a}, \ref{fg:b} and \ref{fg:c}.

\begin{figure}[htbp]
\centering
\tripleinset{0.3in}{0.10in}{0.15in}
  {\includegraphics[width=.9in,height=.9in]{example-image}}
  {\includegraphics[width=1in,height=1in]{example-image}}
  {\includegraphics[width=1.1in,height=1.1in]{example-image}}
  {0.2\baselineskip}{This is my other figure}
\end{figure}
\end{document}

enter image description here

share|improve this answer
    
Nice parallel answer to mine, in a sense that it moves caption instead of drawing it in the image. +1 –  Tahtisilma Apr 10 at 13:33
    
@Tahtisilma In my case, it is even more subtle. I move the image to place it under the subcaption label. and +1 back at you. –  Steven B. Segletes Apr 10 at 13:40

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.