图表标题超出 TeX 容量

图表标题超出 TeX 容量

我得到了以下代码来为由任意数量的子图组成的图像定义宏:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{expl3,xparse}
\usepackage{subcaption}
\usepackage{graphicx}

\ExplSyntaxOn
\clist_new:N \l__gerry_mycl
\cs_new:Npn \__gerry_customfiggg:n #1#2#3#4 {
    \includegraphics[width=#1\textwidth]{#2}
% caption produces tex capacity exceeded.
    \caption[#3]{#4}
    \label{fig:#2}
}
\NewDocumentCommand{\multifig}{m}{
\begin{figure}
\clist_set:Nn \l__gerry_mycl {#1}
\clist_map_inline:Nn \l__gerry_mycl {
        \clist_set:Nn \l_tmpa_clist {##1}
        \begin{subfigure}{{\clist_item:Nn \l_tmpa_clist {1}}\textwidth}
        \exp_args:Nx \__gerry_customfiggg:n {0.95}{\clist_item:Nn \l_tmpa_clist {2}}{\clist_item:Nn \l_tmpa_clist {3}\clist_item:Nn \l_tmpa_clist {4}}
        \end{subfigure}
}
\end{figure}
}
\ExplSyntaxOff



\begin{document}

% size, file name, caption, caption
\multifig{{.2, fig_1, small caption 1, big caption 1}, {.1, fig_2, small caption 2, big caption 2}}

\end{document}

(来源:访问特定索引处的列表

该命令\caption[]{}产生以下错误:

TeX capacity exceeded, sorry [input stack size=5000].
\caption@ifstrut #1#2->#1

明确定义图形时不会出现问题。但我真的很想定义宏。有办法让它工作吗?

谢谢。

答案1

问题在于,{\clist_item:Nn \l_tmpa_clist {1}}\textwidth在我看来,它没有扩展到正确的维度(多个)。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xparse}
\usepackage{subcaption}
\usepackage{graphicx}

\ExplSyntaxOn
\clist_new:N \l__gerry_mycl


\cs_new:Nn \__gerry_customfiggg:nnnn {
  \includegraphics[width=#1\textwidth]{#2}
  % caption produces tex capacity exceeded.
  \caption[#3]{#4}
  \label{fig:#2}
}

\NewDocumentCommand{\multifig}{m}{
  \begin{figure}
    \clist_set:Nn \l__gerry_mycl {#1}
    \clist_map_inline:Nn \l__gerry_mycl {
      \clist_set:Nn \l_tmpa_clist {##1}
      \begin{subfigure}{\clist_item:Nn \l_tmpa_clist {1}\textwidth}
        \__gerry_customfiggg:nnnn {0.95}{\clist_item:Nn \l_tmpa_clist {2}}{\clist_item:Nn \l_tmpa_clist {3}}{\clist_item:Nn \l_tmpa_clist {4}}
      \end{subfigure}
    }
  \end{figure}
}
\ExplSyntaxOff



\begin{document}

% size, file name, caption, caption
\multifig{{.2, ente, small caption 1, big caption 1}, {.1, ente1, small caption 2, big caption 2}}

\end{document}

在此处输入图片描述

答案2

您的内部函数应该以不同的方式命名,因为它有四个参数。

此外,\exp_args:Nx它不会像您期望的那样工作,因为它只是在下一个标记后扩展第一个括号组的内容。实际上,这并不是真正需要的,但我只是将其作为示例。

你应该定义一个扩展的变体全部参数。顺便说一句,您不需要 clist 变量。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{expl3,xparse}
\usepackage{subcaption}
\usepackage{graphicx}

\ExplSyntaxOn
\cs_new:Npn \__gerry_customfiggg:nnnn #1#2#3#4
 {
    \includegraphics[width=#1\textwidth]{#2}
    \caption[#3]{#4}
    \label{fig:#2}
 }
\cs_generate_variant:Nn \__gerry_customfiggg:nnnn { xxxx }

\NewDocumentCommand{\multifig}{m}
 {
    \begin{figure}
    \clist_map_inline:nn { #1 }
     {
        \begin{subfigure}{\clist_item:nn {##1}{1}\textwidth}
        \__gerry_customfiggg:xxxx % \__gerry_customfiggg:nnnn would do as well
          {0.95}
          {\clist_item:nn {##1}{2}}
          {\clist_item:nn {##1}{3}}
          {\clist_item:nn {##1}{4}}
        \end{subfigure}
}
\end{figure}
}
\ExplSyntaxOff



\begin{document}

% size, file name, caption, caption
\multifig{
  {.2, example-image-a, small caption 1, big caption 1},
  {.1, example-image-b, small caption 2, big caption 2}
}

\end{document}

在此处输入图片描述

另一方面,我赞成一种完全不同且更加灵活的方法:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{expl3,xparse}
\usepackage{subcaption}
\usepackage{graphicx}

\ExplSyntaxOn
\keys_define:nn { gerry/multifig }
 {
  align .tl_set:N        = \l_gerry_multifig_align_tl,
  scale .tl_set:N        = \l_gerry_multifig_scale_tl,
  innerscale .tl_set:N   = \l_gerry_multifig_innerscale_tl,
  caption .tl_set:N      = \l_gerry_multifig_caption_tl,
  shortcaption .tl_set:N = \l_gerry_multifig_shortcaption_tl,
  label .tl_set:N        = \l_gerry_multifig_label_tl,
  align .initial:n       = c,
  scale .initial:n       = 1,
  innerscale .initial:n  = 0.95,
 }
\NewDocumentEnvironment{multifig}{O{htp}}
 {
  \begin{figure}[#1]
 }
 {
  \end{figure}
 }

\NewDocumentCommand{\shortfig}{mm}
 {
  \group_begin:
  \keys_set:nn { gerry/multifig } { #2 }
  \begin{subfigure}[\l_gerry_multifig_align_tl]{\l_gerry_multifig_scale_tl\textwidth}
  \centering
  \includegraphics[width=\l_gerry_multifig_innerscale_tl\textwidth]{#1}
  \tl_if_empty:NTF \l_gerry_multifig_shortcaption_tl
   {
    \caption{\l_gerry_multifig_caption_tl}
   }
   {
    \caption[\l_gerry_multifig_shortcaption_tl]{\l_gerry_multifig_caption_tl}
   }
  \tl_if_empty:NF \l_gerry_multifig_label_tl { \label{\l_gerry_multifig_label_tl} }
  \end{subfigure}
  \group_end:
 }
\ExplSyntaxOff



\begin{document}

\ref{examplelabel}

\begin{multifig}
\shortfig{example-image-a}{
  scale=.2,
  shortcaption=small caption 1,
  caption=big caption 1,
  label=examplelabel,
}\qquad
\shortfig{example-image-b}{
  align=t,
  scale=.2,
  innerscale=.5,
  caption=caption 2,
 }
\end{multifig}

\begin{multifig}
\shortfig{example-image-a}{
  align=t,
  scale=.2,
  shortcaption=small caption 1,
  caption=big caption 1,
}\qquad
\shortfig{example-image-b}{
  align=t,
  scale=.2,
  innerscale=.5,
  caption=caption 2,
 }
\end{multifig}

\end{document}

在此处输入图片描述

相关内容