在图上方添加“a)”标签

在图上方添加“a)”标签

我正在写一篇论文,惯例是将几个图放在一起,每个图的顶部都直接标有“a)”,“b)”等。

对我来说,完美的解决方案看起来像是 includegraphics 有一个可选参数来执行此操作,如下所示:

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

这是已经在包中实现的功能吗?如果没有,我该如何创建自己的“includegraphics”来实现这一点?

编辑:澄清一下,我的意思不是将标签放在图形上方,就像地平线上天空在海洋上方一样,而是将其放在图形上方,就像香蕉上的贴纸一样。图形的角落应该被“a)”标签覆盖。请参阅以下来自 springer.com 的示例

在此处输入图片描述

答案1

我避免过载\includegraphics并定义了一个新命令,当label在键中未指定时,其行为完全相同\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}

在此处输入图片描述

这是一个改进的版本,它还接受按键

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

的默认值fontsize\footnotesize;修改默认值时应该清楚要更改什么。如果说labelbox,标签将打印在白色背景上。这可以用 覆盖labelbox=false(如果\setlabel{labelbox}已使用,请参阅示例)。

还有一个\setlabel命令可用于设置一些键,并将值保持在当前范围内。

\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}

在此处输入图片描述

答案2

caption这可能是你所需要的:和的组合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}

在此处输入图片描述

相关内容