如何创建缺少的默认命令

如何创建缺少的默认命令

我想创建一个命令,其输入为 $X,t$,输出为 伊[X_t]如果给出并输出 前任] 如果不见了。


迄今为止

如果我没有条件然后

\newcommand{\e}[1]{E[#1]}

有效,但除此之外......我很困惑。

答案1

要允许该语法,您可以使用\@ifnextchar来检查第一个参数后是否有逗号。然而,这是一种可疑的语法,可能会导致不可预测的行为。无论哪种方式,您都不应使用单字母命令名称:

\documentclass{article}

% Option 1:
\makeatletter
\newcommand{\CmdE}[1]{%
  E[#1%
  \@ifnextchar,{\grab@sub}]}
\def\grab@sub,#1{%
  _{#1}]}
\makeatother

\begin{document}
\pagenumbering{gobble}

\begin{tabular}{ll}
\(\CmdE X\)      & \(\CmdE {XX}\)      \\
\(\CmdE X,t\)    & \(\CmdE {XX},t\)    \\
\(\CmdE X,{tt}\) & \(\CmdE {XX},{tt}\) \\
\end{tabular}

\end{document}

生成:

在此处输入图片描述

我建议使用一种更清晰(且更强大)的语法xparse

\documentclass{article}

% Option 2:
\usepackage{xparse}
\NewDocumentCommand\CmdE{mo}
  {%
    E[#1\IfValueT{#2}{_{#2}}]%
  }

\begin{document}

\begin{tabular}{ll}
\(\CmdE {X}\)     & \(\CmdE {XX}\)     \\
\(\CmdE {X}[t]\)  & \(\CmdE {XX}[t]\)  \\
\(\CmdE {X}[tt]\) & \(\CmdE {XX}[tt]\) \\
\end{tabular}

\end{document}

两种方法的结果相同。

哦,是的,还有简单的 LaTeX 语法:

\documentclass{article}

% Option 3:
\newcommand{\CmdE}[2][]
  {%
    E[#2%
    \if\relax\detokenize{#1}\relax
    \else
      _{#1}%
    \fi]%
  }

\begin{document}

\begin{tabular}{ll}
\(\CmdE {X}\)     & \(\CmdE {XX}\)     \\
\(\CmdE [t]{X}\)  & \(\CmdE [t]{XX}\)  \\
\(\CmdE [tt]{X}\) & \(\CmdE [tt]{XX}\) \\
\end{tabular}

\end{document}

答案2

通过下面的示例,\CmdE采用一个未分隔的参数并检查该参数的标记之一是否是逗号。

仅在数学模式下使用\CmdE,因为它对逗号周围的空格标记及其参数的其他组成部分不起作用。

\documentclass{article}

\makeatletter
\newcommand\CheckWhetherArgBlank[1]{%
  \if\relax\detokenize\expandafter{\@firstoftwo#1{}.}\relax
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
\newcommand\gobbletocomma{}\long\def\gobbletocomma#1,{}%
\newcommand\removecomma{}\long\def\removecomma#1,{#1}%
\newcommand\gobbledot{}\def\gobbledot.{}%
\newcommand\firsttoSelDoM{}\long\def\firsttoSelDoM#1,#2\SelDoM{{#1},}%
\newcommand\keeptillcomma[1]{%
   \expandafter\CheckWhetherArgBlank\expandafter{\gobbletocomma#1}{%
     \expandafter\gobbledot\removecomma#1%
   }{%
    \expandafter\keeptillcomma\expandafter{\firsttoSelDoM#1}%
   }%
}%
\newcommand\CmdE[1]{%
  \expandafter\CheckWhetherArgBlank\expandafter{\gobbletocomma#1,}%
  {E[#1]}% no comma
  {\expandafter\CheckWhetherArgBlank\expandafter{\gobbletocomma#1}%
    {\removecomma E[#1]}% comma but only blankness behind it.
    {E[{\keeptillcomma{.#1\SelDoM}}_{\gobbletocomma#1}]}% comma and something other than blankness behind it
  }%
}%
% Blankness = Either only explicit space tokens or no tokens at all.
\makeatother

\begin{document}

\begin{verbatim}
\begin{tabular}{ll}
\(\CmdE{X}\)          & \(\CmdE{{XX}}\)          \\
\(\CmdE{X,t}\)        & \(\CmdE{{XX},t}\)        \\
\(\CmdE{X, t}\)       & \(\CmdE{{XX}, t}\)       \\
\(\CmdE{X,{tt}}\)     & \(\CmdE{{XX},{tt}}\)     \\
\(\CmdE{X, tt}\)      & \(\CmdE{XX, tt}\)        \\
\(\CmdE{[XX], [tt]}\) & \(\CmdE{[XX], [tt],u}\)  \\
\(\CmdE{X,}\)         & \(\CmdE{{[XX], [tt]},u}\)\\
\end{tabular}
\end{verbatim}

\begin{tabular}{ll}
\(\CmdE{X}\)          & \(\CmdE{{XX}}\)          \\
\(\CmdE{X,t}\)        & \(\CmdE{{XX},t}\)        \\
\(\CmdE{X, t}\)       & \(\CmdE{{XX}, t}\)       \\
\(\CmdE{X,{tt}}\)     & \(\CmdE{{XX},{tt}}\)     \\
\(\CmdE{X, tt}\)      & \(\CmdE{XX, tt}\)        \\
\(\CmdE{[XX], [tt]}\) & \(\CmdE{[XX], [tt],u}\)  \\
\(\CmdE{X,}\)         & \(\CmdE{{[XX], [tt]},u}\)\\
\end{tabular}

\end{document}

在此处输入图片描述

答案3

不同的expl3实现:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\E}{m}
 {
  \clist_set:Nn \l_aimblb_e_clist { #1 }
  \clist_pop:NN \l_aimblb_e_clist \l_aimblb_e_main_tl
  E[ \l_aimblb_e_main_tl % print the main
     \clist_if_empty:NF \l_aimblb_e_clist
      {
       \sb{ \clist_use:Nn \l_aimblb_e_clist { , } }
      }
  ]
 }
\clist_new:N \l_aimblb_e_clist
\tl_new:N \l_aimblb_e_main_tl
\ExplSyntaxOff

\begin{document}

Simple: $\E{X}$

With subscript: $\E{X,t}$

More subscripts: $\E{X,t,u}$

\end{document}

这支持更多下标;因为它是免费的,所以我添加了它们。您可能对它们有不同的用法。

在此处输入图片描述

经典的实现:

\documentclass{article}

\makeatletter
\newcommand{\E}[1]{\aimblb@E#1,,\@nil}
\def\aimblb@E#1,#2,#3\@nil{%
  E[#1\if\relax\detokenize{#2}\relax\else_{#2}\fi]
}

\begin{document}

$\E{X}$

$\E{X,t}$

$\E{X,abcdef}$

Comparing with Ulrich's:

\begin{verbatim}
\begin{tabular}{ll}
\(\E{X}\)          & \(\E{{XX}}\)          \\
\(\E{X,t}\)        & \(\E{{XX},t}\)        \\
\(\E{X, t}\)       & \(\E{{XX}, t}\)       \\
\(\E{X,{tt}}\)     & \(\E{{XX},{tt}}\)     \\
\(\E{X, tt}\)      & \(\E{XX, tt}\)        \\
\(\E{[XX], [tt]}\) & \(\E{[XX], [tt],u}\)  \\
\(\E{X,}\)         & \(\E{{[XX], [tt]},u}\)\\
\end{tabular}
\end{verbatim}

\begin{tabular}{ll}
\(\E{X}\)          & \(\E{{XX}}\)          \\
\(\E{X,t}\)        & \(\E{{XX},t}\)        \\
\(\E{X, t}\)       & \(\E{{XX}, t}\)       \\
\(\E{X,{tt}}\)     & \(\E{{XX},{tt}}\)     \\
\(\E{X, tt}\)      & \(\E{XX, tt}\)        \\
\(\E{[XX], [tt]}\) & \(\E{[XX], [tt],u}\)  \\
\(\E{X,}\)         & \(\E{{[XX], [tt]},u}\)\\
\end{tabular}

\end{document}

在此处输入图片描述

相关内容