
次のMWEを考えてみましょう
\documentclass{article}
\usepackage[version=3]{mhchem}
\begin{document}
\begin{equation*}
% c_\ce{CO2} not working
c_{\ce{CO2}} \quad c_\text{working}
\end{equation*}
\end{document}
生産する
\ce{CO2}
さて、なぜマクロには括弧が必要ないのに、マクロに括弧を付けなければならないのでしょうか\text{}
?コメントされたテキストはエラーを生成しますmissing { inserted c_\ce{CO2}
答え1
何が書いてあるか見てみましょうamstext.sty
:
\DeclareRobustCommand{\text}{%
\ifmmode\expandafter\text@\else\expandafter\mbox\fi}
\def\text@#1{{\mathchoice
{\textdef@\displaystyle\f@size{#1}}%
{\textdef@\textstyle\f@size{\firstchoice@false #1}}%
{\textdef@\textstyle\sf@size{\firstchoice@false #1}}%
{\textdef@\textstyle \ssf@size{\firstchoice@false #1}}%
\check@mathfonts
}%
}
数式モードの場合、 が\text{xyz}
見つかると、TeX は「true」の分岐に従い、 が表示されます( は破棄される\text@{xyz}
ため) 。\else...\fi
\text@
次に、定義を代入します。
{\mathchoice{...}}
そして、これらの追加の中括弧は幸せを保ちます。数式モードでは、次のトークンの展開が発生する_
ことを思い出す必要があります。_
_\text{xyz}
追加の中括弧は、そうでなければ発生するであろう不可解なエラーを回避するためだけに導入されたのだと思います。
残念ながら、許可するそういった「間違った」入力です。これは_\mathrm{xyz}
、私が大まかに説明した で起こることとよく似ています。https://tex.stackexchange.com/a/160538/4427
一方、 の定義には\ce
このようなことは何もありません。
\newcommand*{\ce}{%
\ifx\protect\@typeset@protect
\csname ce \expandafter\endcsname
\else
\ifx\protect\@unexpandable@protect
\protect@unexpand@cmd@arg\ce
\else
\ifx\protect\string
\protect@string@cmd@arg\ce
\else
\expandafter\protect@unknown@cmd@arg
\csname ce \endcsname
\fi
\fi
\fi
}
これを見ると、TeX は間違いなく不満を抱くでしょう_\ce{...}
。