%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --------------------------------------------------------------------------- %
% - the CHEMMACROS bundle                                                   - %
% - chemmacros-codehelper.tex                                               - %
% - macros and commands for chemists                                        - %
% --------------------------------------------------------------------------- %
% - Clemens Niederberger                                                    - %
% - 2012/03/19                                                              - %
% --------------------------------------------------------------------------- %
% - https://bitbucket.org/cgnieder/chemmacros/                              - %
% - contact@mychemistry.eu                                                  - %
% --------------------------------------------------------------------------- %
% - If you have any ideas, questions, suggestions or bugs to report, please - %
% - feel free to contact me.                                                - %
% --------------------------------------------------------------------------- %
% - Copyright 2011-2012 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.            - %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Just to shorten the preamble of the documentation
%
% The beispiel environment is just a copy from the LaTeXdemo environment in
% the siunitx documentation which has been (minimally) adapted and provides
% environment beispiel with options
%  * [code and example] (default)
%  * [below] (like default but forces example below code)
%  * [ox] (like below but with larger skip)
%  * [code only]
%  * [code and float]
%
% For creating examples with nice highlighting of code, and so on; based on the
% system used in the listings source (lstsample).
\RequirePackage[final]{listings}
\RequirePackage{framed,xcolor}
\RequirePackage[framemethod=tikz]{mdframed}
\RequirePackage[colorinlistoftodos,disable]{todonotes}
  \xdefinecolor{code}{rgb}{.6,.25,.15}
  \xdefinecolor{key}{rgb}{0.02,0.48,0.18}
  \xdefinecolor{myblue}{rgb}{0.02,0.04,0.48}
  \xdefinecolor{myred}{rgb}{0.65,0.04,0.07}
  \xdefinecolor{comment}{rgb}{0.4,0.4,0.4}
  \colorlet{codebackground}{yellow!10}
  \colorlet{module}{myred}

