3

I'm writing a paper and the convention is to put several figures together, each of them with the marking "a)", "b)", etc... directly on top of the figure.

The perfect solution for me would look like includegraphics having an optional argument to do it, like this:

\includegraphics[scale=0.5,label=a)]{myfig.png}

Is this something that has already been implemented in a package? If not, how would I go about creating my own "includegraphics" that would achieve this?

EDIT: To clarify, I do not mean placing the label above the figure like the sky is above the ocean in the horizon, but I mean placing it on top of it, like a sticker on a banana. The corner of the figure should become covered by the "a)" label. See example below from springer.com

enter image description here

Miguel
  • 504
  • 3
    Please have a look at the subcaption package. – Jesse op den Brouw Oct 10 '18 at 17:35
  • Thanks for the suggestion, I didn't know about this package. Unfortunately it doesn't work for me because there is no possibility to add the "a)" annotation to the picture automatically. – Miguel Oct 10 '18 at 21:38

2 Answers2

13

I avoided to overload \includegraphics and defined a new command that, when label is not specified among the keys, behaves exactly as \includegraphics.

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{xparse}

\ExplSyntaxOn

\keys_define:nn { miguel/label }
 {
  label   .tl_set:N = \l_miguel_label_tl,
  unknown .code:n   = \clist_put_right:Nx \l_miguel_label_clist
                       { \l_keys_key_tl = \exp_not:n { #1 } }
 }
\clist_new:N \l_miguel_label_clist
\box_new:N \l_miguel_label_box
\box_new:N \l_miguel_label_image_box

\NewDocumentCommand{\xincludegraphics}{O{}m}
 {
  \tl_clear:N \l_miguel_label_tl
  \clist_clear:N \l_miguel_label_clist
  \keys_set:nn { miguel/label } { #1 }
  \tl_if_empty:NTF \l_miguel_label_tl
   {
    \miguel_includegraphics:Vn \l_miguel_label_clist { #2 }
   }
   {
    \hbox_set:Nn \l_miguel_label_image_box
     {
      \miguel_includegraphics:Vn \l_miguel_label_clist { #2 }
     }
    \hbox_set:Nn \l_miguel_label_box
     {
      \skip_horizontal:n { 3pt }
      \fcolorbox{white}{white}{\footnotesize \tl_use:N \l_miguel_label_tl}
     }
    \leavevmode
    \box_use:N \l_miguel_label_image_box
    \skip_horizontal:n { -\box_wd:N \l_miguel_label_image_box }
    \hbox_overlap_right:n
     {
      \box_move_up:nn
       {
        \box_ht:N \l_miguel_label_image_box - 
        \box_ht:N \l_miguel_label_box - 3pt
       }
       { \box_use_drop:N \l_miguel_label_box }
     }
    \skip_horizontal:n { \box_wd:N \l_miguel_label_image_box }
   }
 }

\cs_new_protected:Nn \miguel_includegraphics:nn
 {
  \includegraphics[#1]{#2}
 }
\cs_generate_variant:Nn \miguel_includegraphics:nn { V }

\ExplSyntaxOff

\begin{document}

\begin{table}[htp]

\centering

\xincludegraphics[scale=0.3,label=a)]{example-image-a}
\xincludegraphics[scale=0.3,label=b)]{example-image-b}

\xincludegraphics[scale=0.2,angle=90,label=c)]{example-image-c}

\end{table}

\end{document}

enter image description here

Here is an improved version that accepts also the keys

pos = nw | n | ne | e | se | s | sw | w
labelbox = true | false
fontsize = <fontsize command>

The default value for fontsize is \footnotesize; it should be clear what to change for modifying the default. If one says labelbox, the label will be printed on a white background. This can be overridden with labelbox=false (if \setlabel{labelbox} has been used, see the example).

There is also a \setlabel command that can be used to set some keys, keeping the value in the current scope.

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{xparse,xcoffins}

\ExplSyntaxOn
\NewCoffin\imagecoffin
\NewCoffin\labelcoffin

\keys_define:nn { miguel/label }
 {
  label   .tl_set:N = \l_miguel_label_tl,
  labelbox .bool_set:N = \l_miguel_label_box_bool,
  labelbox .default:n = true,
  fontsize .tl_set:N = \l_miguel_label_size_tl,
  fontsize .initial:n = \footnotesize,
  pos .choice:,
  pos/nw .code:n = \tl_set:Nn \l_miguel_label_pos_tl { left,up },
  pos/ne .code:n = \tl_set:Nn \l_miguel_label_pos_tl { right,up },
  pos/sw .code:n = \tl_set:Nn \l_miguel_label_pos_tl { left,down },
  pos/se .code:n = \tl_set:Nn \l_miguel_label_pos_tl { right,down },
  pos/n .code:n = \tl_set:Nn \l_miguel_label_pos_tl { hc,up },
  pos/w .code:n = \tl_set:Nn \l_miguel_label_pos_tl { left,vc },
  pos/s .code:n = \tl_set:Nn \l_miguel_label_pos_tl { hc,down },
  pos/e .code:n = \tl_set:Nn \l_miguel_label_pos_tl { right,vc },
  pos .initial:n = nw,
  unknown .code:n   = \clist_put_right:Nx \l_miguel_label_clist
                       { \l_keys_key_tl = \exp_not:n { #1 } }
 }
\clist_new:N \l_miguel_label_clist
\box_new:N \l_miguel_label_box
\box_new:N \l_miguel_label_image_box

\NewDocumentCommand{\xincludegraphics}{O{}m}
 {
  \group_begin:
  \tl_clear:N \l_miguel_label_tl
  \clist_clear:N \l_miguel_label_clist
  \keys_set:nn { miguel/label } { #1 }
  \tl_if_empty:NTF \l_miguel_label_tl
   {
    \miguel_includegraphics:Vn \l_miguel_label_clist { #2 }
   }
   {
    \SetHorizontalCoffin\imagecoffin
     {
      \miguel_includegraphics:Vn \l_miguel_label_clist { #2 }
     }
    \SetHorizontalCoffin\labelcoffin
     {
      \raisebox{\depth}
       {
        \bool_if:NTF \l_miguel_label_box_bool
         { \fcolorbox{white}{white}{\l_miguel_label_size_tl\l_miguel_label_tl} }
         { \l_miguel_label_size_tl\l_miguel_label_tl }
       }
     }
    \SetVerticalPole\imagecoffin{left}{3pt+\CoffinWidth\labelcoffin/2}
    \SetVerticalPole\imagecoffin{right}{\Width-3pt-\CoffinWidth\labelcoffin/2}
    \SetHorizontalPole\imagecoffin{up}{\Height-3pt-\CoffinHeight\labelcoffin/2}
    \SetHorizontalPole\imagecoffin{down}{3pt+\CoffinHeight\labelcoffin/2}
    \use:x{\JoinCoffins\imagecoffin[\l_miguel_label_pos_tl]\labelcoffin[vc,hc]} 
    \TypesetCoffin\imagecoffin
   }
   \group_end:
 }
\NewDocumentCommand{\setlabel}{m}
 {
  \keys_set:nn { miguel/label } { #1 }
 }

\cs_new_protected:Nn \miguel_includegraphics:nn
 {
  \includegraphics[#1]{#2}
 }
\cs_generate_variant:Nn \miguel_includegraphics:nn { V }

\ExplSyntaxOff

\begin{document}

\begin{table}[htp]

\centering

\xincludegraphics[scale=0.3,label=a)]{example-image-a}
\xincludegraphics[scale=0.3,label=b)]{example-image-b}

\xincludegraphics[scale=0.2,angle=90,label=c),pos=sw,labelbox,fontsize=\Large]{example-image-c}
\xincludegraphics[scale=0.2,angle=90,label=c),pos=w,labelbox,fontsize=\tiny]{example-image-c}

\setlabel{pos=ne,fontsize=\scriptsize,labelbox}
\xincludegraphics[scale=0.2,angle=90,label=x)]{example-image-c}
\xincludegraphics[scale=0.2,angle=90,label=y),labelbox=false]{example-image-c}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712
  • @Miguel That wasn't too hard. Thanks for patience. If you need them, we could add keys for moving around the label and also for the font size. – egreg Oct 12 '18 at 17:47
  • Wow! Amazing! I'm trying it in overleaf.com and it's not working there, but it works on my local machine (although I had to run an update because I was having a weird error with dates of packages). I'll reach out to overleaf ppl to see if they can fix it. I think regarding formatting the label, it is easy enough to pass things like \large and \textbf{} directly in the label. The positioning however would be great, something like NW, N, NE, E, SE, S, SW, W. Also, it would be great to be able to only turn the box around the label if needed, like labelbox=True – Miguel Oct 12 '18 at 20:58
  • @Miguel That's much more complicated. The issues with Overleaf are usual: they lag behind with their TeX distribution. – egreg Oct 12 '18 at 21:31
  • @Miguel New version with more keys is online. – egreg Oct 12 '18 at 23:06
  • 1
    This is really good! And it doesn't conflict with overleaf anymore (previously the problem was that they use TL2017 so it doesn't have '\box_use_drop'). Thank you so much! This should be a package!!! This is amazingly good for scientific publications! – Miguel Oct 15 '18 at 16:38
6

This is what you probably need: a combination of caption and subcaption:

\documentclass[a4paper]{article}

\usepackage{graphicx}
\usepackage{caption}
\usepackage[labelformat=simple]{subcaption}
\renewcommand\thesubfigure{\alph{subfigure})}

\begin{document}
\thispagestyle{empty}
\begin{figure}
\begin{subfigure}{0.5\textwidth}
\centering
\caption{}
\label{figurea}
\includegraphics[scale=0.5]{example-image-a}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\centering
\caption{}
\label{figureb}
\includegraphics[scale=0.5]{example-image-b}
\end{subfigure}
\end{figure}

See figure~\subref{figurea}.
See figure~\subref{figureb}.

\end{document}

enter image description here