如何正确地将从任意长的列表解析出来的参数元组传递给命令?

如何正确地将从任意长的列表解析出来的参数元组传递给命令?

下列的我之前的问题,我正在继续设计一个复杂的图像嵌入命令,但在管理每次迭代中需要传递给其他命令的联合参数组方面仍然存在问题。这是我正在设计的代码:

\documentclass[a4paper, 12pt, oneside]{paper}

% Preamble

% structure settings

\usepackage{graphicx}  
\usepackage{calc}      % programmer's tools of the trade
\usepackage{etoolbox}  % programmer's tools of the trade, II
\usepackage{ifthen}    % programmer's tools of the trade, III
\newcounter{cmdargs}   % Number of variable arguments for user defined commands
\newcounter{argnum}    % index of an argument in a list
\newlength{\picsheight}  % height of multipics picture table
\newlength{\picswidht}   % height of multipics picture table
\newlength{\captheight}  % height of multipict caption text
\newlength{\pboxheight}  % height of multipict box (text height)

\newcommand*{\xpargs}[1]{
  \stepcounter{argnum}
  \ifthenelse{\value{argnum}=1}
  {\gdef\picn@me{#1}}{\gdef\crop@lims{#1}}
}

\newcommand{\showpicname}{\picn@me}    % First parameter of the argument couple to be extracted
\newcommand{\showcroplims}{\crop@lims} % Second parameter of the argument couple to be extracted

\newcommand*{\addimage}[1]{% Image insertion via \multipic(t)s
  \setcounter{argnum}{0}
  \forcsvlist{\xpargs}{#1}
  \hspace*{\fill}\includegraphics[width=\linewidth-3em,keepaspectratio,\showcroplims,clip]{\showpicname}\hspace*{\fill}
  \\[2ex]
  \setcounter{argnum}{0}
}

\setlength{\fboxsep}{0pt}
\newcommand{\multipicts}[3]{% Multipicture environment
  \settototalheight{\captheight}{\parbox{.5\textwidth}{#2}} % Caption height
  \settototalheight{\picsheight}{\parbox{.5\textwidth{\forcsvlist{\addimage}{#3}}} % Picture column height
  \ifthenelse {\lengthtest{\captheight > \picsheight}} % Check who requires more height of text
  {\setlength{\pboxheight}{\captheight}}{\setlength{\pboxheight}{\picsheight}}  % then correspondingly set the picture box height
  \ifthenelse {\lengthtest{\pboxheight > \textheight}} {Picture box height is bigger than the maximum textblock height admitted in a single page: \the\pboxheight$>$\the\textheight}
  {\noindent
    \begin{minipage}[t]{\linewidth}
      \addcontentsline{lof}{figure}{#1}
      % \fbox{
      \parbox[t][\pboxheight][c]{0.5\linewidth}{\forcsvlist{\addimage}{#3}}%}
      \hfill
      % \fbox{
        \parbox[t][\pboxheight][c]{0.5\linewidth}{%
          #2\\
          \ifthenelse {\lengthtest{\pboxheight > \textheight}}
          {\the\pboxheight\ $>$ \the\textheight} {The picture box is \the\pboxheight, smaller than \the\textheight, which is the maximum allowable text height.}}%}
    \end{minipage}
  }
}

\begin{document}

\noindent
\multipicts{General power supply isolator}{General power supply isolator {\bf (A)}, which can be secured with a padlock.}{{2016-05-11_Valvola_intercettazione_lucchettata-low_dim.jpg,0.5cm 0.5cm 0.5cm 0.5cm},{2016-05-11_Valvola_intercettazione_lucchettata-low_dim.jpg,0.5cm 0.5cm 0.5cm 0.5cm},{2016-05-11_Valvola_intercettazione_lucchettata-low_dim.jpg,0.5cm 0.5cm 0.5cm 0.5cm},{2016-05-11_Valvola_intercettazione_lucchettata-low_dim.jpg,0.5cm 0.5cm 0.5cm 0.5cm}}

\end{document}

我使用的示例图像如下: 在此处输入图片描述

该命令的工作方式(或应执行的操作)的简要说明如下:

  • 该命令接受两个文本以及未指定数量的对参数作为输入参数。每个输入对的第一个参数是图片名称,而第二个参数是其裁剪限制。
  • 每对参数都被扩展为两个独立的参数pin@me,并由函数\crop@lims进行\xpargs处理:它们都\includegraphics在同一迭代期间传递给命令。
  • 完整的解析利用了\forcsvlist库函数。
  • \addimage命令首先在命令内部启动,\settototalheight以估计图片框的整个高度:将此高度与图片一侧应存在的文本高度进行比较。然后\pboxheight相应地定义参数,并minipage根据所选尺寸创建环境,并设置两个高度相同、宽度为一半的 parbox:一个 parbox 中填充图片,另一个 parbox 中填充文本。
  • 图形文件列表中添加了一个简短的标题.lof

该命令似乎有效:但是,虽然picn@ame正确传递给了 命令\includegraphics,但裁剪限制却没有。 参数 \crop@lims未被 确认keyval,这给出了以下错误消息(在文件中也发现.log):

ERROR: Package keyval Error: 0.5cm 0.5cm 0.5cm 0.5cm undefined.

--- TeX said ---

See the keyval package documentation for explanation.
Type  H <return>  for immediate help.
...                                              

l.66 ...tata-low_dim.jpg,0.5cm 0.5cm 0.5cm 0.5cm}}

--- HELP ---
No help available

总之,我的问题是:“在每次迭代过程中,如何最好地将从任意长的列表解析出的参数对(或元组)传递给另一个命令?”

答案1

您的问题是您传递的键值不正确。首先,选项是trim= 0.5cm 0.5cm 0.5cm 0.5cm而不是0.5cm 0.5cm 0.5cm 0.5cm。其次,您无法在 trim 键的命令中隐藏该值——解析器必须看到空格。因此,您应该在调用 includegraphics 之前将其展开。

\documentclass[]{article}
\usepackage{graphicx}
\usepackage{calc}
\begin{document}
\def\showcroplims{4cm 4cm 1cm 1cm}

\edef\next{\noexpand\includegraphics[width=\noexpand\linewidth-3em,keepaspectratio,trim=\showcroplims,clip]{example-image-A}}
\next

\end{document}

答案2

这不是一个新答案:我只是想分享我如何应用于我的问题Ulrike Fischer 女士的决议建议\addimage。按照她的建议,仅仅改变宏来\includegraphics在正确的时间扩展它及其所有参数是不够的:还\xpargs必须改变解析宏,使用它\xdef来定义并立即扩展参数\picname,并\croplims使其以扩展形式随时可用。下面报告了经过稍微清理的结果代码。

\documentclass[a4paper, 12pt, oneside]{paper}

% Preamble

% structure settings

\usepackage{graphicx}  
\usepackage{calc}      % programmer's tools of the trade
\usepackage{etoolbox}  % programmer's tools of the trade, II
\usepackage{ifthen}    % programmer's tools of the trade, III
\newcounter{argnum}    % index of an argument in a list
\newlength{\picsheight}  % height of multipics picture table
\newlength{\picswidht}   % height of multipics picture table
\newlength{\captheight}  % height of multipict caption text
\newlength{\pboxheight}  % height of multipict box (text height)

\newcommand*{\xpargs}[1]{
  \stepcounter{argnum}
  \ifthenelse{\value{argnum}=1}
  {\xdef\picname{#1}}%
  {\xdef\croplims{#1}}%
}

\newcommand*{\addimage}[1]{            % Image insertion via \multipic(t)s
  \setcounter{argnum}{0}
  \forcsvlist{\xpargs}{#1}
  \xdef\next{\noexpand\includegraphics[width=\linewidth-.5em,keepaspectratio,\croplims,clip]{\picname}}
  \next\hspace*{\fill}
  \\[.2ex]
}

\newcommand{\multipicts}[3]{     % Multipicture environment
  \setlength{\fboxsep}{0pt}
  \settototalheight{\captheight}{\parbox{.5\textwidth}{#2}}      % Caption height
  \settototalheight{\picsheight}{\parbox{.5\textwidth}{\forcsvlist{\addimage}{#3}}} % Picture column height
  \ifthenelse {\lengthtest{\captheight > \picsheight}}     % Check who requires more height of text
  {\setlength{\pboxheight}{\captheight}}{\setlength{\pboxheight}{\picsheight} % then correspondingly set the picture box height
  \ifthenelse {\lengthtest{\pboxheight > \textheight}} {Picture box height is bigger than the maximum textblock height admitted in a single page: \the\pboxheight$>$\the\textheight}
  {\noindent
    \begin{minipage}[t]{\linewidth}
      \addcontentsline{lof}{figure}{#1}
      \parbox[t][\pboxheight][c]{0.5\linewidth}{\forcsvlist{\addimage}{#3}}
      \hfill
      \parbox[t][\pboxheight][c]{0.5\linewidth}{%
        #2\\
        \ifthenelse {\lengthtest{\pboxheight > \textheight}}
        {\the\pboxheight\ $>$ \the\textheight} {The picture box is \the\pboxheight, smaller than \the\textheight, which is the maximum allowable text height.}}
    \end{minipage}
  }
}

\begin{document}
\noindent
\multipicts{General power supply isolator}{General power supply isolator {\bf (A)}, which can be secured with a padlock.}{{2016-05-11_Valvola_intercettazione_lucchettata-low_dim.jpg,trim=0.5cm 0.5cm 0.5cm 0.5cm},{2016-05-11_Valvola_intercettazione_lucchettata-low_dim.jpg,trim=0.5cm 0.5cm 0.5cm 0.5cm},{2016-05-11_Valvola_intercettazione_lucchettata-low_dim.jpg,trim=0.5cm 0.5cm 0.5cm 0.5cm},{2016-05-11_Valvola_intercettazione_lucchettata-low_dim.jpg,trim=0.5cm 0.5cm 0.5cm 0.5cm}}
\end{document}

相关内容