\makeatletter
\newcommand*\bspmidlength[2]{%
  \@namedef{beispiel@#1@midlength}{#2}}

\lst@RequireAspects{writefile}
\newsavebox{\beispiel@box}

\lstnewenvironment{beispiel}[1][code and example]
  {%
    \global\let\lst@intname\@empty
    \expandafter\let\expandafter\beispiel@end
      \csname beispiel@#1@end\endcsname
    \@nameuse{beispiel@#1}%
  }
  {\beispiel@end}

\newcommand*\beispiel@new[3]{%
  \expandafter\newcommand\expandafter*\expandafter
    {\csname beispiel@#1\endcsname}{#2}%
  \expandafter\newcommand\expandafter*\expandafter
    {\csname beispiel@#1@end\endcsname}{#3}%
}

\newcommand*\beispiel@common{%
  \setkeys{lst}
    {
      language=[LaTeX]TeX,
      basicstyle={\ttfamily\footnotesize},          % Grundstil
      extendedchars=true,
      numbers=left,                                 % Zeilennummern
      numberstyle=\tiny,                            % Größe des Zeilennummern
      numberblanklines=true,                        % Leerzeilen nummerieren
      gobble=0,                                     % das erste Leerzeichen abschneiden
      xleftmargin=20pt,                             % Einrückung links
      breaklines=true,                              % Zeilenumbruch
      literate={ä}{{\"a}}1 {ö}{{\"o}}1 {ü}{{\"u}}1 {Ä}{{\"A}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1 {ß}{{\ss}}1,
      commentstyle={\color{comment}},
      keywordstyle=\color{code},
      texcsstyle=[30]\color{code},
      moretexcs=[30]
        {
          a,
          abinitio,
          AddRxnDesc,
          aq,
          aqi,
          b,
          ba,
          bottomrule,
          cancel,
          cd,
          cdot,
          ce,
          cee,
          celsius,
          centering,
          chemabove,
          Chemalpha,
          Chembeta,
          Chemgamma,
          Chemdelta,
          ChemDelta,
          chemfig,
          chemname,
          Chemomega,
          chemsetup,
          cip,
          cis,
          ch,
          cnsetup,
          CNMR,
          color,
          cstsetup,
          d,
          D,
          DeclareChemArrow,
          DeclareChemIUPAC,
          DeclareChemLatin,
          DeclareChemParticle,
          DeclareChemPhase,
          DeclareChemReaction,
          DeclareChemState,
          DeclareInstance,
          DeclareSIUnit,
          definecolor,
          delm,
          delp,
          Delta,
          Dfi,
          draw,
          E,
          el,
          ElPot,
          Enthalpy,
          enthalpy,
          Entropy,
          footnotesize,
          fmch,
          fpch,
          fscrm,
          fscrp,
          g,
          gas,
          ghs,
          ghslistall,
          ghspic,
          Gibbs,
          glqq,
          grqq,
          H,
          hapto,
          HNMR,
          Helmholtz,
          hertz,
          hspace,
          includegraphics,
          insitu,
          intertext,
          invacuo,
          iupac,
          IUPAC,
          joule,
          Ka,
          Kb,
          kilo,
          Kw,
          L,
          latin,
          lewis,
          Lewis,
          Lfi,
          listofreactions,
          lqd,
          mch,
          mega,
          meta,
          mhName,
          midrule,
          mmHg,
          mole,
          N,
          nicefrac,
          newman,
          NMR,
          Nu,
          Nuc,
          num,
          O,
          ominus,
          orbital,
          ortho,
          oplus,
          ox,
          OX,
          p,
          P,
          para,
          pch,
          per,
          pgfarrowsdeclarealias,
          pgfarrowsrenewalias,
          pH,
          phase,
          photon,
          pKa,
          pKb,
          pOH,
          positron,
          Pot,
          prt,
          R,
          Rad,
          redox,
          RenewChemArrow,
          RenewChemIUPAC,
          RenewChemLatin,
          RenewChemParticle,
          RenewChemPhase,
          RenewChemState,
          renewtagform,
          rightarrow,
          S,
          Sf,
          sample,
          scriptscriptstyle,
          scrm,
          scrp,
          setatomsep,
          setbondoffset,
          sfrac,
          shorthandoff,
          ShowChemArrow,
          si,
          SI,
          sisetup,
          sld,
          Sod,
          State,
          subsection,
          textcolor,
          textendash,
          textsuperscript,
          tiny,
          toprule,
          trans,
          upbeta,
          upeta,
          upgamma,
          volt,
          vphantom,
          vspave,
          w,
          xspace,
          Z
        }
    }%
}

\newcommand*\beispiel@input[1][\jobname.tmp]{%
  \catcode`\^^M=5\relax
  \small
  \begingroup
    \leavevmode
      \input{#1}%
  \endgroup
}

\beispiel@new{code and example}
  {%
    \setbox\beispiel@box=\hbox\bgroup
      \lst@BeginAlsoWriteFile{\jobname.tmp}%
      \beispiel@common
  }
  {%
      \lst@EndWriteFile
    \egroup
    \colorlet{shadecolor}{codebackground}
    \begin{shaded}
      \ifdim\wd\beispiel@box>0.48\linewidth\relax
        \begin{minipage}{.9\linewidth}
          \hbox to \linewidth{\box\beispiel@box\hss}%
        \end{minipage}
        \vskip3mm
        \begin{minipage}{.9\linewidth}
          \beispiel@input
        \end{minipage}
      \else
        \begin{minipage}{0.48\linewidth}
          \hbox to\linewidth{\box\beispiel@box\hss}%
        \end{minipage}
        \hfill
        \begin{minipage}{0.48\linewidth}
          \beispiel@input
        \end{minipage}
      \fi
    \end{shaded}
  }

\bspmidlength{below}{3mm}
\beispiel@new{below}
  {%
    \setbox\beispiel@box=\hbox\bgroup
      \lst@BeginAlsoWriteFile{\jobname.tmp}%
      \beispiel@common
  }
  {%
      \lst@EndWriteFile
    \egroup
    \colorlet{shadecolor}{codebackground}
    \begin{shaded}
      \begin{minipage}{.9\linewidth}
        \hbox to \linewidth{\box\beispiel@box\hss}%
      \end{minipage}
      \expandafter\vskip\beispiel@below@midlength
      \begin{minipage}{.9\linewidth}
        \beispiel@input
      \end{minipage}
    \end{shaded}
  }

\bspmidlength{ox}{7mm}
\beispiel@new{ox}
  {%
    \setbox\beispiel@box=\hbox\bgroup
      \lst@BeginAlsoWriteFile{\jobname.tmp}%
      \beispiel@common
  }
  {%
      \lst@EndWriteFile
    \egroup
    \colorlet{shadecolor}{codebackground}
    \begin{shaded}
      \begin{minipage}{.9\linewidth}
        \hbox to \linewidth{\box\beispiel@box\hss}%
      \end{minipage}
      \expandafter\vskip\beispiel@ox@midlength
      \begin{minipage}{.9\linewidth}
        \beispiel@input
      \end{minipage}
      \vskip7mm
    \end{shaded}
  }

\beispiel@new{code and float}{%
  \colorlet{shadecolor}{codebackground}
  \shaded
  \lst@BeginAlsoWriteFile{bsp.tmp}%
  \beispiel@common
}{%
  \lst@EndWriteFile
  \endshaded
  \beispiel@input[bsp.tmp]
}

\beispiel@new{code only}
  {\colorlet{shadecolor}{codebackground}\shaded\beispiel@common}{\endshaded}
\makeatother

\lstset{
      language=[LaTeX]TeX,
      basicstyle={\ttfamily},                       % Grundstil
      extendedchars=true,
      numbers=left,                                 % Zeilennummern
      numberstyle=\tiny,                            % Größe des Zeilennummern
      numberblanklines=true,                        % Leerzeilen nummerieren
      gobble=0,                                     % das erste Leerzeichen abschneiden
      xleftmargin=20pt,                             % Einrückung links
      breaklines=true,                              % Zeilenumbruch
      literate={ä}{{\"a}}1 {ö}{{\"o}}1 {ü}{{\"u}}1 {Ä}{{\"A}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1 {ß}{{\ss}}1,
      commentstyle={\color{comment}},
      keywordstyle=\color{code},
      texcsstyle=[30]\color{code},
      moretexcs=[30]
        {
          a,
          abinitio,
          AddRxnDesc,
          aq,
          aqi,
          b,
          ba,
          bottomrule,
          cancel,
          cd,
          cdot,
          ce,
          cee,
          celsius,
          centering,
          chemabove,
          Chemalpha,
          Chembeta,
          Chemgamma,
          Chemdelta,
          ChemDelta,
          chemfig,
          chemname,
          Chemomega,
          chemsetup,
          cip,
          cis,
          ch,
          cnsetup,
          CNMR,
          color,
          cstsetup,
          d,
          D,
          DeclareChemArrow,
          DeclareChemIUPAC,
          DeclareChemLatin,
          DeclareChemParticle,
          DeclareChemPhase,
          DeclareChemReaction,
          DeclareChemState,
          DeclareInstance,
          DeclareSIUnit,
          definecolor,
          delm,
          delp,
          Delta,
          Dfi,
          draw,
          E,
          el,
          ElPot,
          Enthalpy,
          enthalpy,
          Entropy,
          footnotesize,
          fmch,
          fpch,
          fscrm,
          fscrp,
          g,
          gas,
          ghs,
          ghslistall,
          ghspic,
          Gibbs,
          glqq,
          grqq,
          H,
          hapto,
          HNMR,
          Helmholtz,
          hertz,
          hspace,
          includegraphics,
          insitu,
          intertext,
          invacuo,
          iupac,
          IUPAC,
          joule,
          Ka,
          Kb,
          kilo,
          Kw,
          L,
          latin,
          lewis,
          Lewis,
          Lfi,
          listofreactions,
          lqd,
          mch,
          mega,
          meta,
          mhName,
          midrule,
          mmHg,
          mole,
          N,
          nicefrac,
          newman,
          NMR,
          Nu,
          Nuc,
          num,
          O,
          ominus,
          orbital,
          ortho,
          oplus,
          ox,
          OX,
          p,
          P,
          para,
          pch,
          per,
          pgfarrowsdeclarealias,
          pgfarrowsrenewalias,
          pH,
          phase,
          photon,
          pKa,
          pKb,
          pOH,
          positron,
          Pot,
          prt,
          R,
          Rad,
          redox,
          RenewChemArrow,
          RenewChemIUPAC,
          RenewChemLatin,
          RenewChemParticle,
          RenewChemPhase,
          RenewChemState,
          renewtagform,
          rightarrow,
          S,
          Sf,
          sample,
          scriptscriptstyle,
          scrm,
          scrp,
          setatomsep,
          setbondoffset,
          sfrac,
          shorthandoff,
          ShowChemArrow,
          si,
          SI,
          sisetup,
          sld,
          Sod,
          State,
          subsection,
          textcolor,
          textendash,
          textsuperscript,
          tiny,
          toprule,
          trans,
          upbeta,
          upeta,
          upgamma,
          volt,
          vphantom,
          vspave,
          w,
          xspace,
          Z
        }
    }%

\makeatletter
\def\GetFileInfo#1{%
  \def\filename{#1}%
  \def\@tempb##1 ##2 ##3\relax##4\relax{%
    \def\filedate{##1}%
    \def\fileversion{##2}%
    \def\fileinfo{##3}}%
  \edef\@tempa{\csname ver@#1.sty\endcsname}%
  \expandafter\@tempb\@tempa\relax? ? \relax\relax}
\makeatother

\ExplSyntaxOn
\DeclareDocumentEnvironment { myquote } { o }
  {
    \quote
    \small\sffamily
  }
  {
    \IfNoValueF { #1 }
      {
        \par\noindent
        \parbox { \linewidth } { \raggedleft \normalfont \itshape #1 }
      }
    \endquote
  }

\makeatletter
\NewDocumentCommand \paket { som }
  {
    \textsf { #3 }
    \cs_if_exist:cF { paket @ #3 }
      {
        \cs_new_nopar:cpn { paket @ #3 } { }
        \IfBooleanF { #1 }
          {
            \footnote
              {
                CTAN: ~
                \IfNoValueTF { #2 }
                  { \hyper@linkurl { #3 } { http://www.ctan.org/pkg/#3/} }
                  { \hyper@linkurl { #3 } { http://www.ctan.org/pkg/#2/} }
              }
          }
      }
    \index { #3 @ \textsf { #3 } }
  }
\makeatother

% #1: module #2: key #3: value
\NewDocumentCommand \key { somg }
  {
    \textcolor { key } { \texttt { #3 } }
    \IfNoValueF { #4 } { \texttt { ~=~#4 } }
    \index { #3 @ \textcolor { key } { \texttt { #3 } } }
    \IfBooleanT { #1 }
      {
        \cs_if_exist:cF { none_#3 }
          {
            \cs_new:cpn { none_#3 } {}
            \marginline { \small \textcolor { red }  { \textendash none \textendash } }
            \label{key:none_#3}
          }
      }
    \IfNoValueF { #2 }
      {
        \index
          {
            #2 @ \textcolor { module } { \texttt { #2 } } !
            #3 @ \textcolor { key } { \texttt { #3 } }
          }
        \cs_if_exist:cF { #2_#3 }
          {
            \cs_new:cpn { #2_#3 } {}
            \marginline { \small \textcolor { module }  { \texttt { #2 } } }
            \label{key:#2_#3}
          }
      }
  }

\NewDocumentCommand \cmd { omd()od()og }
  {
    \textcolor{code} { \texttt { \textbackslash #2 } }
    \IfNoValueF { #3 } { \texttt { ( #3 ) } }
    \IfNoValueF { #4 } { \texttt { [ #4 ] } }
    \IfNoValueF { #5 } { \texttt { ( #5 ) } }
    \IfNoValueF { #6 } { \texttt { [ #6 ] } }
    \IfNoValueF { #7 } { \texttt { \{ #7 \} } }
    \index
      {
        \IfNoValueTF { #1 } { #2 } { #1 }
        @ \textcolor { code } { \texttt { \textbackslash #2 } }
      }
  }

\NewDocumentCommand \env { omm }
  {
    \texttt{\textcolor{code}{\textbackslash begin}\{#2\}}
    \IfNoValueF { #1 } { \texttt { [ #2 ] } } 
    \tl_use:N \c_space_tl \texttt { #3 } ~
    \texttt{\textcolor{code}{\textbackslash end}\{#2\}}
    \index { #2 @ \texttt { #2 } ~ ( env. ) }
  }

\NewDocumentCommand \secidx { somo }
  {
    \IfBooleanTF { #1 }
      {
        \IfNoValueT { #2 }
          { \index { \IfNoValueTF { #4 } { #3 } { #4 } @ \textbf { #3 } |) } }
      }
      {
        \IfNoValueT { #2 }
          { \index { \IfNoValueTF { #4 } { #3 } { #4 } @ \textbf { #3 } |( } }
      }
    \IfNoValueF { #2 }
      { \index { \IfNoValueTF { #4 } { #3 } { #4 } @ \textbf { #3 } ! #2 } }
  }

\NewDocumentCommand \ma { m }
  { \texttt { \{ #1 \} } }

\NewDocumentCommand \oa { m }
  { \texttt { [ #1 ] } }
  
\NewDocumentCommand \da { m }
  { \texttt { ( #1 ) } }

\makeatletter
\NewDocumentCommand \achtung { m }
  {
    \ifvmode\addvspace{\baselineskip}\fi
    \begin{mdframed}[linecolor=red,backgroundcolor=red!10]
      #1
    \end{mdframed}
    \par\addvspace{\baselineskip}
    \@endpetrue
  }
\makeatother

\cs_new:Nn \chemmacros_doc_todo:nn
  { \todo [ #1 ] { #2 } }
\cs_generate_variant:Nn \chemmacros_doc_todo:nn { xn }

\tl_new:N \l_chemmacros_doc_todo_inline_tl

\NewDocumentCommand\TODO{sm}
  {
    \group_begin:
      \IfBooleanTF{#1}
        { \tl_set:Nn \l_chemmacros_doc_todo_inline_tl {, inline } }
        { \tl_set:Nn \l_chemmacros_doc_todo_inline_tl { } }
      \chemmacros_doc_todo:xn
        {
          color = myblue,
          backgroundcolor = white,
          size = \exp_not:N \small
          \l_chemmacros_doc_todo_inline_tl
        }
        { \textcolor { myblue } { \textbf { TODO: } } \\ #2 }
    \group_end:
  }

\ExplSyntaxOff

\newcommand*\IUPAC{\mbox{IUPAC}\xspace}

\endinput