缺少$错误

缺少$错误

为什么这段代码不能编译?

% Oxidation and reduction
% Author: Arne Röhrs
% Note that two compilations are necessary. 
\documentclass{article}

\usepackage{tikz}
\usepackage[version=3,arrows=pgf]{mhchem}
\begin{document}

% Insert a named coordinate for later reference.
% Usage:
% \ncoord{name}
% \ncoord[1em,2em]{name}
\newcommand\ncoord[2][0,0]{%
    \tikz[remember picture,overlay]{\path (#1) coordinate (#2);}%

}

% Define a handful of special cases of \ncoord
\newcommand\ccoord[1]{\ncoord[0.5em,0.9em]{#1}}%
\newcommand\bcoord[1]{\ncoord[0.4em,-0.3em]{#1}}%

\newcommand\dcoord[1]{\ncoord[0.9em,1.5em]{#1}}%
\newcommand\ecoord[1]{\ncoord[0.9em,-0.3em]{#1}}%

\tikzset{
    oxidation/.style={thick,red!80!black},
    reduction/.style={thick,blue!80!black},
}

~

% First reaction
\[
\ce{{\ccoord{a}} Na + {\bcoord{c}} Cl -> {\ccoord{b}} Na+  + {\bcoord{d}} Cl-}

\]
\tikz[overlay,remember picture] {
    \draw[oxidation,->] (a) -- ++(0,0.7em) -| (b)
        node[above, near start] {Oxidation};
    \draw[reduction,->] (c) -- ++(0,-0.7em) -| (d)
        node[below, near start] {Reduktion};

}
\\[0.5cm] % add some extra vertical spacing

% Second reaction
\[
    \ce{%

    {\dcoord{a}} $\overset{\text{0}}{\text{2\,Na}}$
    +{\ecoord{c}}$\overset{\text{0}}{\text{Cl$_2$}}$ ->

    {\dcoord{b}} $\overset{\text{+I}}{\text{2\,Na$^+$}}$  +

    {\ecoord{d}} $\overset{\text{-I}}{\text{2\,Cl$^-$}}$}

\]
\tikz[overlay,remember picture] {
    \draw[oxidation,->] (a) -- ++(0,0.7em) -| (b)
        node[above, near start] {Oxidation};
    \draw[reduction,->] (c) -- ++(0,-0.7em) -| (d)
        node[below, near start] {Reduktion};

}



\end{document} 

答案1

定义中不要使用空行!

\newcommand\ncoord[2][0,0]{%
    \tikz[remember picture,overlay]{\path (#1) coordinate (#2);}}

或者至少

\newcommand\ncoord[2][0,0]{%
    \tikz[remember picture,overlay]{\path (#1) coordinate (#2);}%
}

相关内容