%
% OCHEM.STY  - Stilfile zum Erzeugen von chemischen Formeln in LaTeX
% (c) 1993-1996,1999-2000 by Ingo Kloeckl
%
% 11.09.1993  error handling improved
% 09.12.1993  neue befehle fuer orbitallappen und geb. bindungen
% 04.10.1994  griechische symbole in texten
% 14.02.1996  environment ochem ersetzt durch \ochem-Befehl
% 15.02.1996  supermini eingefuehrt, die gfbox in htshow... geloescht
% 20.09.1996  zentrierungsmoeglichkeit durch ochem als \hbox, wertet nun
%               bounding box info aus.
% 21.09.1996  Trennung zwischen fixen und gleitfaehigen Objekten, verursachen
%               nun keine Verwirrungen mehr.
% 20.10.1996  Korrekte Bestimmung der BBox (/erg), npr ok
% 21.10.1996  Verbesserung BBox der Glasgeraete
% -------------------
% 22.03.1999  Version 3 beta release: schema compiler in Perl 
% 11.04.1999  Version 3 consolidation
% 02.05.1999  fshow (PS command) removed due to interference with MetaPost
% 09.01.2000  new bond shapes >> and >. and b
% 19.01.2000  new bond shape ~
% 19.04.2000  undesired spaces inhibited (thanks to B. Seckinge)
% 10.04.2001  option 'separate' added to easily produce EPS figures
%

\newcommand{\chemversion}{OCHEM Version 3-0e 2001-04-10 }
\typeout{package \chemversion}
\typeout{(c) 1993-1996, 1999 -- 2001 Ingo Kloeckl}

\RequirePackage[dvips]{color}
\RequirePackage{verbatim}
\RequirePackage{ifthen}

\newboolean{separate}\setboolean{separate}{false}
\DeclareOption{separate}{%
  \setboolean{separate}{true}
  \newwrite\verb@chemnames
  \immediate\openout\verb@chemnames \jobname.names
}
\ProcessOptions\relax

