在公式中使用自己的宏时“缺少 { 插入”

在公式中使用自己的宏时“缺少 { 插入”

当使用以下代码时,我收到一些错误,但输出正是我想要的,所以我不确定为什么会出现错误:

\newcommand{\desc}[2]{\underbrace{#1}_\substack{#2}}
${\desc{R-H}{Alkan}} + {\desc{X_2}{Halogen}} \xrightarrow{H_\nu + UV-Bestrahlung} {\desc{R-X}{Halogenalkan}} + {\desc{H-X}{Halogenwasserstoff}}$

这就是它看起来的样子以及它应该是的样子:

输出

我收到的错误:

! Missing { inserted.
<to be read again> 
                   \vcenter 
l.250 ${{\desc{R-H}{Alkan}} + {\desc{X_2}{Halogen}
                                                  } \xrightarrow{H_\nu + UV-...
A left brace was mandatory here, so I've put one in.
You might want to delete and/or insert some corrections
so that I will find a matching right brace soon.
(If you're confused by all this, try typing `I}' now.)

! Missing { inserted.
<to be read again> 
                   \vcenter 
l.250 ...UV-Bestrahlung} {\desc{R-X}{Halogenalkan}
                                                  } + {\desc{H-X}{Halogenwas...
A left brace was mandatory here, so I've put one in.
You might want to delete and/or insert some corrections
so that I will find a matching right brace soon.
(If you're confused by all this, try typing `I}' now.)

! Missing { inserted.
<to be read again> 
                   \vcenter 
l.250 ...alkan}} + {\desc{H-X}{Halogenwasserstoff}
                                                  }}$
A left brace was mandatory here, so I've put one in.
You might want to delete and/or insert some corrections
so that I will find a matching right brace soon.
(If you're confused by all this, try typing `I}' now.)

! Missing } inserted.
<inserted text> 
                }
l.250 ...an}} + {\desc{H-X}{Halogenwasserstoff}}}$

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.

答案1

\substack是一个可扩展的标记,因此您需要将其括在括号中以使其成为下标。另外,我认为您不需要通过将反应的每个项括在括号中来使其成为子公式。 编辑:我忽略了这个\text问题,但是 Torbjørn T. 引起了我的注意。 第二次编辑:看来今天我比平时更心不在焉:也感谢@lblb,指出还有另一段文字应该用\text!换行。

因此我提出以下修复方案:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}

\newcommand{\desc}[2]{\underbrace{#1}_{\text{#2}}}



\begin{document}

Abc $\desc{R-H}{Alkan} + \desc{X_2}{Halogen}
\xrightarrow{H_\nu + \text{UV-Bestrahlung}}
\desc{R-X}{Halogenalkan} + \desc{H-X}{Halogenwasserstoff}$ def.

\end{document}

相关内容