LaTeX3:将参数(标记、星号、修饰)转发给另一个命令的优雅方式

LaTeX3:将参数(标记、星号、修饰)转发给另一个命令的优雅方式

我很难优雅地定义一个 LaTeX3 命令并将其参数(特别是星号、标记、修饰)转发给另一个命令。我想我可以使用\expandonce或类似命令来使其工作,但我想有一个更好的解决方案。

梅威瑟:

\documentclass[]{article}

\NewDocumentCommand{\mycommand}{st't-t.e_O{}m}{%
  I am a command called \IfBooleanT{#1}{with a star, }\IfBooleanT{#2}{with a prime, }\IfBooleanT{#3}{with a dash, }\IfBooleanT{#4}{with a dot, }\IfNoValueTF{#5}{}{with an underscore (#5), } with the optional argument ``#6'' and with the mandatory argument ``#7''.
}

%% This fails:
% \NewDocumentCommand{\mynewcommand}{st't-t.e_O{}m}{%
%   \mycommand\IfBooleanT{#1}{*}\IfBooleanT{#2}{'}\IfBooleanT{#3}{.}\IfBooleanT{#4}{_{#4}}[foo#5]{#6}%
% }

%% This fails:
% \NewDocumentCommand{\mynewcommand}{st't-t.e_O{}m}{%
%   \mycommand#1#2#3#4[foo#5]{#6}%
% }

\begin{document}
\mycommand'_{below text}[I am optional]{yes}

How to define a new command so that mynewcommand behave like mycommand except that it prepends a string like "foo" in front of the optional argument?

% \mynewcommand*'-._{below text}[I am optional]{yes}


\end{document}

答案1

这就是你想要的吗?我想你肯定忽略了 中的参数#3(参数s-) \mynewcommand,所以我擅自添加了它。

\documentclass[]{article}

\NewDocumentCommand{\mycommand}{st't-t.e_O{}m}{%
  I am a command called \IfBooleanT{#1}{with a star, }\IfBooleanT{#2}{with a prime, }\IfBooleanT{#3}{with a dash, }\IfBooleanT{#4}{with a dot, }\IfValueT{#5}{with an underscore (#5), } with the optional argument ``#6'' and with the mandatory argument ``#7''.
}

% This works:
 \NewDocumentCommand{\mynewcommand}{st't-t.e_O{}m}{%
   \expandafter\mycommand\expanded{\IfBooleanT{#1}{*}\IfBooleanT{#2}{'}\IfBooleanT{#3}{-}\IfBooleanT{#4}{.}\IfValueT{#5}{\unexpanded{_{#5}}}\unexpanded{[foo#6]{#7}}}%
 }

%% This fails:
% \NewDocumentCommand{\mynewcommand}{st't-t.e_O{}m}{%
%   \mycommand#1#2#3#4[foo#5]{#6}%
% }

\begin{document}
\mycommand'_{below text}[I am optional]{yes}

How to define a new command so that mynewcommand behave like mycommand except that it prepends a string like "foo" in front of the optional argument?

\mynewcommand*'-._{below text}[I am optional]{yes}


\end{document}

在此处输入图片描述

答案2

别这样。这不是\NewDocumentCommand目的。

无论如何,首先修复:您的列表中的参数\mynewcommand已关闭。

\documentclass[]{article}

\NewDocumentCommand{\mycommand}{st't-t.e_O{}m}{%
  I am a command called
  \IfBooleanT{#1}{with a star, }%
  \IfBooleanT{#2}{with a prime, }%
  \IfBooleanT{#3}{with a dash, }%
  \IfBooleanT{#4}{with a dot, }%
  \IfValueT{#5}{with an underscore (#5), }
  with the optional argument ``#6'' and with the mandatory argument ``#7''.
}

\NewDocumentCommand{\mynewcommand}{st't-t.e{_}O{}m}{%
  \expanded{%
    \mycommand
    \IfBooleanT{#1}{*}%
    \IfBooleanT{#2}{'}%
    \IfBooleanT{#3}{-}%
    \IfBooleanT{#4}{.}
    \IfValueT{#5}{_{\unexpanded{#5}}}%
    [foo\unexpanded{#6}]%
    {\unexpanded{#7}}%
  }%
}


\begin{document}
\mycommand'_{below text}[I am optional]{yes}

How to define a new command so that mynewcommand behave like mycommand except that it prepends a string like "foo" in front of the optional argument?

\mynewcommand*'-._{below text}[I am optional]{yes}

\end{document}

在此处输入图片描述

相关内容