%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ------------------------------------------------------------------------------------- %
% - myChemistry - mychemistry.sty ----------------------------------------------------- %
% - Creating reaction schemes with LaTeX and ChemFig ---------------------------------- %
% ------------------------------------------------------------------------------------- %
% - Clemens Niederberger -------------------------------------------------------------- %
% - 2011/03/20 ------------------------------------------------------------------------ %
% ------------------------------------------------------------------------------------- %
% - http://www.niederberger-berlin.net/2011/02/latex-mychemistry/ --------------------- %
% - kontakt@niederberger-berlin.net --------------------------------------------------- %
% ------------------------------------------------------------------------------------- %
% - 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 mychemistry.sty, mychemistry_de.tex and           - %
% - mychemistry_en.tex                                                                - %
% ------------------------------------------------------------------------------------- %
% - 2011/02/24 version 1.0                                                            - %
% ------------------------------------------------------------------------------------- %
% - 2011/03/01 version 1.1                                                            - %
% -   package option `english' added                                                  - %
% -   package key `placement' added                                                   - %
% -   rxnscheme key `placement' added                                                 - %
% -   package key `shade=boolean' changed into package option `shade'                 - %
% -   package option `nochemexec' added, will prevent mC from loading chemexec        - %
% - 2011/03/20 version 1.2                                                            - %
% -   license changed into LPPL 1.3 or later                                          - %
% -   package option `nocompounds' added, will prevent mC from loading chemcompounds  - %
% -   package option `chemstyle' added, will load chemstyle                           - %
% -   package option `nomhchem' added, will prevent mC from loading mhchem            - %
% -     if chemexec isn't loaded either                                               - %
% -   package option `nopackages' added, will prevent mC from loading any             - %
% -     chemistry package except chemfig                                              - %
% -   some small issues resolved                                                      - %
% -   bug in arrow type <=> resolved                                                  - %
% -   arrow argument placement rewritten; two new keys for arrow: `aboveshift'        - %
% -     and `belowshift' (causing new problems with alignment, though)                - %
% -   wrong arrow alignment (due to last change) mainly resolved (NEEDS pgf v2.10!)   - %
% -     => solved wrong branch alignment as well                                      - %
% -   `inner sep' set to 0, arrow, transition & mesomeric adjusted                    - %
% -   bug in arrow type -|> resolved                                                  - %
% -   commands \makevisible / \makeinvisible added                                    - %
% -   arrow key `both' added                                                          - %
% -   rxn has now keys instead of an option                                           - %
% -   commands \setrcndist, \setrxnalign, \setschemealign added                       - %
% -   rxn-, rxnscheme-key align added                                                 - % 
% ------------------------------------------------------------------------------------- %
% - TODO                                                                              - %
% -   make the arrow type {-|>} look nicer                                            - %
% -   work on how chemistry-packages are automatically loaded;                        - %
% -     add ui for easy handling this issue? (via \mCsetup ?)                         - %
% -   add rotate=<winkel> for diagonal arrows?                                        - %
% -   add \mCdefault or sim. to restore all default settings?                         - %
% ------------------------------------------------------------------------------------- %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\@mc@version{v1.2}
\def\@mc@date@de{20. M\"arz 2011}
\def\@mc@date@en{March 20th 2011}
\ProvidesPackage{mychemistry}[2011/03/20]
\typeout{myChemistry \@mc@version}
\typeout{2011/03/20 Clemens Niederberger}
\typeout{}
\RequirePackage{ifthen,calc,float}
\RequirePackage{xkeyval}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Optionen, z.T. auch an chemexec weitergeben
\newcommand{\@mc@exec@color}{black}
\newcommand{\@mc@exec@shade}{false}
\newif\if@usecolor\@usecolorfalse
% wenn key color, dann Farbe und Option shade=true an chemexec weitergeben
\define@key[MC]{mychemistry.sty}%
  {color}{
    \@usecolortrue
    \renewcommand{\@mc@exec@color}{#1}
    \renewcommand{\@mc@exec@shade}{true}
  }
% sonst nicht:
\DeclareOptionX[MC]{nocolor}{
  \@usecolortrue
  \renewcommand{\@mc@exec@color}{black}
  \renewcommand{\@mc@exec@shade}{false}
}
% shade separat (de-)aktivieren
\DeclareOptionX[MC]{shade}{%
  \renewcommand{\@mc@exec@shade}{true}
}
% chemexec abschalten
\newcommand{\@mc@exec@true}{true}
\DeclareOptionX[MC]{nochemexec}{%
  \renewcommand{\@mc@exec@true}{false}
}
% mhchem abschalten
\newcommand{\@mc@mhchem@true}{true}
\DeclareOptionX[MC]{nomhchem}{%
  \renewcommand{\@mc@mhchem@true}{false}
}
% chemcompounds abschalten
\newcommand{\@mc@compound@true}{true}
\DeclareOptionX[MC]{nocompounds}{%
  \renewcommand{\@mc@compound@true}{false}
}
% chemstyle einschalten
\newcommand{\@mc@chemstyle@true}{false}
\DeclareOptionX[MC]{chemstyle}{%
  \renewcommand{\@mc@chemstyle@true}{true}
}
% nur `chemfig' laden
\DeclareOptionX[MC]{nopackages}{%
  \renewcommand{\@mc@exec@true}{false}
  \renewcommand{\@mc@mhchem@true}{false}
  \renewcommand{\@mc@compound@true}{false}
  \renewcommand{\@mc@chemstyle@true}{false}
}
% englische Version von chemexec, englische Variante von myChemistry
\newcommand{\@mc@exec@english}{}
\newcommand{\@mc@rs@name}{Reaktionsschema}
\DeclareOptionX[MC]{english}{
  \renewcommand{\@mc@exec@english}{,english}
  \renewcommand{\@mc@rs@name}{Reaction scheme}
}
\newcommand{\@mc@rs@placement}{H}
\newcommand{\@mc@rs@currentplacement}{\@mc@rs@placement}
\define@key[MC]{mychemistry.sty}{placement}{
  \renewcommand{\@mc@rs@placement}{#1}
}
\ProcessOptionsX[MC]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% chemstyle laden, falls verfügbar und Option aktiv
\IfFileExists{chemstyle.sty}{%
  \ifthenelse{\equal{\@mc@chemstyle@true}{true}}{
    \@ifpackageloaded{chemstyle}{}{
      \RequirePackage[varioref=false,floats=float]{chemstyle}
      \PackageInfo{mychemistry}{You loaded `chemstyle' with options `varioref=false' and `floats=float'. Please be aware, that you might need to restyle the float `table'. Take a look at the `chemstyle' documentation for further information.}
    }
  }{}
}{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% chemcompounds laden, falls verfügbar
\IfFileExists{chemcompounds.sty}{%
  \ifthenelse{\equal{\@mc@compound@true}{true}}{
    \@ifpackageloaded{chemcompounds}{}{
      \RequirePackage{chemcompounds}
    }
  }{}
}{
  \typeout{`chemcompounds' is not installed.}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% chemfig laden, falls verfügbar
\IfFileExists{chemfig.sty}{%
  \@ifpackageloaded{chemfig}{}{
    \RequirePackage{chemfig}
  }
  \usetikzlibrary{arrows,positioning,decorations.pathmorphing,shapes,calc,matrix,chains,scopes}
}{% sonst Fehlermeldung
  \PackageError{mychemistry}{`ChemFig' is not installed! `mychemistry' can't work without it!}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% chemexec laden, falls verfügbar, falls nicht, dann wenigstens mhchem laden
\IfFileExists{chemexec.sty}{% mit den Default-Optionen color=black, shade=false,exercise,exersize=large
  \ifthenelse{\equal{\@mc@exec@true}{true}}{
    \ifthenelse{\equal{\@mc@mhchem@true}{true}}{}{
      \PackageWarning{mychemistry}{You used the option `nomhchem' but not `nochemexec'. Please be aware that `mhchem' is loaded by `chemexec' anyway.}
    }
    \@ifpackageloaded{chemexec}{}{
      \RequirePackage[color=\@mc@exec@color,shade=\@mc@exec@shade,exercise,exersize=large\@mc@exec@english]{chemexec}
    }
  }{}
}{
  \typeout{`chemexec' ist not installed.}
  \IfFileExists{mhchem.sty}{
    \ifthenelse{\equal{\@mc@mhchem@true}{true}}{
      \@ifpackageloaded{mhchem}{}{
        \RequirePackage[version=3]{mhchem}
      }
    }{}
  }{
    \typeout{`mhchem' ist not installed.}
  }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NEUE BEFEHLE:                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\providecommand{\mCversion}{\@mc@version}
\providecommand{\mCdate}[1]{
  \ifthenelse{\equal{#1}{de}}{\@mc@date@de}{}
  \ifthenelse{\equal{#1}{en}}{\@mc@date@en}{}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PFEILE ------------------------------------------------------------------------------ %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% interne Befehle, Voreinstellungen --------------------------------------------------- %
\newcommand{\@arrow@above}{above}                % oben
\newcommand{\@arrow@currentabove}{\@arrow@above} %
\newcommand{\@arrow@below}{below}                % unten
\newcommand{\@arrow@currentbelow}{\@arrow@below} %
\newcommand{\@arrow@direction}{right}            % Richtung
\newcommand{\@arrow@currentdirection}{\@arrow@direction}
\newcommand{\@arrow@currentlength}{\@arrow@length}
\newlength{\@arrow@length@actual}
\newcommand{\@arrow@rotate}{}
\newcommand{\@arrow@argument@angle}{}
\newcommand{\@arrow@argument@shift}{2pt}
\newcommand{\@arrow@argument@anchor}{}
\newcommand{\@arrow@argument@both}{}
%\newcommand{\@arrow@argument@size}{}
\newcommand{\@arrow@above@anchor}{}
\newcommand{\@arrow@below@anchor}{}
\newcommand{\@arrow@type}{->}                    % Pfeiltyp
\newcommand{\@arrow@currenttype}{\@arrow@type}   %
\newcommand{\@arrow@start@up}{0,0.05}            % Verschiebung für Glgw-Pfeil
\newcommand{\@arrow@start@down}{0,-0.05}         %
\newcommand{\@arrow@placement}{on chain=going \@arrow@currentdirection} % wohin?
\newcommand{\@arrow@currentplacement}{\@arrow@placement}
\newcommand{\@arrow@cross@base@x}{}              % Befehle für durchgestrichenen Pfeil
\newcommand{\@arrow@cross@base@y}{}              %
\newcommand{\@arrow@cross@angle}{}               %
\newcommand{\@arrow@cross@length}{}              %
\newcommand{\@arrow@cross@draw}{}                %

% Laengenfaktor ----------------------------------------------------------------------- %
\newcommand{\@arrow@length}{1.0}                 % Längenfaktor
\newcommand{\@arrow@length@factor}{1.0}          % Default-Faktor
\newcommand{\@arrow@end}{\@arrow@length@actual,0} % Pfeilende
\define@key{arrow}{length}[\@arrow@length]{%     %
    \renewcommand{\@arrow@currentlength}{#1}}%   %
\newlength{\@arrow@length@default}               %
\setlength{\@arrow@length@default}{5em}          % Default-Laenge
\newcommand{\arrowlength}[1]{\setlength{\@arrow@length@default}{#1}}

% Beschriftungen ---------------------------------------------------------------------- %
\newlength{\@arrow@argument@aboveshift}           % Verschieben der Beschriftung
\newlength{\@arrow@argument@belowshift}           % Verschieben der Beschriftung
\define@key{arrow}{aboveshift}[2pt]{%
  \setlength{\@arrow@argument@aboveshift}{#1}}
\define@key{arrow}{belowshift}[2pt]{%
  \setlength{\@arrow@argument@belowshift}{#1}}
\define@key{arrow}{both}[]{
  \renewcommand{\@arrow@argument@both}{true}
}

% Anker ------------------------------------------------------------------------------- %
\newcommand{\@arrow@name}{}                      % Anker
\newcommand{\@arrow@currentname}{\@arrow@name}   %
\define@key{arrow}{name}[\@arrow@name]{%         %
    \renewcommand{\@arrow@currentname}{#1}}%     %

% Richtung ---------------------------------------------------------------------------- %
\define@key{arrow}{direction}[\@arrow@direction]{%
    \renewcommand{\@arrow@currentdirection}{#1}  %
    \ifthenelse{\equal{#1}{right}}{% nach rechts, Einstellungen:
     \renewcommand{\@arrow@end}{\@arrow@length@actual,0}
     \renewcommand{\@arrow@currentabove}{above}
     \renewcommand{\@arrow@currentbelow}{below}
     \renewcommand{\@arrow@above@anchor}{south}
     \renewcommand{\@arrow@below@anchor}{north}
     \renewcommand{\@arrow@argument@anchor}{
       \node(@arrow@center) at ($(\tikzchainprevious.east)+(.5\@arrow@length@actual+\@rxn@node@distance,0)$) {};
     }
     \renewcommand{\@arrow@argument@angle}{0}
     \renewcommand{\@arrow@rotate}{0}
     \renewcommand{\@arrow@start@up}{0,0.03}
     \renewcommand{\@arrow@start@down}{0,-0.03}
     \renewcommand{\@arrow@cross@base@x}{.15em}
     \renewcommand{\@arrow@cross@base@y}{0}
     \renewcommand{\@arrow@cross@angle}{60}
     \renewcommand{\@arrow@cross@length}{.5em}
    }{
     \ifthenelse{\equal{#1}{above right}}{% nach oben rechts, Einstellungen:
      \renewcommand{\@arrow@end}{\@arrow@length@actual,\@arrow@length@actual}
      \renewcommand{\@arrow@currentabove}{above left}
      \renewcommand{\@arrow@currentbelow}{below right}
      \renewcommand{\@arrow@above@anchor}{south}
      \renewcommand{\@arrow@below@anchor}{north}
      \renewcommand{\@arrow@argument@anchor}{
        \node(@arrow@center) at ($(\tikzchainprevious.north east)+(.5\@arrow@length@actual+.707\@rxn@node@distance,.5\@arrow@length@actual+.707\@rxn@node@distance)$) {};
      }
      \renewcommand{\@arrow@argument@angle}{45}
      \renewcommand{\@arrow@rotate}{45}
      \renewcommand{\@arrow@start@up}{-0.02,0.02}
      \renewcommand{\@arrow@start@down}{0.02,-0.02}
      \renewcommand{\@arrow@cross@base@x}{.1em}
      \renewcommand{\@arrow@cross@base@y}{.1em}
      \renewcommand{\@arrow@cross@angle}{-15}
      \renewcommand{\@arrow@cross@length}{.7em}
     }{
     \ifthenelse{\equal{#1}{above}}{% nach oben, Einstellungen:
      \renewcommand{\@arrow@end}{0,\@arrow@length@actual}
      \renewcommand{\@arrow@currentabove}{left}
      \renewcommand{\@arrow@currentbelow}{right}
      \renewcommand{\@arrow@above@anchor}{east}
      \renewcommand{\@arrow@below@anchor}{west}
      \renewcommand{\@arrow@argument@anchor}{
       \node(@arrow@center) at ($(\tikzchainprevious.north)+(0,.5\@arrow@length@actual+\@rxn@node@distance)$) {};
      }
      \renewcommand{\@arrow@argument@angle}{90}
      \renewcommand{\@arrow@rotate}{0}
      \renewcommand{\@arrow@start@up}{-0.03,0}
      \renewcommand{\@arrow@start@down}{0.03,0}
      \renewcommand{\@arrow@cross@base@x}{0}
      \renewcommand{\@arrow@cross@base@y}{.15em}
      \renewcommand{\@arrow@cross@angle}{30}
      \renewcommand{\@arrow@cross@length}{.5em}
      }{
       \ifthenelse{\equal{#1}{above left}}{% nach oben links, Einstellungen:
        \renewcommand{\@arrow@end}{-\@arrow@length@actual,\@arrow@length@actual}
        \renewcommand{\@arrow@currentabove}{above right}
        \renewcommand{\@arrow@currentbelow}{below left}
        \renewcommand{\@arrow@above@anchor}{south}
        \renewcommand{\@arrow@below@anchor}{north}
        \renewcommand{\@arrow@argument@anchor}{
          \node(@arrow@center) at ($(\tikzchainprevious.north west)+(-.5\@arrow@length@actual-.707\@rxn@node@distance,.5\@arrow@length@actual+.707\@rxn@node@distance)$) {};
        }
        \renewcommand{\@arrow@argument@angle}{-45}
        \renewcommand{\@arrow@rotate}{-45}
        \renewcommand{\@arrow@start@up}{-0.02,-0.02}
        \renewcommand{\@arrow@start@down}{0.02,0.02}
        \renewcommand{\@arrow@cross@base@x}{-.1em}
        \renewcommand{\@arrow@cross@base@y}{.1em}
        \renewcommand{\@arrow@cross@angle}{15}
        \renewcommand{\@arrow@cross@length}{.7em}
       }{
       \ifthenelse{\equal{#1}{left}}{% nach links, Einstellungen:
        \renewcommand{\@arrow@end}{-\@arrow@length@actual,0}
        \renewcommand{\@arrow@currentabove}{above}
        \renewcommand{\@arrow@currentbelow}{below}
        \renewcommand{\@arrow@above@anchor}{south}
        \renewcommand{\@arrow@below@anchor}{north}
        \renewcommand{\@arrow@argument@anchor}{
          \node(@arrow@center) at ($(\tikzchainprevious.west)+(-.5\@arrow@length@actual-\@rxn@node@distance,0)$) {};
        }
        \renewcommand{\@arrow@argument@angle}{0}
        \renewcommand{\@arrow@rotate}{0}
        \renewcommand{\@arrow@start@up}{0,-0.03}
        \renewcommand{\@arrow@start@down}{0,0.03}
        \renewcommand{\@arrow@cross@base@x}{.15em}
        \renewcommand{\@arrow@cross@base@y}{0}
        \renewcommand{\@arrow@cross@angle}{60}
        \renewcommand{\@arrow@cross@length}{.5em}
       }{
        \ifthenelse{\equal{#1}{below left}}{% nach unten links, Einstellungen:
         \renewcommand{\@arrow@end}{-\@arrow@length@actual,-\@arrow@length@actual}
         \renewcommand{\@arrow@currentabove}{above left}
         \renewcommand{\@arrow@currentbelow}{below right}
         \renewcommand{\@arrow@above@anchor}{south}
         \renewcommand{\@arrow@below@anchor}{north}
         \renewcommand{\@arrow@argument@anchor}{
           \node(@arrow@center) at ($(\tikzchainprevious.south west)+(-.5\@arrow@length@actual-.707\@rxn@node@distance,-.5\@arrow@length@actual-.707\@rxn@node@distance)$) {};
         }
         \renewcommand{\@arrow@argument@angle}{45}
         \renewcommand{\@arrow@rotate}{45}
         \renewcommand{\@arrow@start@up}{0.02,-0.02}
         \renewcommand{\@arrow@start@down}{-0.02,0.02}
         \renewcommand{\@arrow@cross@base@x}{.1em}
         \renewcommand{\@arrow@cross@base@y}{.1em}
         \renewcommand{\@arrow@cross@angle}{-15}
         \renewcommand{\@arrow@cross@length}{.7em}
        }{
         \ifthenelse{\equal{#1}{below}}{% nach unten, Einstellungen:
          \renewcommand{\@arrow@end}{0,-\@arrow@length@actual}
          \renewcommand{\@arrow@currentabove}{left}
          \renewcommand{\@arrow@currentbelow}{right}
          \renewcommand{\@arrow@above@anchor}{east}
          \renewcommand{\@arrow@below@anchor}{west}
          \renewcommand{\@arrow@argument@anchor}{
            \node(@arrow@center) at ($(\tikzchainprevious.south)+(0,-.5\@arrow@length@actual-\@rxn@node@distance)$) {};
          }
          \renewcommand{\@arrow@argument@angle}{90}
          \renewcommand{\@arrow@rotate}{0}
          \renewcommand{\@arrow@start@up}{0.03,0}
          \renewcommand{\@arrow@start@down}{-0.03,0}
          \renewcommand{\@arrow@cross@base@x}{0}
          \renewcommand{\@arrow@cross@base@y}{.15em}
          \renewcommand{\@arrow@cross@angle}{30}
          \renewcommand{\@arrow@cross@length}{.5em}
         }{
          \ifthenelse{\equal{#1}{below right}}{% nach unten rechts, Einstellungen:
           \renewcommand{\@arrow@end}{\@arrow@length@actual,-\@arrow@length@actual}
           \renewcommand{\@arrow@currentabove}{above right}
           \renewcommand{\@arrow@currentbelow}{below left}
           \renewcommand{\@arrow@above@anchor}{south}
           \renewcommand{\@arrow@below@anchor}{north}
           \renewcommand{\@arrow@argument@anchor}{
             \node(@arrow@center) at ($(\tikzchainprevious.south east)+(.5\@arrow@length@actual+.707\@rxn@node@distance,-.5\@arrow@length@actual-.707\@rxn@node@distance)$) {};
           }
           \renewcommand{\@arrow@argument@angle}{-45}
           \renewcommand{\@arrow@rotate}{-45}
           \renewcommand{\@arrow@start@up}{0.02,0.02}
           \renewcommand{\@arrow@start@down}{-0.02,-0.02}
           \renewcommand{\@arrow@cross@base@x}{-.1em}
           \renewcommand{\@arrow@cross@base@y}{.1em}
           \renewcommand{\@arrow@cross@angle}{15}
           \renewcommand{\@arrow@cross@length}{.7em}
          }{
           \PackageError{mychemistry}{Unknown arrow direction}% oder Fehler
          }
         }
        }
       }
      }
     }
    }
   }
}

% Pfeiltyp ---------------------------------------------------------------------------- %
\newcommand{\@arrow@drawing}{}
\define@key{arrow}{type}[\@arrow@type]{%
  \renewcommand{\@arrow@currenttype}{#1}
  \ifthenelse{\equal{#1}{->}}{
  \renewcommand{\@arrow@drawing}{\draw[->,thick,text height=0] (0,0) -- node[pos=.5](@arrow@center){} ++(\@arrow@end);}
  }{
    \ifthenelse{\equal{#1}{<->}}{
    \renewcommand{\@arrow@drawing}{\draw[<->,thick,text height=0] (0,0) -- node[pos=.5](@arrow@center){} ++(\@arrow@end);}
    }{
      \ifthenelse{\equal{#1}{<-}}{
      \renewcommand{\@arrow@drawing}{\draw[<-,thick,text height=0] (0,0) -- node[pos=.5](@arrow@center){} ++(\@arrow@end);}
      }{
        \ifthenelse{\equal{#1}{<=>}}{
        \renewcommand{\@arrow@drawing}{\draw[-left to,thick,text height=0] (\@arrow@start@up) -- ++(\@arrow@end);\draw[left to-,thick,text height=0] (\@arrow@start@down) -- ++(\@arrow@end);\draw[draw=none] (0,0) -- node[pos=.5](@arrow@center){} ++(\@arrow@end);}
        }{
          \ifthenelse{\equal{#1}{-|>}}{
           \setlength{\@arrow@argument@aboveshift}{.75em}
           \setlength{\@arrow@argument@belowshift}{.75em}
           \renewcommand{\@arrow@cross@draw}{\draw[thick] ($(@arrow@center.base) + (\@arrow@cross@base@x,\@arrow@cross@base@y) + (\@arrow@cross@angle:\@arrow@cross@length)$) -- ($(@arrow@center.base) + (\@arrow@cross@base@x,\@arrow@cross@base@y) + (\@arrow@cross@angle-180:\@arrow@cross@length)$);\draw[thick] ($(@arrow@center.base) + (-\@arrow@cross@base@x,-\@arrow@cross@base@y) + (\@arrow@cross@angle:\@arrow@cross@length)$) -- ($(@arrow@center.base) + (-\@arrow@cross@base@x,-\@arrow@cross@base@y) + (\@arrow@cross@angle-180:\@arrow@cross@length)$);}
           \renewcommand{\@arrow@drawing}{\draw[->,thick,text height=0] (0,0) -- node[pos=.5](@arrow@center){} ++(\@arrow@end);\@arrow@cross@draw}
          }{
           \PackageError{mychemistry}{Unknown arrow type}% oder Fehler
          }
        }
      }
    }
  }
}%
% ------------------------------------------------------------------------------------- %
% Maße der Nodes der Pfeilargumente bestimmen, größere später an Pfeil weitergeben
\newlength{\@mc@height@A}
\newlength{\@mc@height@B}
\newlength{\@arrow@argument@height}
\newlength{\@mc@width@A}
\newlength{\@mc@width@B}
\newlength{\@arrow@argument@width}
\newcommand{\@mc@savenode}{%
    \global\let\@mc@minx=\pgfpositionnodelaterminx%
    \global\let\@mc@maxx=\pgfpositionnodelatermaxx%
    \global\let\@mc@miny=\pgfpositionnodelaterminy%
    \global\let\@mc@maxy=\pgfpositionnodelatermaxy%
}%
% der nächste Befehl benötigt pgf v2.10!
\newcommand{\@mc@largernode}[2]{
  \@ifundefined{pgfpositionnodelater}{
    \PackageError{mychemistry}{The command \string\pgfpositionnodelater is undefined. Make sure, you have installed the latest version of pgf.}
  }{}
  {\pgfpositionnodelater{\@mc@savenode}
  \node{#1};
  \setlength{\@mc@width@A}{\@mc@maxx}%
  \addtolength{\@mc@width@A}{-\@mc@minx}
  \setlength{\@mc@height@A}{\@mc@maxy}%
  \addtolength{\@mc@height@A}{-\@mc@miny}
  \node{#2};
  \setlength{\@mc@width@B}{\@mc@maxx}%
  \addtolength{\@mc@width@B}{-\@mc@minx}
  \setlength{\@mc@height@B}{\@mc@maxy}%
  \addtolength{\@mc@height@B}{-\@mc@miny}
  \pgfmathsetlength{\@arrow@argument@width}{max(\@mc@width@A,\@mc@width@B)}
  \pgfmathsetlength{\@arrow@argument@height}{max(\@mc@height@A,\@mc@height@B)}
  \global\@arrow@argument@width=\@arrow@argument@width
  \global\@arrow@argument@height=\@arrow@argument@height
  }
}

% Pfeil zeichnen ---------------------------------------------------------------------- %
\newcommand{\@arrow}[3][]{
 \renewcommand{\@arrow@length@factor}{\@arrow@currentlength}
 \setlength{\@arrow@length@actual}{\@arrow@currentlength\@arrow@length@default}
 \ifthenelse{\equal{\@arrow@argument@both}{true}}{
   \@mc@largernode{#2}{#3}
 }{
   \setlength{\@arrow@argument@width}{0pt}
   \setlength{\@arrow@argument@height}{0pt}
 }
 \draw node[on chain=going \@arrow@currentdirection](\@arrow@currentname) {\tikz{
   \@arrow@drawing
   }
 };
 \@arrow@argument@anchor
 \draw node[anchor=\@arrow@above@anchor,rotate=\@arrow@rotate,minimum width=\@arrow@argument@width, minimum height=\@arrow@argument@height]
  at ($(@arrow@center.base)+(90+\@arrow@argument@angle:\@arrow@argument@aboveshift)$) {#2};
 \draw node[anchor=\@arrow@below@anchor,rotate=\@arrow@rotate,minimum width=\@arrow@argument@width, minimum height=\@arrow@argument@height]
  at ($(@arrow@center.base)+(-90+\@arrow@argument@angle:\@arrow@argument@belowshift)$) {#3};
 \setlength{\@arrow@argument@aboveshift}{2pt}
 \setlength{\@arrow@argument@belowshift}{2pt}
}

% Voreinstellungen:
\presetkeys%
  {arrow}
  {direction=\@arrow@direction,length=\@arrow@length,type=\@arrow@type,aboveshift=\@arrow@argument@shift,belowshift=\@arrow@argument@shift}
  {}

% Eigentlicher Befehl:
\providecommand{\arrow}[3][]{
 \setkeys{arrow}{#1}
 \@arrow{#2}{#3}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MERGE ------------------------------------------------------------------------------- %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\@merge@direction}{below}
\newcommand{\@merge@to}{}
\newcommand{\@merge@from@one}{}
\newcommand{\@merge@from@two}{}
\newcommand{\@merge}[3]{
  \renewcommand{\@merge@to}{#1}
  \renewcommand{\@merge@from@one}{#2}
  \renewcommand{\@merge@from@two}{#3}
}
% Basisbefehle ------------------------------------------------------------------------ %
\newcommand{\@merge@right}{
 \draw[stealth-,thick] ($(\@merge@to.west)+(-1em,0)$) -- ($(\@merge@to.west)+(-4em,0)$) node(crossing) {};
 \draw[thick] ($(\@merge@from@one.east)+(1em,0)$) -| (crossing.base);
 \draw[thick] ($(\@merge@from@two.east)+(1em,0)$) -| (crossing.base);
}
\newcommand{\@merge@above}{
 \draw[stealth-,thick] ($(\@merge@to.south)+(0,-1em)$) -- ($(\@merge@to.south)+(0,-4em)$) node(crossing) {};
 \draw[thick] (crossing.base) -| ($(\@merge@from@one.north)+(0,1em)$);
 \draw[thick] (crossing.base) -| ($(\@merge@from@two.north)+(0,1em)$);
}
\newcommand{\@merge@left}{
 \draw[stealth-,thick] ($(\@merge@to.east)+(1em,0)$) -- ($(\@merge@to.east)+(4em,0)$) node(crossing) {};
 \draw[thick] ($(\@merge@from@one.west)+(-1em,0)$) -| (crossing.base);
 \draw[thick] ($(\@merge@from@two.west)+(-1em,0)$) -| (crossing.base);;
}
\newcommand{\@merge@below}[3]{
 \draw[stealth-,thick] ($(\@merge@to.north)+(0,1em)$) -- ($(\@merge@to.north)+(0,4em)$) node(crossing) {};
 \draw[thick] (crossing.base) -| ($(\@merge@from@one.south)+(0,-1em)$);
 \draw[thick] (crossing.base) -| ($(\@merge@from@two.south)+(0,-1em)$);
}
% key zur Auswahl --------------------------------------------------------------------- %
\define@choicekey{merge}{direction}[\val\nr]{right,above,left,below}[below]{
  \ifcase\nr\relax
    \@merge@right
  \or
    \@merge@above
  \or
    \@merge@left
  \or
    \@merge@below
  \fi
}
% Voreinstellung ---------------------------------------------------------------------- %
\presetkeys%
  {merge}
  {direction=below}
  {}
% eigentlicher Befehl ----------------------------------------------------------------- %
\newcommand{\merge}[4][]{
  \@merge{#2}{#3}{#4}
  \setkeys{merge}{#1}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% REAKTIONSSCHEMA --------------------------------------------------------------------- %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newlength{\@rxn@node@distance}
\setlength{\@rxn@node@distance}{1em}
\providecommand{\setrcndist}[1]{
  \ifthenelse{\equal{#1}{}}{
    \setlength{\@rxn@node@distance}{1em}
  }{
    \setlength{\@rxn@node@distance}{#1}
  }
}
% ------------------------------------------------------------------------------------- %
% Schema ------------------------------------------------------------------------------ %
% ------------------------------------------------------------------------------------- %
\newcommand{\@mc@rs@scale}{1.0}                  % Default-Skalierung
\newcommand{\@mc@rs@currentscale}{\@mc@rs@scale} %
\providecommand{\setschemename}[1]{\renewcommand{\@mc@rs@name}{#1}}
\newcommand{\@mc@rs@caption}{}                   % Beschriftung
\newcommand{\@mc@rs@label}{}                     % Label
\newcommand{\@mc@rs@currentlabel}{\@mc@rs@label} %
\define@key{rxnscheme}{label}{%                  %
  \ifthenelse{\equal{#1}{}}{}{                   %
    \renewcommand{\@mc@rs@currentlabel}{#1}      %
  }                                              %
}                                                %
\define@key{rxnscheme}{scale}[1.0]{              % Skalierung
  \renewcommand{\@mc@rs@currentscale}{#1}        %
}                                                %
\define@key{rxnscheme}{name}{                    % Name
  \renewcommand{\@mc@rs@name}{#1}                %
}                                                %
\define@key{rxnscheme}{placement}{               % Float-Platzierung
  \renewcommand{\@mc@rs@currentplacement}{#1}    %
}                                                %
\newcommand{\@mc@rs@align}{center}               %
\newcommand{\@mc@rs@right}{right}                %
\newcommand{\@mc@rs@center}{center}              %
\newcommand{\@mc@rs@left}{left}                  %
\providecommand{\setschemealign}[1]{             %
  \ifthenelse{\equal{#1}{}}{                     %
    \renewcommand{\@mc@rs@align}{center}         %
  }{                                             %
    \renewcommand{\@mc@rs@align}{#1}             %
  }                                              %
}                                                %
\define@key{rxnscheme}{align}{                   % Alignment
  \renewcommand{\@mc@rs@align}{#1}
}
% Voreinstellungen:
%\presetkeys%
%  {rxnscheme}
%  {scale=\@mc@rs@scale,align=\@mc@rs@align,label=\@mc@rs@label}
%  {}
% Gleitumgebung
\floatstyle{ruled}
\newfloat{rxnfloat}{\@mc@rs@currentplacement}{rxnscheme}
\floatname{rxnfloat}{\@mc@rs@name}               % Name ändern
% eigentliche Umgebung:
\newenvironment{rxnscheme}[2][]{%
  \setkeys{rxnscheme}{#1}
  \renewcommand\@mc@rs@caption{#2}
  \begin{rxnfloat}
  %\centering
  \ifx\@mc@rs@align\@mc@rs@center \centering \fi
  \ifx\@mc@rs@align\@mc@rs@left \raggedright \fi
  \ifx\@mc@rs@align\@mc@rs@right \raggedleft \fi
  \@mc@atomsize\@mc@bondlength\@mc@bondshape
  \begin{tikzpicture}[every picture/.style={scale=\@mc@rs@currentscale},start chain,node distance=\@rxn@node@distance,inner sep=0pt]
}{%
  \end{tikzpicture}%
  \caption{\@mc@rs@caption}
  \ifthenelse{\equal{\@mc@rs@currentlabel}{}}{}{\label{\@mc@rs@currentlabel}}
  \end{rxnfloat}
}

% ------------------------------------------------------------------------------------- %
% einfache Reaktion ------------------------------------------------------------------- %
% ------------------------------------------------------------------------------------- %
\newcommand{\@mc@reaction@scale}{1.0}
\newcommand{\@mc@reaction@align}{center}
\providecommand{\setrxnalign}[1]{
  \ifthenelse{\equal{#1}{}}{
    \renewcommand{\@mc@reaction@align}{center}
  }{
    \renewcommand{\@mc@reaction@align}{#1}
  }
}
\newcommand{\@mc@reaction@right}{right}
\newcommand{\@mc@reaction@center}{center}
\newcommand{\@mc@reaction@left}{left}
\define@key{rxn}{scale}{
  \renewcommand{\@mc@reaction@scale}{#1}}
\define@key{rxn}{align}{
  \renewcommand{\@mc@reaction@align}{#1}}
% Voreinstellungen:
%\presetkeys%
%  {rxn}
%  {scale=\@mc@reaction@scale,align=\@mc@reaction@align}
%  {}
\newenvironment{rxn}[1][]{%
  \setkeys{rxn}{#1}
  \ifx\@mc@reaction@align\@mc@reaction@center
  \begin{center}
  \fi
  \ifx\@mc@reaction@align\@mc@reaction@left
  \begin{flushleft}
  \fi
  \ifx\@mc@reaction@align\@mc@reaction@right
  \begin{flushright}
  \fi
  \@mc@bondlength\@mc@bondshape\@mc@atomsize
  \begin{tikzpicture}[every picture/.style={scale=\@mc@reaction@scale},start chain,node distance=\@rxn@node@distance,inner sep=0pt]
}{%
  \end{tikzpicture}%
  \ifx\@mc@reaction@align\@mc@reaction@center
  \end{center}
  \fi
  \ifx\@mc@reaction@align\@mc@reaction@left
  \end{flushleft}
  \fi
  \ifx\@mc@reaction@align\@mc@reaction@right
  \end{flushright}
  \fi
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% REAKTANDEN -------------------------------------------------------------------------- %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Standardreaktand -------------------------------------------------------------------- %
\newcommand{\@mc@reactand}[3][]{\draw node[on chain=going #1](#3) {#2};}
\providecommand{\reactand}[3][right]{\@mc@reactand[#1]{#2}{#3}}

% Mesomerie --------------------------------------------------------------------------- %
\newcommand{\@mc@mesomeric}[3][]{\draw node[inner sep=1pt,on chain=going #1,left delimiter={[},right delimiter={]}](#3) {#2};}
\providecommand{\mesomeric}[3][right]{\@mc@mesomeric[#1]{\begin{tikzpicture}[start branch]#2\end{tikzpicture}}{#3}}
\providecommand{\marrow}[1][right]{\arrow[type=<->,length=.5,direction=#1]{}{}}
\providecommand{\dummy}[1][]{\draw node[on chain,xshift=-\@rxn@node@distance](#1) {};}

% Uebergangszustand ------------------------------------------------------------------- %
\newcommand\@mc@transition@name{}
\newcommand{\@mc@transition}[3][]{
  \ifthenelse{\equal{#3}{}}{\renewcommand\@mc@transition@name{a}}{\renewcommand\@mc@transition@name{#3}}
  \draw node[inner sep=1pt,on chain=going #1,left delimiter={[},right delimiter={]}](\@mc@transition@name) {#2} node at ($(\@mc@transition@name.north east)+(.25,0)$) {$\ddagger$};
}
\providecommand{\transition}[3][right]{\@mc@transition[#1]{\begin{tikzpicture}[start branch]#2\end{tikzpicture}}{#3}}

% Abzweigung -------------------------------------------------------------------------- %
\newcommand{\@mc@branch}[3][]{\node[#1] (#3) {#2};}
\providecommand{\branch}[3][on chain=going right]{\@mc@branch[#1]{\begin{tikzpicture}[start branch]#2\end{tikzpicture}}{#3}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sonstiges --------------------------------------------------------------------------- %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Elektronenverschiebungspfeile ------------------------------------------------------- %
\providecommand{\elmove}[5][->,red,shorten <=3pt,shorten >=1pt]{\chemmove{\draw[#1](#2).. controls +(#3) and +(#5)..(#4);}}
% Bindungslänge ----------------------------------------------------------------------- %
\newcommand{\@mc@bondlength}{\setatomsep{1.8em}}
\providecommand{\bondlength}[1]{
  \ifthenelse{\equal{#1}{}}{
    \renewcommand{\@mc@bondlength}{\setatomsep{1.8em}}
  }{
    \renewcommand{\@mc@bondlength}{\setatomsep{#1}}
  }
}
% Keilbindungen ----------------------------------------------------------------------- %
\newcommand{\@mc@bondshape@one}{3pt}
\newcommand{\@mc@bondshape@two}{.5pt}
\newcommand{\@mc@bondshape@three}{1pt}
\providecommand{\bondshape}[3]{
  \ifthenelse{\equal{#1}{}}{
    \renewcommand{\@mc@bondshape@one}{3pt}
  }{
    \renewcommand{\@mc@bondshape@one}{#1}
  }
  \ifthenelse{\equal{#2}{}}{
    \renewcommand{\@mc@bondshape@two}{.5pt}
  }{
    \renewcommand{\@mc@bondshape@two}{#2}
  }
  \ifthenelse{\equal{#3}{}}{
    \renewcommand{\@mc@bondshape@three}{1pt}
  }{
    \renewcommand{\@mc@bondshape@three}{#3}
  }
}
\newcommand{\@mc@bondshape}{\setcrambond{\@mc@bondshape@one}{\@mc@bondshape@two}{\@mc@bondshape@three}}
% Atomgröße --------------------------------------------------------------------------- %
\newcommand{\@mc@atomsize}{\small}
\providecommand{\atomsize}[1]{
  \ifthenelse{\equal{#1}{}}{
    \renewcommand{\@mc@atomsize}{\small}
  }{
    \renewcommand{\@mc@atomsize}{#1}
  }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% NODES SICHTBAR/UNSICHTBAR MACHEN ---------------------------------------------------- %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\makevisible{
  \renewcommand{\@mc@branch}[3][]{\node[draw=purple,dotted,##1] (##3) {##2};}
  \renewcommand{\@mc@reactand}[3][]{\draw node[draw=red!60,solid,on chain=going ##1](##3) {##2};}
  \renewcommand{\@mc@mesomeric}[3][]{\draw node[draw=red!60,solid,on chain=going ##1,left delimiter={[},right delimiter={]}](##3) {##2};}
  \renewcommand{\@arrow}[3][]{
   \renewcommand{\@arrow@length@factor}{\@arrow@currentlength}
   \setlength{\@arrow@length@actual}{\@arrow@currentlength\@arrow@length@default}
   \ifthenelse{\equal{\@arrow@argument@both}{true}}{
     \@mc@largernode{##2}{##3}
   }{
     \setlength{\@arrow@argument@width}{0pt}
     \setlength{\@arrow@argument@height}{0pt}
   }
   \draw node[solid,on chain=going \@arrow@currentdirection](\@arrow@currentname) {\tikz{
     \@arrow@drawing
     }
   };
   \@arrow@argument@anchor
   \draw node[draw=green!60,solid,anchor=\@arrow@above@anchor,rotate=\@arrow@rotate,minimum width=\@arrow@argument@width, minimum height=\@arrow@argument@height]
    at ($(@arrow@center.base)+(90+\@arrow@argument@angle:\@arrow@argument@aboveshift)$) {##2};
   \draw node[draw=blue!60,solid,anchor=\@arrow@below@anchor,rotate=\@arrow@rotate,minimum width=\@arrow@argument@width, minimum height=\@arrow@argument@height]
    at ($(@arrow@center.base)+(-90+\@arrow@argument@angle:\@arrow@argument@belowshift)$) {##3};
   \setlength{\@arrow@argument@aboveshift}{2pt}
   \setlength{\@arrow@argument@belowshift}{2pt}
  }
}

\newcommand\makeinvisible{
  \renewcommand{\@mc@reactand}[3][]{\draw node[on chain=going ##1](##3) {##2};}
  \renewcommand{\@mc@mesomeric}[3][]{\draw node[on chain=going ##1,left delimiter={[},right delimiter={]}](##3) {##2};}
  \renewcommand{\@mc@branch}[3][]{\node[##1] (##3) {##2};}
  \renewcommand{\@arrow}[3][]{
   \renewcommand{\@arrow@length@factor}{\@arrow@currentlength}
   \setlength{\@arrow@length@actual}{\@arrow@currentlength\@arrow@length@default}
   \ifthenelse{\equal{\@arrow@argument@both}{true}}{
     \@mc@largernode{##2}{##3}
   }{
     \setlength{\@arrow@argument@width}{0pt}
     \setlength{\@arrow@argument@height}{0pt}
   }
   \draw node[on chain=going \@arrow@currentdirection](\@arrow@currentname) {\tikz{
     \@arrow@drawing
     }
   };
   \@arrow@argument@anchor
   \draw node[anchor=\@arrow@above@anchor,rotate=\@arrow@rotate,minimum width=\@arrow@argument@width, minimum height=\@arrow@argument@height]
    at ($(@arrow@center.base)+(90+\@arrow@argument@angle:\@arrow@argument@aboveshift)$) {##2};
   \draw node[anchor=\@arrow@below@anchor,rotate=\@arrow@rotate,minimum width=\@arrow@argument@width, minimum height=\@arrow@argument@height]
    at ($(@arrow@center.base)+(-90+\@arrow@argument@angle:\@arrow@argument@belowshift)$) {##3};
   \setlength{\@arrow@argument@aboveshift}{2pt}
   \setlength{\@arrow@argument@belowshift}{2pt}
  }
}
% ------------------------------------------------------------------------------------- %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%