아래 첨자의 mhchem 매크로

아래 첨자의 mhchem 매크로

다음 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{...}.

관련 정보