PGF 密钥 - 如何重复使用代码

PGF 密钥 - 如何重复使用代码

我喜欢定义一个自定义图形命令,并在标题中使用多个子标签和引用:

\documentclass[varwidth=100pt]{standalone}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{pgf}
\pgfkeys{/myfigure/.is family, /myfigure,
label a/.initial=a,
label a/.store in =\labelA,
label b/.initial=b,
label b/.store in =\labelB,
caption a/.initial={},
caption b/.initial={},
}

\pgfkeys{myfigure,#1}
\newcommand\myfigure[1]{
\begin{figure}
    {\phantomsubcaption\label{\labelA}
    \phantomsubcaption\label{\labelB}}
    \includegraphics[width=100pt]{example-image-a}
    \caption{
        (\subref{\labelA}) \pgfkeysvalueof{/myfigure/caption a} 
        (\subref{\labelB}) \pgfkeysvalueof{/myfigure/caption b} 
    }
\end{figure}
}

\begin{document}

\myfigure{
    label a = figa,
    caption a = caption 1,
    label b = figb,
    caption  b = caption 2,
}

\end{document}

结果应该看起来像

在此处输入图片描述

是否可以\myfigure{...}这样定义,不需要为每个子标签/子标题定义一个变量?例如:

\myfigure{
    label = figa,
    caption = caption 1,
    label = figb
    caption = caption 2,
}

这是我的想法的一次失败的尝试:

\documentclass[varwidth=100pt]{standalone}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{pgf}

\pgfkeys{/myfigure/.is family, /myfigure,
sublabels/.initial = {},
fullcaption/.initial = {},
label/.initial=a,
label/.store in=\mylabel,
label/.style={sublabels/.append={\phantomsubcaption\label{subfig:#1}}},
caption/.initial={},
caption/.style={fullcaption/.append={(\subref{subfig:\mylabel}) #1}},
}

\newcommand\myfigure[1]{
\pgfkeys{/myfigure,#1}
\begin{figure}
    \pgfkeys{/myfigure, sublabels}
    \includegraphics[width=100pt]{example-image-a}
    \pgfkeys{/myfigure, fullcaption}
\end{figure}
}

\begin{document}

\myfigure{
    label = figa,
    caption = caption 1,
    label = figb,
    caption = caption 2,
}

\ref{subfig:figa}

\end{document}

这里,我刚收到错误消息

! 未定义控制序列。sub@subfig:\mylabel

在此处输入图片描述

不知何故\mylabel内部没有扩大fullcaption/.style

答案1

基本思想是将标签/标题对存储为\mysubcap{label}{caption}\mysubcaps。处理示例中的 pgfkeys 后,\mysubcaps将包含

\mysubcap{figa}{caption 1}\mysubcap{figb}{caption 2}

现在,您可以通过\let连接\mysubcap到带有两个参数的命令然后执行来处理这些对\mysubcaps。通过\let连接\mysubcap到不同的命令,您可以以不同的方式处理这些对。

为了能够以形式指定对

\myfigure{subcaption={figa}{caption 1},subcaption={figb}{caption 2}}

我们将 pgf 密钥设置subcaption

\pgfkeys
  {/myfigure/subcaption/.code 2 args=
    {\expandafter\def\expandafter\mysubcaps\expandafter
       {\mysubcaps\mysubcap{#1}{#2}%
       }%
    }%
  }

为了处理选项,我们必须清除宏\mysubcaps,然后\pgfkeys使用选项进行调用。

\def\mysubcaps{}%
\pgfkeys{/myfigure/.cd,subcaption={figa}{caption 1},subcaption={figb}{caption 2}}%

下面是将片段集成到命令定义中的完整代码\myfigure。需要注意的是:在\caption命令中使用花哨的宏可能会导致各种奇怪的错误,因为会将\caption其参数以扩展形式写入aux文件。至少在测试阶段,我建议使用\caption[]{...}而不是\caption{...};然后将空的可选参数写入aux文件。

在此处输入图片描述

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{pgfkeys}
\pgfkeys
  {/myfigure/subcaption/.code 2 args=
    {\expandafter\def\expandafter\mysubcaps\expandafter
       {\mysubcaps\mysubcap{#1}{#2}%
       }%
    }%
  }
\newcommand\mysubcap{}% make sure \mysubcap is not in use
\newcommand\mysubcaps{}% make sure \mysubcaps is not in use
\newcommand\mysubcapphantom[2]{{\phantomsubcaption\label{#1}}}
\newcommand\mysubcapsubref[2]{ (\subref{#1}) #2}
\newcommand\myfigure[1]%
  {\def\mysubcaps{}% clear \mysubcaps
   \pgfkeys{/myfigure/.cd,#1}% store subcaption={X}{Y} as \mysubcap{X}{Y} in \mysubcaps
   \begin{figure}
   \centering
   \let\mysubcap\mysubcapphantom
   \mysubcaps
   \includegraphics[width=100pt]{example-image-a}%
   \let\mysubcap\mysubcapsubref
   \caption{\mysubcaps}%
   \end{figure}
  }
\begin{document}
\myfigure
  {subcaption={figa}{caption 1},
   subcaption={figb}{caption 2}
  }

\end{document}

相关内容