讓 \mycommand\mycommand 執行與 \mycommand 不同的操作兩次

讓 \mycommand\mycommand 執行與 \mycommand 不同的操作兩次

有沒有什麼方法可以定義一個命令\mycommand,使其在單獨使用時執行一件事,而在連續使用時執行另一件事?

我還想允許“\mycommand”可以有(可選)參數。

答案1

實現這一點的最簡單方法是\NewDocumentCommandt-argument 類型一起使用:

\documentclass{article}

\NewDocumentCommand \mycommand { t{\mycommand} O{} }
  {%
    \IfBooleanTF{#1}{Duplicated}{Single} variant.
    Optional argument: #2.%
  }

\begin{document}
\mycommand[abc]

\mycommand\mycommand[def]
\end{document}

在此輸入影像描述

相關內容