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

這是你想要的嗎?我想你肯定忽略了argument #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}

在此輸入影像描述

相關內容