程式碼

程式碼

我在原始碼中定義了一個新命令:

\newcommand{\IN}{interface}

列印出介面,但有時我想列印它介面(大寫 I)或介面相反,如果不定義另外 2 個新命令,我該如何做到這一點?

答案1

像這樣的東西嗎?

程式碼

\documentclass{article}
\newcommand*{\IN}[1][0]{%
    \ifnum#1=0 interface\fi
    \ifnum#1=1 Interface\fi
    \ifnum#1=2 interfaces\fi
}
\begin{document}
    \IN, \IN[1], \IN[2]
\end{document}

輸出

在此輸入影像描述

然而,正如 Egreg 所說,輸入“interface”很簡單。 :)

答案2

您也可以為大寫定義帶有星號的命令,並簡單地附加s複數:

\documentclass{article}
\makeatletter
\DeclareRobustCommand*{\IN}{%
    \@ifstar{Interface}{interface}}
\makeatother
\begin{document}
Singular: \IN, \IN*
\par Ways to write plurals: \IN s, \IN{s}, \IN{}s, \IN*s
\end{document}

\DeclareRobustCommand需要使命令不脆弱,因此您不需要\protect在標題等中使用它。

相關內容