%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ------------------------------------------------------------------------------------- %
% - chemmacros - chemmacros.sty ------------------------------------------------------- %
% - a collection of macros to make typesetting chemistry documents more convenient ---- %
% ------------------------------------------------------------------------------------- %
% - Clemens Niederberger -------------------------------------------------------------- %
% - 2011/06/22 ------------------------------------------------------------------------ %
% ------------------------------------------------------------------------------------- %
% - http://www.mychemistry.eu/ -------------------------------------------------------- %
% - contact@mychemistry.eu ------------------------------------------------------------ %
% ------------------------------------------------------------------------------------- %
% - If you have any ideas, questions, suggestions or bugs to report, please feel free - %
% - to contact me. -------------------------------------------------------------------- %
% ------------------------------------------------------------------------------------- %
% - Copyright 2011 Clemens Niederberger                                               - %
% -                                                                                   - %
% - This work may be distributed and/or modified under the                            - %
% - conditions of the LaTeX Project Public License, either version 1.3                - %
% - of this license or (at your option) any later version.                            - %
% - The latest version of this license is in                                          - %
% -   http://www.latex-project.org/lppl.txt                                           - %
% - and version 1.3 or later is part of all distributions of LaTeX                    - %
% - version 2005/12/01 or later.                                                      - %
% -                                                                                   - %
% - This work has the LPPL maintenance status `maintained'.                           - %
% -                                                                                   - %
% - The Current Maintainer of this work is Clemens Niederberger.                      - %
% -                                                                                   - %
% - This work consists of the files chemmacros.sty, chemmacros_doc_de.tex,            - %
% - chemmacros_doc_de.tex, README                                                     - %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand*\CM@version{1.1}%
\newcommand*\CM@date{2011/06/22}%
\newcommand*\CM@name{chemmacros}%
\NeedsTeXFormat{LaTeX2e}[2009/09/24]%
\ProvidesPackage{chemmacros}[2011/06/22 v1.1 chemistry macros]%
\RequirePackage{amsmath,tikz,xparse,ifthen,siunitx}%
\usetikzlibrary{calc}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - ifs ------------------------------------------------------------------------------- %
\newif\if@CM@circled\@CM@circledfalse%
\newif\if@CM@german\@CM@germanfalse%
\newif\if@CM@xspace\@CM@xspacefalse%
\newif\if@CM@mhchemloaded\@CM@mhchemloadedfalse%
\newif\if@CM@chemstyleloaded\@CM@chemstyleloadedfalse%
\newif\if@CM@bpchemloaded\@CM@bpchemloadedfalse%
\newif\if@CM@bpchem\@CM@bpchemfalse%
\newif\if@CM@mychemistryloaded\@CM@mychemistryloadedfalse%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - package options ------------------------------------------------------------------- %
% circled:
\DeclareOption{circled}{%
  \@CM@circledtrue%
}

% xspace:
\newcommand*\CM@xspace{}
\DeclareOption{xspace}{%
  \@CM@xspacetrue%
}

% german:
\DeclareOption{german}{%
  \@CM@germantrue%
}

% bpchem:
\DeclareOption{bpchem}{
  \@CM@bpchemtrue
}

% unknown options:
\DeclareOption*{%
  \PackageWarning{chemmacros}{Unknown option `\CurrentOption'}%
}
\ProcessOptions\relax

\if@CM@bpchem
  \RequirePackage{bpchem}
\fi

\if@CM@xspace
  \RequirePackage{xspace}%
  \let\CM@xspace\xspace%
\fi