% user command for chem font setting
\newcommand{\chemfont}[1]
 {\renewcommand{\chemfontname}{#1}}
% default font is sans serif
\newcommand{\chemfontname}{\sffamily}

%
% Erzeugt gleitfaehiges Reaktionsschema mit ldf. Nummer und Labelmoeglichkeit
% \begin{schema[*]}
%   \ochem, \caption, \label...
% \end{schema[*]}
%
\@ifundefined{chapter}
  {\newcounter{schema}
   \renewcommand{\theschema}{\arabic{schema}}}
  {\newcounter{schema}[chapter]
   \renewcommand{\theschema}{\thechapter--\arabic{schema}}}
\newcommand{\fps@schema}{htbp}
\newcommand{\ftype@schema}{5}
\newcommand{\ext@schema}{lor}
\newcommand{\fnum@schema}{\schemaname~\theschema}
\newcommand{\schemaname}{Schema}
\newcommand{\lorname}{Die Reaktionsschemata}
\newcommand{\l@schema}{\@dottedtocline{1}{1.5em}{2.3em}}

\newcommand{\tableofschemes}
 {\@restonecolfalse
  \if@twocolumn\@restonecoltrue\onecolumn\fi
  \chapter*{\lorname\@mkboth{\lorname}{\lorname}}
  {\parindent\z@\@starttoc{lor}}
  \if@restonecol\twocolumn\fi
 }

\newenvironment{schema}
 {\@float{schema}}
 {\end@float}
\newenvironment{schema*}
 {\@dblfloat{schema}}
 {\end@dblfloat}

%
%  open .chm output file for chemistry environments and install
%  font for chemical text
%
\newwrite\verb@chem
\immediate\openout\verb@chem \jobname.chm

\AtBeginDocument
 {{\chemfontname\immediate\write\verb@chem{font(\string"
  \string\usefont\string{\f@encoding\string}\string{\f@family\string}
		 \string{\f@series\string}\string{\f@shape\string}
  \string\fontsize\string{\f@size\string}\string{\f@baselineskip\string}\string\selectfont
  \string")}}
  \ifthenelse{\boolean{separate}}{\pagestyle{empty}}{}
 }

\AtEndDocument
 {\immediate\closeout\verb@chem
  \ifthenelse{\boolean{separate}}{\immediate\closeout\verb@chemnames}{}
 }

%
%  chemistry environment for placing scheme descriptions in
%  use #1 as name of TeX fragment (-> #1.ctx)
%
\newcounter{chemcnt}

\newenvironment{chemistry}[1][\arabic{chemcnt}]
 {\ifthenelse{\boolean{separate}}{\newpage}{}%
  \ifthenelse{\boolean{separate}}{\immediate\write\verb@chemnames{#1}}{}%
  \InputIfFileExists{#1.ctx}{}{\typeout{ochem: cannot find #1.ctx}}%
  \immediate\write\verb@chem{schema(\string"#1.ctx\string")}%
  \@bsphack
  \let\do\@makeother\dospecials
  \catcode`\^^M\active \catcode`\^^I=12
  \def\verbatim@processline{%
    \immediate\write\verb@chem{\the\verbatim@line}}%
  \verbatim@start}
 {\@esphack
  \immediate\write\verb@chem{endschema}%
  \stepcounter{chemcnt}}

\newenvironment{chemspecial}
 {\@bsphack
  \let\do\@makeother\dospecials
  \catcode`\^^M\active \catcode`\^^I=12
  \def\verbatim@processline{%
    \immediate\write\verb@chem{\the\verbatim@line}}%
  \verbatim@start}
 {\@esphack
 }


%
% ------------------------------------------
%    auxiliary functions and macros
% ------------------------------------------
%
\special{!
  /\par {} def
  /posx 10 dict def
  posx begin
    /c -.5 def   /bl 0 def   /b -.5 def   /br  -1 def
    /r -1  def   /tr -1 def  /t -.5 def   /tl  0 def   /l 0  def
  end 
  /posy 10 dict def
  posy begin
    /c  -.5 def  /bl 0 def   /b  0 def    /br  0  def
    /r  -.5 def  /tr  -1 def /t  -1 def   /tl -1 def   /l -.5 def
  end 
  % x y pos text ftshow -
  /ftshow { /t exch def cvn /p exch def gs translate 
	   t stringwidth exch posx p get mul exch posy p get mul moveto 
	   t show gr} def
  %
  %  abbreviations
  %
  /tr { translate } def
  /sc { scale }def
  /ro { rotate } def
  /l { lineto } def
  /s { stroke } def
  /m { moveto } def
  /np { newpath } def
  /gs { gsave } def
  /gr { grestore } def
  %
  %  different bond types
  %  phi x y len s0..s12
  %
  /sb__ { currentlinewidth .7 mul setlinewidth } def
  /b0 {  /bl exch def gsave translate rotate 
	 newpath 0 0 moveto bl 0 lineto stroke 
	 grestore } def
  /b1 {  /bl exch def gsave translate rotate 
	 gsave 1 setgray
	   newpath lw lw 1.5 mul moveto bl lw 2 mul sub 0 rlineto stroke
	   newpath lw lw -1.5 mul moveto bl lw 2 mul sub 0 rlineto stroke 
	 grestore
	 currentlinewidth 2 mul setlinewidth 
	 newpath 0 0 moveto bl 0 lineto stroke 
	 grestore } def
  /b2 {  /bl exch def gsave translate rotate 
	 newpath 0 lw -.5 mul moveto bl bw -.5 mul lineto 0 bw rlineto 
	 0 lw .5 mul lineto closepath fill 
	 grestore } def
  /b3 {  /bl exch def gsave translate rotate 
	 newpath 0 lw -.5 mul moveto bl bw -.5 mul lineto 0 bw rlineto 
	 0 lw .5 mul lineto closepath clip 
	 [ bd dup ] 0 setdash bw setlinewidth
	 newpath 0 0 moveto bl 0 lineto stroke  
	 grestore } def
  /b4 {  /bl exch def gsave translate rotate 
	 newpath 0 bw -.15 mul moveto bl bw -.15 mul lineto 
	 0 bw .3 mul rlineto 0 bw .15 mul lineto closepath clip 
	 [ bd dup ] 0 setdash bw setlinewidth
	 newpath 0 0 moveto bl 0 lineto stroke
	 grestore } def
  /b5 {  /bl exch def gsave translate rotate 
	 newpath 0 bw .5 mul moveto bl 0 rlineto 0 bw -.5 mul moveto bl 0 rlineto stroke 
	 grestore } def
  /b6 {  /bl exch def gsave translate rotate 
	 newpath 0 0 moveto bl 0 rlineto stroke 
	 sb__ .1 bl mul bw  moveto .8 bl mul 0 rlineto stroke 
	 grestore } def
  /b7 {  /bl exch def gsave translate rotate 
	 newpath 0 0 moveto bl 0 rlineto stroke 
	 sb__ .1 bl mul bw neg moveto .8 bl mul 0 rlineto stroke 
	 grestore } def
  /b8 {  /bl exch def gsave translate rotate 
	 newpath 0 0 moveto bl 0 rlineto stroke 
	 sb__  .1 bl mul bw neg moveto .8 bl mul 0 rlineto 
	 .1 bl mul bw moveto .8 bl mul 0 rlineto stroke 
	 grestore } def
  /b9 {  /bl exch def gsave translate rotate 
	 currentlinewidth 2 mul setlinewidth 1 setlinecap
	 newpath 0 0 moveto bl 0 lineto stroke 
	 grestore } def
  /b10 { /bl exch def gsave translate rotate 
	 newpath 0 0 moveto bl 0 rlineto stroke 
	 newpath bl 0 moveto bw neg bw neg rlineto 0 bw dup add rlineto closepath fill 
	 grestore } def
  /b11 { /bl exch def  gsave translate rotate 
	 newpath 0 0 moveto bl 0 rlineto stroke 
	 newpath 0 0 moveto bw bw neg rlineto 0 bw dup add rlineto closepath fill 
	 grestore } def
  /b12 { pop pop pop pop} def
  /b13 { /bl exch def gsave translate rotate 
	 newpath 0 bw -.5 mul moveto bl lw -.5 mul lineto 0 lw rlineto 
	 0 bw .5 mul lineto closepath fill 
	 grestore } def
  /b14 { /bl exch def gsave translate rotate 
	 newpath 0 bw -.5 mul moveto bl lw -.5 mul lineto 0 lw rlineto 
	 0 bw .5 mul lineto closepath clip 
	 [ bd dup ] 0 setdash bw setlinewidth
	 newpath 0 0 moveto bl 0 lineto stroke  
	 grestore } def
  /b15 { /bl exch def gsave translate rotate 
	 bw setlinewidth 1 setlinecap
	 newpath 0 0 moveto bl 0 lineto stroke 
	 grestore } def
  /b16 { /bl exch def gsave translate rotate 
         newpath 0 0 moveto 
         3 dict begin
           /n bl bw 2 mul div cvi 1 add def
           /blt bl n div def
           /bwt bw bl blt div mul .5 mul def
           n {bwt 0.3 mul bw
              bwt 0.7 mul bw neg
              bwt 0 rcurveto} repeat
         end
         stroke 
	 grestore } def
    
  %
  % arrow types
  % len phi x y ar0..ar7
  %
  /ar0 { gsave translate rotate 0 0 moveto 0 lineto stroke grestore} def
  /ar1 { gsave translate rotate 0 0 moveto dup 0 lineto stroke newpath 0 moveto aw neg dup rlineto
	  0 aw dup add rlineto closepath fill grestore} def
  /ar2 { gsave translate rotate 0 0 moveto 0 lineto stroke newpath 0 0 moveto aw aw rlineto
	  0 aw dup add neg rlineto closepath fill grestore} def
  /ar3 { gsave translate rotate 0 aw 2 div moveto dup 0 rlineto 0 aw -.5 mul moveto dup 0 rlineto stroke 
	  newpath aw 2 div moveto aw neg aw rlineto 0 aw neg rlineto closepath fill newpath
	  0 aw -.5 mul moveto aw 0 rlineto 0 aw neg rlineto closepath fill grestore} def
  /ar4 { gsave translate rotate 0 0 moveto dup 0 lineto stroke newpath 0 moveto aw neg dup rlineto
	  0 aw dup add rlineto closepath fill newpath 0 0 moveto aw aw lineto
	  0 aw -2 mul rlineto closepath fill grestore} def
  /ar5 { gsave translate rotate 0 0 moveto dup 0 lineto stroke newpath dup 0 moveto aw neg dup rlineto
	  0 aw dup add rlineto closepath fill newpath 2 div dup aw moveto aw -2 mul dup
	  rlineto aw neg moveto aw dup add dup rlineto stroke grestore} def
  /ar6 { gsave translate rotate 0 0 moveto dup 0 lineto stroke newpath 0 0 moveto aw aw rlineto
	  0 aw dup add neg rlineto closepath fill newpath 2 div dup aw moveto aw -2 mul dup
	  rlineto aw neg moveto aw dup add dup rlineto stroke grestore} def
  % phi x y arrow tip
  /atip { gsave translate rotate 
	  /maw aw 2 div def
	  newpath 0 0 moveto maw neg maw rlineto 0 maw -2 mul rlineto closepath fill
	  grestore } def
  %
  % marker types: size x y m<type>
  %
  /slw { .035 setlinewidth }def
  /m0 { gsave translate slw dup scale 0 0 .5 0 360 arc closepath stroke
	grestore } def
  /m1 { gsave translate slw dup scale 0 0 .5 0 360 arc closepath fill
	grestore } def
  /m2 { gsave translate slw dup scale -.5 -.5 moveto 1 0 rlineto 0 1 rlineto
	  -1 0 rlineto closepath stroke
	grestore } def
  /m3 { gsave translate slw dup scale -.5 -.5 moveto 1 0 rlineto 0 1 rlineto
	  -1 0 rlineto closepath fill
	grestore } def
  /m4 { gsave translate slw dup scale 0 -.5 moveto .5 0 lineto 0 .5 lineto
	  -.5 0 lineto closepath stroke
	grestore } def
  /m5 { gsave translate slw dup scale 0 -.5 moveto .5 0 lineto 0 .5 lineto
	  -.5 0 lineto closepath fill
	grestore } def
  /m6 { gsave translate slw dup scale -.5 -.5 moveto 1 0 rlineto -.5 1 rlineto
	  closepath stroke
	grestore } def
  /m7 { gsave translate slw dup scale -.5 -.5 moveto 1 0 rlineto -.5 1 rlineto
	  closepath fill
	grestore } def
  /m8 { gsave translate slw dup scale -.5 0 moveto 1 0 rlineto 
	  0 -.5 moveto 0 1 rlineto stroke
	grestore } def
  /m9 { gsave translate slw dup scale -.5 -.5 moveto .5 .5 lineto
	  -.5 .5 moveto .5 -.5 lineto stroke
	grestore } def
  /m10 { gsave translate slw dup scale -.5 -.5 moveto .5 .5 lineto
	  -.5 .5 moveto .5 -.5 lineto 0 -.5 moveto 0 .5 lineto stroke
	grestore } def
  /m11 { } def
}
