用星號定義指令

用星號定義指令

我有一個帶有多個參數的命令,其中一個是星號:

\NewDocumentCommand{\foo}{ s m m }{
    \IfBooleanTF{#1}{Asterisk is here.}{Asterisk is not here.} #2 #3.
}

我想定義另一個命令,其工作方式與第一個命令類似,但帶有一些預先定義的參數,例如

\NewDocumentCommand{\baz}{ s m }{
    \IfBooleanTF{#1}{
        \foo*{#2}{a}
    }{
        \foo{#2}{a}
    }
}

\baz並且\baz*以同樣的方式工作,所以我想以某種方式定義\baz而不重複該{#2}{a}部分兩次。我努力了

\NewDocumentCommand{\baz}{ s m }{
    \expandafter\foo\IfBooleanT{#1}{*}{#2}{a}
}

但它不起作用。完整的MWE

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\foo}{ s m m }{
    \IfBooleanTF{#1}{Asterisk is here.}{Asterisk is not here.} #2 #3.
}

\NewDocumentCommand{\baz}{ s m }{
    \IfBooleanTF{#1}{
        \foo*{#2}{a}
    }{
        \foo{#2}{a}
    }
}

\begin{document}

    \baz{b}

    \baz*{c}

\end{document}

答案1

由於 TeX 透過巨集擴展工作,因此不需要全部\foo條件中的參數,所以我們可以只得到唯一的部分:

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\foo}{ s m m }{%
    \IfBooleanTF{#1}{Asterisk is here.}{Asterisk is not here.} #2 #3.%
}

\NewDocumentCommand{\baz}{ s m }{%
    \IfBooleanTF{#1}%
      {\foo*}%
      {\foo}%
        {#2}{a}%
}

\begin{document}

    \baz{b}

    \baz*{c}

\end{document}

相關內容