\AtBeginDocument{%
  \@ifpackageloaded{mhchem}{\@CM@mhchemloadedtrue}{\@CM@mhchemloadedfalse}
  \@ifpackageloaded{chemstyle}{\@CM@chemstyleloadedtrue}{\@CM@chemstyleloadedfalse}
  \@ifpackageloaded{bpchem}{\@CM@bpchemloadedtrue}{\@CM@bpchemloadedfalse}
  \@ifpackageloaded{mychemistry}{\@CM@mychemistryloadedtrue}{\@CM@mychemistryloadedfalse}

  \if@CM@mhchemloaded
    \newcommand*\CM@atom[1]{\cf{#1}}%
  \else
    \newcommand*\CM@atom[1]{\mbox{#1}}%
  \fi

  \if@CM@circled
    \let\CM@plus\oplus
    \let\CM@minus\ominus
  \else
    \newcommand*\CM@plus{+}%
    \newcommand*\CM@minus{-}%
  \fi
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - tools ----------------------------------------------------------------------------- %
% Test if #2 is part of #1
% (the macro is a copy of chemfig's \CF@if@instr by Christian Tellechea)
\newcommand*\CM@ifinstr[2]{%
  \def\CM@ifinstr@i##1#2##2\@nil{\ifx\@empty##2\@empty\expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi}%
  \CM@ifinstr@i#1#2\@nil%
}

% split at comma, save part before in #3, save part after in \CM@RemainStr
% (the macro is a copy of chemfig's \CF@analyze@movearg by Christian Tellechea)
\def\CM@split#1,#2\@nil#3{%
  \def#3{#1}\def\CM@RemainStr{#2}%
}

% Optional Argument Analysis ---------------------------------------------------------- %
\newcounter{CM@tmp@cnt}
\setcounter{CM@tmp@cnt}{0}
\newcounter{CM@splitting@cnt}
% empty \CM@arg@<num>
\newcommand*\CM@ClearArgs[1]{%
  \whiledo{\value{CM@tmp@cnt}<#1}{%
    \stepcounter{CM@tmp@cnt}%
    \expandafter\let\csname CM@arg@\roman{CM@tmp@cnt}\endcsname\@empty%
  }%
  \setcounter{CM@tmp@cnt}{0}%
}
% split a list separated by commas into \CM@arg@<num>, number of arguments chosen by #2
\newcommand*\CM@splitting[2]{%
  \CM@ClearArgs{#2}%
  \setcounter{CM@splitting@cnt}{#2-1}%
  \def\CM@RemainStr{#1}%
  \whiledo{\value{CM@tmp@cnt}<\value{CM@splitting@cnt}}{%
    \expandafter\CM@ifinstr\expandafter{\CM@RemainStr}{,}{%
      \stepcounter{CM@tmp@cnt}%
      \expandafter\CM@split\CM@RemainStr\@nil\CM@tmp@name%
      \expandafter\let\csname CM@arg@\roman{CM@tmp@cnt}\endcsname\CM@tmp@name%
    }{%
      \stepcounter{CM@tmp@cnt}%
      \expandafter\let\csname CM@arg@\roman{CM@tmp@cnt}\endcsname\CM@RemainStr%
      \let\CM@RemainStr\@empty%
      \setcounter{CM@tmp@cnt}{\value{CM@splitting@cnt}}%
    }%
  }%
  \stepcounter{CM@tmp@cnt}%
  \expandafter\let\csname CM@arg@\roman{CM@tmp@cnt}\endcsname\CM@RemainStr%
  \setcounter{CM@tmp@cnt}{0}%
}

% check commands for equality --------------------------------------------------------- %
\newcommand*\CM@compareCs[2]{%
  \expandafter\let\expandafter\CM@cs@one\csname#1\endcsname%
  \expandafter\let\expandafter\CM@cs@two\csname#2\endcsname%
  \ifx\CM@cs@one\CM@cs@two\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}

% check strings for equality ---------------------------------------------------------- %
\newcommand*\CM@compareStr[2]{%
  \edef\CM@cs@one{#1}%
  \edef\CM@cs@two{#2}%
  \ifx\CM@cs@one\CM@cs@two\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - particles, charges ---------------------------------------------------------------- %
% electron
\newcommand*\el{\ensuremath{e^\CM@minus}\CM@xspace}
% proton
\newcommand*\prt{\ensuremath{p^\CM@plus}\CM@xspace}
% charges
\newcommand*\mch[1][]{\ensuremath{{}^{#1\CM@minus}}}
\newcommand*\pch[1][]{\ensuremath{{}^{#1\CM@plus}}}
\newcommand*\delm{\mbox{\tiny$\delta\CM@minus$}\CM@xspace}
\newcommand*\delp{\mbox{\tiny$\delta\CM@plus$}\CM@xspace}
\newcommand*\scrm{\ensuremath{\scriptstyle\CM@minus}}
\newcommand*\scrp{\ensuremath{\scriptstyle\CM@plus}}
% neutron
\newcommand*\ntr{\ensuremath{n^0}\CM@xspace}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - stereo descriptors and sim. ------------------------------------------------------- %
% Cahn-Ingold-Prelog
\newcommand*\Rcip{\textit{(R)}}
\newcommand*\Scip{\textit{(S)}}
\newcommand*\cip[1]{\textit{(#1)}}
\newcommand*\Sconf[1][S]{\tikz[baseline=(a.base),text height=1.5ex,text depth=.25ex]{\draw  node(a) {#1};\draw[->,thick,rotate=90] ($(a.center)+(20:.8em)$) arc (20:340:.8em);}}
\newcommand*\Rconf[1][R]{\tikz[baseline=(a.base),text height=1.5ex,text depth=.25ex]{\draw  node(a) {#1};\draw[<-,thick,rotate=90] ($(a.center)+(20:.8em)$) arc (20:340:.8em);}}
% E(ntgegen)/Z(usammen)
\newcommand*\E{\textit{(E)}}
\newcommand*\Z{\textit{(Z)}}
\AtBeginDocument{\if@CM@bpchemloaded
  \renewcommand*\cis{\textit{cis}\CM@xspace}
  \renewcommand*\trans{\textit{trans}\CM@xspace}
 \else
  \newcommand*\cis{\textit{cis}\CM@xspace}
  \newcommand*\trans{\textit{trans}\CM@xspace}
\fi}
\newcommand*\tert{\textit{tert}\CM@xspace}
% Fischer
\newcommand*\Dfi{\textsc{d}}
\newcommand*\Lfi{\textsc{l}}
% ortho/meta/para
\newcommand*\ortho{\textsl{o}}
\newcommand*\meta{\textsl{m}}
\newcommand*\para{\textsl{p}}

% latin phrases
\AtBeginDocument{\if@CM@chemstyleloaded
  \newcommand*\insitu{\latin{in situ}\CM@xspace}
  \newcommand*\abinitio{\latin{ab initio}\CM@xspace}
\else
  \newcommand*\insitu{\textit{in situ}\CM@xspace}
  \newcommand*\abinitio{\textit{ab initio}\CM@xspace}
\fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - ions, molecules ------------------------------------------------------------------- %
% Proton, Hydroxide, Hydronium/Oxonium, water, nucleophile, electrophile
\newcommand*\Hpl{\CM@atom{H\pch}\CM@xspace}
\newcommand*\Hyd{\CM@atom{OH\mch}\CM@xspace}
\newcommand*\HtO{\CM@atom{H$_3$O\pch}\CM@xspace}
\newcommand*\water{\CM@atom{H$_2$O}\CM@xspace}
\newcommand*\Nu{\CM@atom{Nu\mch}\CM@xspace}
\newcommand*\El{\CM@atom{E\pch}\CM@xspace}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - acid/base ------------------------------------------------------------------------- %
\newcommand*\pH{\mbox{\textsl{p}H}\CM@xspace}
\newcommand*\pOH{\mbox{\textsl{p}OH}\CM@xspace}
\newcommand*\pKa[1][]{\mbox{\textsl{p}$K_\mathrm{\if@CM@german S\else A\fi#1}$}\CM@xspace}
\newcommand*\pKb[1][]{\mbox{\textsl{p}$K_\mathrm{B#1}$}\CM@xspace}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - substituents ---------------------------------------------------------------------- %
% \R[<sign>]{<subscript>}
\newcommand*\R[2][]{\CM@atom{R$_{\mathrm{#2}}^{\ifx#1+\CM@plus\else\ifx#1-\CM@minus\fi\fi}$}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - units ----------------------------------------------------------------------------- %
\DeclareSIUnit{\atm}{atm}
\DeclareSIUnit{\atmosphere}{atm}
\DeclareSIUnit{\calory}{cal}
\DeclareSIUnit{\cal}{cal}
\AtBeginDocument{\if@CM@chemstyleloaded\else
  \DeclareSIUnit{\cmc}{\cubic\centi\metre}
  \DeclareSIUnit{\molar}{\mole\per\cubic\deci\metre}
  \DeclareSIUnit{\Molar}{\textsc{m}}
\fi}
\DeclareSIUnit{\moLar}{\mole\per\liter}
\DeclareSIUnit{\MolMass}{\gram\per\mole}
\DeclareSIUnit{\normal}{\textsc{n}}
\DeclareSIUnit{\torr}{torr}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - reaction mechanisms --------------------------------------------------------------- %
% \mech[<type>]
% <type> - substitutions: {}, 1, 2, se, 1e, 2e, ar
%        - eliminations:  e, e1, e2, cb
\newcommand*\CM@Substitution[1][]{%
  \CM@compareStr{#1}{se}{\def\CM@subst@mol{_\mathrm{E}}\let\CM@subst@ar\@empty}{%
    \CM@compareStr{#1}{1e}{\def\CM@subst@mol{_\mathrm{E}1}\let\CM@subst@ar\@empty}{%
      \CM@compareStr{#1}{2e}{\def\CM@subst@mol{_\mathrm{E}2}\let\CM@subst@ar\@empty}{%
        \CM@compareStr{#1}{ar}{\def\CM@subst@mol{_\mathrm{E}}\def\CM@subst@ar{Ar-}}{%
          \def\CM@subst@mol{_\mathrm{N}#1}\let\CM@subst@ar\@empty}}}}%
  \CM@atom{\mbox{\CM@subst@ar S}$\CM@subst@mol$}\CM@xspace%
}
\newcommand*\CM@Elimination[1][]{%
  \CM@compareStr{#1}{cb}{\def\CM@elim@mol{1_\mathrm{cb}}}{\def\CM@elim@mol{#1}}%
  \CM@atom{E$\CM@elim@mol$}\CM@xspace%
}
\newcommand*\mech[1][]{%
  \CM@compareStr{#1}{se}{\def\CM@mech@type{S}\def\CM@mech@mol{_\mathrm{E}}\let\CM@mech@ar\@empty}{%
    \CM@compareStr{#1}{1e}{\def\CM@mech@type{S}\def\CM@mech@mol{_\mathrm{E}1}\let\CM@mech@ar\@empty}{%
      \CM@compareStr{#1}{2e}{\def\CM@mech@type{S}\def\CM@mech@mol{_\mathrm{E}2}\let\CM@mech@ar\@empty}{%
        \CM@compareStr{#1}{ar}{\def\CM@mech@type{S}\def\CM@mech@mol{_\mathrm{E}}\def\CM@mech@ar{Ar-}}{%
          \CM@compareStr{#1}{e}{\def\CM@mech@type{E}\def\CM@mech@mol{}\let\CM@mech@ar\@empty}{%
            \CM@compareStr{#1}{e1}{\def\CM@mech@type{E}\def\CM@mech@mol{1}\let\CM@mech@ar\@empty}{%
              \CM@compareStr{#1}{e2}{\def\CM@mech@type{E}\def\CM@mech@mol{2}\let\CM@mech@ar\@empty}{%
                \CM@compareStr{#1}{cb}{\def\CM@mech@type{E}\def\CM@mech@mol{1_\mathrm{cb}}\let\CM@mech@ar\@empty}{%
                  \def\CM@mech@type{S}\def\CM@mech@mol{_\mathrm{N}#1}\let\CM@mech@ar\@empty}}}}}}}}%
  \CM@atom{\mbox{\CM@mech@ar\CM@mech@type}$\CM@mech@mol$}\CM@xspace%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - oxidation numbers ----------------------------------------------------------------- %
% \ox{<number>,<atom>}
\newcommand*\CM@ox@sign[1]{%
 \ifx -#1{$-$}% TODO: bad method here, do you see what happens if #1 is more than 1 token??? Use \@car to avoid the bug!
 \else \ifx +#1{$+$}% TODO: bad method here, do you see what happens if #1 is more than 1 token??? Use \@car to avoid the bug!
 \else #1%
 \fi\fi%
}
% I don't quite understand the problem: that macro is used ONLY in \ox (lines 286-291) where no other input than arabic or (more often) roman numbers with a sign make any sense at all (chemically speaking*). In these cases everything happens as it is supposed to: the signs are changed into the math ones but the letters/numbers stay as they are. I must confess I didn't test senseless cases...
%* And I would like to think that users who use a macro for oxidation numbers know what oxidation numbers are
\DeclareRobustCommand\ox[1]{% needs to be robust in order to be used in mhchem's \ce macro
  \CM@splitting{#1}{2}%
  \CM@compareCs{CM@arg@i}{@empty}{\PackageError{chemmacros}{\string\ox: oxidation number missing}{}}{}%
  \CM@compareCs{CM@arg@ii}{@empty}{\PackageError{chemmacros}{\string\ox: atom missing}{}}{}%
  \ensuremath{\overset{\mbox{\tiny\expandafter\CM@ox@sign\CM@arg@i}}{\expandafter\CM@atom\expandafter{\CM@arg@ii}}}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - oxidation arrows ------------------------------------------------------------------ %
% place and name nodes:
% \OX{<name>,<atom>}
\DeclareRobustCommand*\OX[1]{% needs to be robust in order to be used in mhchem's \ce macro
  \CM@splitting{#1}{2}%
  \CM@compareCs{CM@arg@i}{@empty}{\PackageError{chemmacros}{\string\OX: node name missing}{}}{}%
  \CM@compareCs{CM@arg@i}{@empty}{\PackageError{chemmacros}{\string\OX: atom missing}{}}{}%
  \tikz[baseline=(\CM@arg@i.base),remember picture]{\node[inner sep=0](\CM@arg@i){\expandafter\CM@atom\expandafter{\CM@arg@ii}};}%
}

% connect nodes with "redox"-line
% \redox(<name1>,<name2>)[<tikz>][<dim>]{<text>}
\newcommand*\CM@redox@dist{.6em}
\newcommand*\setredoxdist[1]{\ifx\@empty#1\@empty\renewcommand*\CM@redox@dist{.6em}\else\renewcommand*\CM@redox@dist{#1}\fi}
\NewDocumentCommand\redox{d()oom}{%
  \IfNoValueTF{#1}{%
    \PackageError{chemmacros}{\string\redox: You need to specify coordinates}{}
  }{%
    \CM@splitting{#1}{2}%
    \let\CM@redox@begin\CM@arg@i%
    \let\CM@redox@end\CM@arg@ii%
  }
  \IfNoValueTF{#2}{\def\CM@redox@tikz{}}{\def\CM@redox@tikz{#2}}
  \IfNoValueTF{#3}{%
    \def\CM@redox@shift{1}%
    \def\CM@redox@anchor{above}%
    \def\CM@redox@side{north}%
    \def\CM@redox@sep{.2em}%
  }{%
    \pgfmathparse{#3<0}% TODO : You should learn to test if an integer is less than 0: \ifnum#1<\z@
% #3 is not necessarily an integer. It is more likely real; if you know a way to test if '-.5' is less than 0 that's easier than \pgfmathparse, please let me know.
    \ifnum\pgfmathresult=1 %
      \def\CM@redox@anchor{below}\def\CM@redox@side{south}\def\CM@redox@sep{-.2em}%
    \else%
      \def\CM@redox@anchor{above}\def\CM@redox@side{north}\def\CM@redox@sep{.2em}%
    \fi%
    \def\CM@redox@shift{#3}%
  }
  \tikz[remember picture,overlay]{\expandafter\draw\expandafter[\CM@redox@tikz] ($(\CM@redox@begin.\CM@redox@side)+(0,\CM@redox@sep)$) -- ++(0,\CM@redox@shift*\CM@redox@dist) -| node[pos=.25,\CM@redox@anchor]{#4} ($(\CM@redox@end.\CM@redox@side)+(0,\CM@redox@sep)$);}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - spectroscopy ---------------------------------------------------------------------- %
% \NMR(<num>,<elem>)[<num>,<unit>]{<solvent>} ALL arguments are optional
% \NMR* same but without ": $\delta$" at end
\AtBeginDocument{
 \NewDocumentCommand\NMR{sG{1,H}d()o}{%
  \IfBooleanTF{#1}{\let\CM@nmr@delta\@empty}{\ifmmode\def\CM@nmr@delta{\mbox{: }\delta}\else\def\CM@nmr@delta{: $\delta$}\fi}%
  \CM@splitting{#2}{2}%
  \let\CM@nmr@istp\CM@arg@i
  \let\CM@nmr@elem\CM@arg@ii
  \IfNoValueTF{#3}{%
    \let\CM@nmr@spec\@empty
    \let\CM@nmr@freq\@empty
  }{%
    \let\CM@nmr@spec\relax
    \CM@splitting{#3}{2}%
    \CM@compareCs{CM@arg@ii}{@empty}{\def\CM@nmr@unit{\mega\hertz}}{\def\CM@nmr@unit{\CM@arg@ii}}%
    \def\CM@nmr@freq{\expandafter\SI\expandafter{\CM@arg@i}{\CM@nmr@unit}}%
  }%
  \IfNoValueTF{#4}{%
    \let\CM@nmr@lm\@empty
    \let\CM@nmr@comma\@empty
  }{%
    \let\CM@nmr@spec\relax
    \def\CM@nmr@lm{\CM@atom{#4}}%
    \CM@compareCs{CM@nmr@freq}{@empty}{\let\CM@nmr@comma\@empty}{\def\CM@nmr@comma{,\ifmmode\medspace\else\hspace{\smallskipamount}\fi}}%
  }%
  \CM@compareCs{CM@nmr@spec}{@empty}{}{%
    \def\CM@nmr@spec{\hspace{\smallskipamount}(\CM@nmr@freq\CM@nmr@comma\CM@nmr@lm)}%
  }%
  \if@CM@bpchem
    \ifmmode
      \expandafter\text\expandafter{\IUPAC{\^{\CM@nmr@istp}\CM@nmr@elem-NMR}\CM@nmr@spec}\CM@nmr@delta
    \else
      \IUPAC{\^{\CM@nmr@istp}\CM@nmr@elem-NMR}\CM@nmr@spec\CM@nmr@delta\CM@xspace
    \fi
  \else
    \ifmmode
      ^{\CM@nmr@istp}\expandafter\CM@atom\expandafter{\CM@nmr@elem}\mbox{-}\text{NMR}\CM@nmr@spec\CM@nmr@delta
    \else
      $^{\CM@nmr@istp}$\expandafter\CM@atom\expandafter{\CM@nmr@elem}-NMR\CM@nmr@spec\CM@nmr@delta\CM@xspace
    \fi
  \fi
 }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - for use with mhchem --------------------------------------------------------------- %
\AtBeginDocument{\if@CM@mhchemloaded
% placing text below atom/molecule
  \newcommand*\CM@mhName@textattr@default{\centering\tiny}%
% \setmhName{<commands>}
  \newcommand*\setmhName[1]{%
    \ifx\@empty#1\@empty
      \renewcommand*\CM@mhName@textattr@default{\centering\tiny}%
    \else
      \renewcommand*\CM@mhName@textattr@default{#1}%
    \fi
  }
% \mhName[<width>][<textattr>]{<formula>}{<text>}
  \newlength{\CM@mhName@boxlength}%
  \NewDocumentCommand\mhName{oomm}{%
    \IfNoValueTF{#1}{%
      \settowidth{\CM@mhName@boxlength}{#3}%
    }{%
      \ifx\@empty#1\@empty
        \settowidth{\CM@mhName@boxlength}{#3}%
      \else
        \setlength{\CM@mhName@boxlength}{#1}%
      \fi
    }%
    \IfNoValueTF{#2}{%
      \let\CM@mhName@textattr\CM@mhName@textattr@default
    }{%
      \def\CM@mhName@textattr{#2}
    }
    \def\CM@mhName@formula{#3}
    \ensuremath{\underset{\parbox{\CM@mhName@boxlength}{\CM@mhName@textattr#4}}{\expandafter\ce\expandafter{\CM@mhName@formula}}}%
  }
% reaction environments for mhchem
  \RequirePackage{mathtools,environ}
  \newtagform{CMreaction}{\{}{\}}
  \newcounter{save@reaction}%
  \newcounter{reaction}%
  \newcommand*\CM@AtBeginReaction{\setcounter{save@reaction}{\value{equation}}\setcounter{equation}{\value{reaction}}\usetagform{CMreaction}}%
  \newcommand*\CM@AtEndReaction{\setcounter{reaction}{\value{equation}}\setcounter{equation}{\value{save@reaction}}\usetagform{default}}%
% \newreaction*+{<name>}{<type>}
% * and + are optional
  \NewDocumentCommand\newreaction{st+mm}{%
    \@ifundefined{#3}{%
      \IfBooleanTF{#2}{%
        \NewEnviron{#3}[1]{\CM@AtBeginReaction\begin{#4}{##1}\expandafter\cee\expandafter{\BODY}\end{#4}\CM@AtEndReaction}
        \IfBooleanTF{#1}{%
          \NewEnviron{#3*}[1]{\begin{#4*}{##1}\expandafter\cee\expandafter{\BODY}\end{#4*}}%
        }{}%
      }{%
        \NewEnviron{#3}{\CM@AtBeginReaction\begin{#4}\expandafter\cee\expandafter{\BODY}\end{#4}\CM@AtEndReaction}
        \IfBooleanTF{#1}{%
          \NewEnviron{#3*}{\begin{#4*}\expandafter\cee\expandafter{\BODY}\end{#4*}}%
        }{}%
      }%
    }{\PackageError{chemmacros}{The environment/command #3 is already defined}{}}%
  }
% predefined:
  \newreaction*{reaction}{equation}%
  \newreaction*{reactions}{align}%
\fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - phases ---------------------------------------------------------------------------- %
\newcommand*\solid[1][]{\ensuremath{_\mathrm{(\ifx\@empty#1\@empty\if@CM@german f\else s\fi\else#1\fi)}}}
\newcommand*\gas{\ensuremath{_\mathrm{(g)}}}
\newcommand*\liquid[1][]{\ensuremath{_\mathrm{(\ifx\@empty#1\@empty\if@CM@german f\/l\else l\fi\else#1\fi)}}}
\newcommand*\aq{\ensuremath{_\mathrm{(aq)}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - transition state symbol ----------------------------------------------------------- %
\AtBeginDocument{%
\newcommand*\CM@transition@state@symbol{\tikz[inner sep=0,outer sep=0,minimum size=0]{%
  \draw(0,0)--node[pos=.3333](@TSx){}node[pos=.6667](@TSy){}(0,.6em);%
  \draw(@TSx)++(-.3em,0)--++(.6em,0);%
  \draw(@TSy)++(-.3em,0)--++(.6em,0);}%
}
% make sure symbol scales correctly:
\if@CM@mychemistryloaded
  \renewcommand*\transitionstatesymbol{\ifmmode\text{\CM@transition@state@symbol}\else\CM@transition@state@symbol\fi}
\else
  \newcommand*\transitionstatesymbol{\ifmmode\text{\CM@transition@state@symbol}\else\CM@transition@state@symbol\fi}
\fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - thermodynamics et.al. ------------------------------------------------------------- %
\AtBeginDocument{\if@CM@chemstyleloaded\else\newcommand*\standardstate{\circ}\fi

% \setstatesubscript{right/left}
\newcommand*\CM@State@subscript{left}
\newcommand*\setstatesubscript[1]{%
  \renewcommand*\CM@State@subscript{\ifx\relax#1\relax left\else#1\fi}
}

% \State[<exp>,<Delta>,<subscript pos>]{<Symbol>}{<Index>}
% {<Index>} is an optional argument!
\NewDocumentCommand\State{omG{}}{%
  \IfNoValueTF{#1}{\CM@splitting{}{3}}{\CM@splitting{#1}{3}}%
  \ensuremath{%
    \CM@compareCs{CM@arg@iii}{@empty}{%
      \CM@compareStr{\CM@State@subscript}{right}{\let\CM@State@sub@left\@empty\def\CM@State@sub@right{_\mathrm{#3}}}{\let\CM@State@sub@right\@empty\def\CM@State@sub@left{_\mathrm{#3}}}%
    }{
      \CM@compareStr{\CM@arg@iii}{right}{\let\CM@State@sub@left\@empty\def\CM@State@sub@right{_\mathrm{#3}}}{%
        \CM@compareStr{\CM@arg@iii}{left}{\let\CM@State@sub@right\@empty\def\CM@State@sub@left{_\mathrm{#3}}}{%
          \PackageWarning{chemmacros}{\string\State: <subscript pos> can only have the values 'left' or 'right'}{}%
        }
      }%
    }%
    \CM@compareCs{CM@arg@ii}{@empty}{\Delta}{\CM@arg@ii}\CM@State@sub@left#2\CM@State@sub@right%
    \CM@compareCs{CM@arg@i}{@empty}{^{\standardstate}}{^{\CM@arg@i}}}%
}

% \setnewstate[<exp>,<Delta>,<subscript pos>]{<command>}{<letter>}{<unit>}
% \renewstate[<exp>,<Delta>,<subscript pos>]{<command>}{<letter>}{<unit>}
\newcommand*\CM@setnewstate[4][]{%
  \CM@splitting{#1}{3}% [<exp>,<Delta>,<subscript pos>]
  \CM@compareCs{CM@arg@i}{@empty}{\expandafter\let\csname CM@#2@supscript\endcsname\standardstate}{\expandafter\let\csname CM@#2@supscript\endcsname\CM@arg@i}%
  \CM@compareCs{CM@arg@ii}{@empty}{\expandafter\let\csname CM@#2@delta\endcsname\Delta}{\expandafter\let\csname CM@#2@delta\endcsname\CM@arg@ii}
  \CM@compareCs{CM@arg@iii}{@empty}{\@namedef{CM@#2@right}{left}}{\expandafter\let\csname CM@#2@right\endcsname\CM@arg@iii}%
  \@namedef{#2}{\@ifnextchar[{\@nameuse{CM@#2@ii}}{\@nameuse{CM@#2@i}}}%
  \@namedef{CM@#2@i}##1{\ensuremath{\State[\@nameuse{CM@#2@supscript},\@nameuse{CM@#2@delta},\@nameuse{CM@#2@right}]{#3}{}=\SI{##1}{#4}}}%
  \@namedef{CM@#2@ii}[##1]##2{%
    \CM@splitting{##1}{4}% [<index>,<exp>,<unit>,<subscript pos>]
    \let\CM@State@index\CM@arg@i
    \CM@compareCs{CM@arg@ii}{@empty}{\def\CM@State@supscript{\@nameuse{CM@#2@supscript}}}{\let\CM@State@supscript\CM@arg@ii}%
    \CM@compareCs{CM@arg@iii}{@empty}{\def\CM@State@unit{#4}}{\let\CM@State@unit\CM@arg@iii}%
    \CM@compareCs{CM@arg@iv}{@empty}{\def\CM@State@right{\@nameuse{CM@#2@right}}}{\let\CM@State@right\CM@arg@iv}%
    \ensuremath{\State[\CM@State@supscript,\@nameuse{CM@#2@delta},\CM@State@right]{#3}{\CM@State@index} = \SI{##2}{\CM@State@unit}}%
  }% 
}
\newcommand*\setnewstate[4][]{%
  \@ifundefined{#2}{\CM@setnewstate[#1]{#2}{#3}{#4}}{\PackageError{chemmacros}{\string\setnewstate: state #2 is already defined}{}}%
}
\newcommand*\renewstate[4][]{%
  \@ifundefined{#2}{\PackageError{chemmacros}{\string\renewstate: state #2 is not defined}{}}{\CM@setnewstate[#1]{#2}{#3}{#4}}%
}
\setnewstate{Enthalpy}{H}{\kilo\joule\per\mole}%
\setnewstate[, ]{Entropy}{S}{\joule\per\kelvin\per\mole}%
\setnewstate{Gibbs}{G}{\kilo\joule\per\mole}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - p-orbitals ------------------------------------------------------------------------ %
\newcommand*\CM@porb[1][]{%
  \CM@splitting{#1}{3}%
  \CM@compareCs{CM@arg@i}{@empty}{\let\CM@orbital@height\CM@orb@height}{\def\CM@orbital@height{\CM@arg@i*\CM@orb@height}}%
  \CM@compareCs{CM@arg@ii}{@empty}{\def\CM@orb@color{black}}{\let\CM@orb@color\CM@arg@ii}%
  \CM@compareStr{\CM@orb@color}{white}{\def\CM@orb@color@draw{black}}{\let\CM@orb@color@draw\CM@orb@color}%
  \CM@compareCs{CM@arg@iii}{@empty}{\def\CM@orb@ang{0}}{\let\CM@orb@ang\CM@arg@iii}%
  \tikzpicture[overlay,rotate={\CM@orb@ang-90},opacity=.5,inner sep=0pt,outer sep=0pt,text height=0pt,text depth=0pt]%
   \pgfsetfillopacity{.5}%
   \CM@compareStr{\CM@orb@half}{false}{%
     \draw[draw=black] (0,0) .. controls ++(-.9*\CM@orbital@height,-1.5*\CM@orbital@height) and ++(.9*\CM@orbital@height,-1.5*\CM@orbital@height) .. (0,0);%
   }{}%
   \draw[draw=\CM@orb@color@draw,fill=\CM@orb@color] (0,0) .. controls ++(-.9*\CM@orbital@height,1.5*\CM@orbital@height) and ++(.9*\CM@orbital@height,1.5*\CM@orbital@height) .. (0,0);%
  \endtikzpicture%
}
% full orbital:
% \porb[<size factor>,<color>,<angle>]
\newcommand*\porb{\def\CM@orb@half{false}\CM@porb}%
% half of an orbital:
% \phorb[<size factor>,<color>,<angle>]
\newcommand*\phorb{\def\CM@orb@half{true}\CM@porb}%

% predefined orbitals:
\newcommand*\pzorb{\porb[,,90]}%
\newcommand*\pyorb{\porb[,,0]}%
\newcommand*\pxorb{\porb[,,-135]}%

% change orbital settings:
\newcommand*\CM@orb@height{1em}%
\newcommand*\setorbheight[1]{%
  \ifx\@empty#1\@empty
    \renewcommand*\CM@orb@height{1em}%
  \else
    \renewcommand*\CM@orb@height{#1}%
  \fi
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% - Newman projections ---------------------------------------------------------------- %
% \newman[<angle>,<scale>,<tikz ring>][<tikz (front) nodes>][<tikz back nodes>]{<1>,<2>,<3>,<4>,<5>,<6>}
\NewDocumentCommand\newman{ooom}{%
  \IfNoValueTF{#1}{%
    \def\CM@newman@angle{0}%
    \def\CM@newman@scale{1}%
    \def\CM@newman@tikz@ring{}%
  }{%
    \CM@splitting{#1}{3}%
    \CM@compareCs{CM@arg@i}{@empty}{\def\CM@newman@angle{0}}{\let\CM@newman@angle\CM@arg@i}%
    \CM@compareCs{CM@arg@ii}{@empty}{\def\CM@newman@scale{1}}{\let\CM@newman@scale\CM@arg@ii}%
    \expandafter\def\expandafter\CM@newman@tikz@ring\expandafter{\CM@arg@iii}%
  }
  \IfNoValueTF{#2}{%
    \let\CM@newman@tikz@one\@empty%
  }{%
    \def\CM@newman@tikz@one{#2}%
  }
  \IfNoValueTF{#3}{%
    \let\CM@newman@tikz@two\CM@newman@tikz@one%
  }{%
    \def\CM@newman@tikz@two{#3}%
  }
  \CM@splitting{#4}{6}%
  \edef\CM@newman@tikz{scale=\CM@newman@scale,CMatomFront/.style={inner sep=0,outer sep=0,\CM@newman@tikz@one},CMatomBack/.style={inner sep=0,outer sep=0,\CM@newman@tikz@two}}
  \expandafter\tikz\expandafter[\CM@newman@tikz]{%
    \expandafter\draw\expandafter[\CM@newman@tikz@ring] (0,0) circle (.5);%
    \foreach\i in{30+\CM@newman@angle,150+\CM@newman@angle,270+\CM@newman@angle}{%
      \expandafter\draw\expandafter[\CM@newman@tikz@ring] (\i:0.5) --++(\i:.5);%
    }%
    \node[CMatomBack,anchor=-150+\CM@newman@angle] at (30+\CM@newman@angle:1.05) {\CM@arg@v};%
    \node[CMatomBack,anchor=-30+\CM@newman@angle] at (150+\CM@newman@angle:1.05) {\CM@arg@vi};%
    \node[CMatomBack,anchor=90+\CM@newman@angle] at (270+\CM@newman@angle:1.05) {\CM@arg@iv};%
    \foreach\i in{90,210,330}{%
      \expandafter\draw\expandafter[\CM@newman@tikz@ring] (0,0) --++(\i:1);%
    }%
    \node[CMatomFront,anchor=-90] at (90:1.05) {\CM@arg@i};%
    \node[CMatomFront,anchor=30] at (210:1.05) {\CM@arg@ii};%
    \node[CMatomFront,anchor=150] at (330:1.05) {\CM@arg@iii};%
  }%
}
\endinput
% - Version history: ------------------------------------------------------------------ %
% 2011/05/15 version 1.0 CTAN release                                                   %
% 2011/06/22 version 1.1 "LaTeXified" whole package for safer usage                     %
%                        less user work required (like loading package after another)   %
%                        more particle macros                                           %
%                        latin phrases                                                  %
%                        extra units                                                    %
%                        acid/base commands                                             %
%                        \mech                                                          %
%                        \NMR                                                           %
%                        \mhName, \setmhName, \newreaction, phases                      %
%                        \renewstate, \setstatesubscript                                %
%                        improved orbitals                                              %
%                        simpler package options, option german, bpchem                 %
%                        bug fixes                                                      %