%D \module %D [ file=chem-ini, %D version=2009.05.13, %D subtitle=Chemistry, %D author=Hans Hagen, %D date=\currentdate, %D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}] %C %C This module is part of the \CONTEXT\ macro||package and is %C therefore copyrighted by \PRAGMA. See mreadme.pdf for %C details. %D This module in incomplete and experimental. Eventually this code %D will replace \PPCHTEX. \writestatus{loading}{ConTeXt Chemistry Macros / Structure} \registerctxluafile{chem-str}{1.001} % We have a slightly different interface. This is unchanged: % % \startchemical[axis=on] % \chemical[SIX,ROT2,B,R6,SUB1,FIVE,ROT1,B][1] % \stopchemical % % Here we use chemicalformula instead, so no longer a mix: % % \startchemicalformula % \chemical{H_2}{top}{bottom} % \chemical{PLUS}{top}{bottom} % \chemical{O}{top}{bottom} % \chemical{GIVES}{top}{bottom} % \chemical{H_2O}{top}{bottom} % \stopchemicalformula % % \startchemicalformula % \chemical{H_2} % \chemical{PLUS} % \chemical{O} % \chemical{GIVES} % \chemical{H_2O} % \stopchemicalformula % % The inline variant has only one argument: % % \chemical{H_2,PLUS,O,GIVES,H_2O} % todo: seven | eight | frontsix | fontfive | carbon | newmans | chair \unprotect \installcorenamespace{chemical} \installcorenamespace{chemicalsymbol} \installcorenamespace{chemicalframed} \installcorenamespace{chemicalsize} \installsimplecommandhandler \??chemical {chemical} \??chemical % no \define... \let\setupchemicals\setupchemical \unexpanded\def\setupchemicalframed {\dosingleempty\dosetupchemicalframed} \def\dosetupchemicalframed {\getparameters[\??chemicalframed]} \unexpanded\def\definechemical % is global (so we don't use the commandhandler) {\dosingleargument\chem_define} \def\chem_define[#1]#2% {\startnointerference \edef\currentdefinedchemical{#1}% \let\chemical\chem_chemical_nested \ctxlua{chemicals.undefine("#1")}% #2% flush \stopnointerference} \unexpanded\def\chem_chemical_nested {\dodoubleempty\chem_chemical_nested_indeed} \def\chem_chemical_nested_indeed[#1][#2]% {\ctxlua{chemicals.define("\currentdefinedchemical",\!!bs#1\!!es,\!!bs\detokenize{#2}\!!es)}} % chemical symbols \unexpanded\def\definechemicalsymbol {\dodoubleempty\chem_symbol_define} \def\chem_symbol_define[#1][#2]% {\setvalue{\??chemicalsymbol#1}{#2}} \unexpanded\def\chemicalsymbol[#1]% {\csname\??chemicalsymbol\ifcsname\??chemicalsymbol#1\endcsname\s!unknown\else#1\fi\endcsname} \definechemicalsymbol[\s!unknown][] % empty % size (small medium big) \edef\chemicaltoplocation{t} \edef\chemicalbotlocation{b} % \unexpanded\def\chemicaltext#1% in ppchtex we had a more clever alignment % {\usechemicalstyleandcolor\c!style\c!color % \strut % #1} % maybe also \setstrut \unexpanded\def\chemicaltext#1% {\mathematics {\usechemicalstyleandcolor\c!style\c!color \strut \ifcase\currentxfontsize\or\scriptstyle\or\scriptscriptstyle\fi #1}} \setvalue{\??chemicalsize\v!small }{\txx} \setvalue{\??chemicalsize\v!medium}{\tx} \setvalue{\??chemicalsize\v!big }{} \newtoks \everychemical \newtoks \everystructurechemical \newconditional\indisplaychemical \newtoks \t_chem_every_box \newbox \b_chem_result \newconditional\c_chem_some_text \newdimen \d_chem_width \newdimen \d_chem_height \newdimen \d_chem_depth \unexpanded\def\startchemical {\dodoubleempty\chem_start} \def\chem_start[#1][#2]% {\ifmmode\vcenter\else\vbox\fi \bgroup \dontcomplain \settrue\indisplaychemical \forgetall \ifsecondargument \doifassignmentelse{#1} {\setupcurrentchemical[#1]}% same as \currentchemical {\edef\currentchemical{#1}% \setupcurrentchemical[#2]}% \else\iffirstargument \doifassignmentelse{#1} {\setupcurrentchemical[#1]}% same as \currentchemical {\edef\currentchemical{#1}}% \fi\fi \the\everystructurechemical \setbox\b_chem_result\hbox\bgroup \ctxlua{chemicals.start { width = "\chemicalparameter\c!width", height = "\chemicalparameter\c!height", left = \chemicalparameter\c!left, right = \chemicalparameter\c!right, top = \chemicalparameter\c!top, bottom = \chemicalparameter\c!bottom, scale = "\chemicalparameter\c!scale", axis = "\chemicalparameter\c!axis", offset = "\the\dimexpr.25em\relax", } }% \startnointerference} \unexpanded\def\stopchemical {\stopnointerference \ctxlua{chemicals.stop()}% \egroup \d_chem_width \wd\b_chem_result \d_chem_height\ht\b_chem_result \d_chem_depth \dp\b_chem_result \the\t_chem_every_box \doifelsenothing{\chemicalparameter\c!frame}\chem_framed_nop\chem_framed_yes \egroup} \def\chem_framed_yes {\localframed% [\??chemicalframed]% [\c!frame=\chemicalparameter\c!frame,\c!align=\v!normal,\c!strut=\v!no]{\vbox{\box\b_chem_result\vss}}} % remove depth \def\chem_framed_nop {\localframed% [\??chemicalframed]% [\c!align=\v!normal,\c!strut=\v!no]{\vbox{\box\b_chem_result\vss}}} % remove depth \let\startstructurechemical\startchemical \let\stopstructurechemical \stopchemical \unexpanded\def\structurechemical {\dotripleempty\strc_chem_indeed} \appendtoks \let\chemical\structurechemical \to\everystructurechemical \def\strc_chem_indeed {\ifthirdargument \expandafter\strc_chem_indeed_three \else \expandafter\strc_chem_indeed_two \fi} \def\strc_chem_indeed_three[#1][#2][#3]% {\writestatus\m!chemicals{hyperlinked chemicals not yet supported}% todo reference, for the moment ignored \ctxlua{chemicals.component(\!!bs#2\!!es, \!!bs\detokenize{#3}\!!es, { % maybe also pass first two args this way rulethickness = "\the\dimexpr\chemicalparameter\c!rulethickness\relax", % todo: scaled points rulecolor = "\MPcolor{\chemicalparameter\c!rulecolor}" % we can precalculate this for speedup } ) }% \ignorespaces} \def\strc_chem_indeed_two[#1][#2]% {\ctxlua{chemicals.component(\!!bs#1\!!es,\!!bs\detokenize{#2}\!!es, { % maybe also pass first two args this way rulethickness = "\the\dimexpr\chemicalparameter\c!rulethickness\relax", % todo: scaled points rulecolor = "\MPcolor{\chemicalparameter\c!rulecolor}" % we can precalculate this for speedup } ) }% \ignorespaces} \appendtoks \setbox\b_chem_result\hbox{\raise\MPlly\box\b_chem_result}% \d_chem_width \wd\b_chem_result \d_chem_height\ht\b_chem_result \d_chem_depth \dp\b_chem_result \to \t_chem_every_box % kind of compatible, but text sizes instead of math sizes (i.e. tx is larger than scriptsize) \appendtoks \edef\chemicalbodyfont{\chemicalparameter\c!bodyfont}% public? \ifx\chemicalbodyfont\empty \switchtobodyfont[\chemicalbodyfont]% \fi \getvalue{\??chemicalsize\chemicalparameter\c!size}% % \to \everystructurechemical \to \everychemical \def\chemicaltoptext#1{\global\settrue\c_chem_some_text\gdef\m_chem_top_text{#1}\ignorespaces} \def\chemicalbottext#1{\global\settrue\c_chem_some_text\gdef\m_chem_bot_text{#1}\ignorespaces} \def\chemicalmidtext#1{\global\settrue\c_chem_some_text\gdef\m_chem_mid_text{#1}\ignorespaces} \appendtoks \let\toptext\chemicaltoptext \glet\m_chem_top_text\empty \let\bottext\chemicalbottext \glet\m_chem_bot_text\empty \let\midtext\chemicalmidtext \glet\m_chem_mid_text\empty \global\setfalse\c_chem_some_text \to \everystructurechemical \def\doaddchemicaltexts {\setbox2\hbox to \d_chem_width{\strut\hss\hbox{\strut\m_chem_mid_text}\hss}% \setbox4\hbox to \d_chem_width{\strut\hss\hbox{\strut\m_chem_top_text}\hss}% \setbox6\hbox to \d_chem_width{\strut\hss\hbox{\strut\m_chem_bot_text}\hss}% \setbox\b_chem_result\hbox \bgroup \box\b_chem_result \hskip-\d_chem_width \raise\d_chem_height\hbox{\lower\ht4\box4}% \hskip-\d_chem_width \lower.5\dimexpr\ht2-\dp2\relax\box2% \hskip-\d_chem_width \lower\d_chem_depth \hbox{\raise\dp6\box6}% \hss \egroup} % text on top of chemicals \appendtoks \ifconditional\c_chem_some_text \doaddchemicaltexts \d_chem_width \wd\b_chem_result \d_chem_height\ht\b_chem_result \d_chem_depth \dp\b_chem_result \fi \to \t_chem_every_box % todo: enspace or emspace \definechemicalsymbol[space] [\enspace\quad\enspace] \definechemicalsymbol[plus] [\enspace+\enspace] \definechemicalsymbol[minus] [\enspace-\enspace] \definechemicalsymbol[gives] [\chem_arrow_construct\xrightarrow] \definechemicalsymbol[equilibrium] [\chem_arrow_construct\xrightoverleftarrow] \definechemicalsymbol[mesomeric] [\chem_arrow_construct\xleftrightarrow] \definechemicalsymbol[opencomplex] [\mathematics{\Bigg[}] % not yet ok \definechemicalsymbol[closecomplex][\mathematics{\Bigg]}] % not yet ok \definechemicalsymbol[SPACE] [{\chemicalsymbol[space]}] \definechemicalsymbol[PLUS] [{\chemicalsymbol[plus]}] \definechemicalsymbol[MINUS] [{\chemicalsymbol[minus]}] \definechemicalsymbol[GIVES] [{\chemicalsymbol[gives]}] \definechemicalsymbol[EQUILIBRIUM] [{\chemicalsymbol[equilibrium]}] \definechemicalsymbol[MESOMERIC] [{\chemicalsymbol[mesomeric]}] \definechemicalsymbol[OPENCOMPLEX] [{\chemicalsymbol[opencomplex]}] \definechemicalsymbol[CLOSECOMPLEX][{\chemicalsymbol[closecomplex]}] \def\chem_arrow_construct#1#2#3% {\enspace \mathematics{#1% {\strut\hbox \!!spread 2\emwidth{\hss\ctxlua{chemicals.inline(\!!bs#2\!!es)}\hss}}% {\strut\hbox \!!spread 2em{\hss#2\hss}}% {\strut\hbox \!!spread 2\emwidth{\hss\ctxlua{chemicals.inline(\!!bs#3\!!es)}\hss}}}% {\strut\hbox \!!spread 2em{\hss#3\hss}}}% \enspace} % special macros (probably needs some more work) \def\chem_top_construct#1#2#3#4% {\begingroup \setbox0\hbox{\tx\setstrut\strut#3}% \setbox2\hbox{\setstrut\strut\molecule{#4}}% \setbox0\hbox{\raise\dimexpr\dp0+\ht2\relax\hbox to \wd2{#1\box0#2}}% % no: \smashbox0 \hbox{\box0\box2}% \endgroup}% \def\chem_bottom_construct#1#2#3#4% {\begingroup \setbox0\hbox{\tx\setstrut\strut#3}% \setbox2\hbox{\setstrut\strut#4}% \setbox0\hbox{\lower\dimexpr\dp2+\ht0\relax\hbox to \wd2{#1\box0#2}}% % no: \smashbox0 \hbox{\box0\box2}% \endgroup}% \unexpanded\def\chemicalleft#1#2% {\begingroup \hbox{\llap{\tx\setstrut\strut#1}\setstrut\strut#2}% \endgroup}% \unexpanded\def\chemicalright#1#2% {\begingroup \hbox{\setstrut\strut#2\rlap{\tx\setstrut\strut#1}}% \endgroup}% \unexpanded\def\chemicaltop {\chem_top_construct \hss \hss } \unexpanded\def\chemicallefttop {\chem_top_construct \relax \hss } \unexpanded\def\chemicalrighttop {\chem_top_construct \hss \relax} \unexpanded\def\chemicalbottom {\chem_bottom_construct \hss \hss } \unexpanded\def\chemicalleftbottom {\chem_bottom_construct \relax \hss } \unexpanded\def\chemicalrightbottom {\chem_bottom_construct \hss \relax} \unexpanded\def\chemicaltopleft #1{\chemicalleft {\chemicalrighttop {#1}{}}} \unexpanded\def\chemicalbottomleft #1{\chemicalleft {\chemicalrightbottom{#1}{}}} \unexpanded\def\chemicaltopright #1{\chemicalright{\chemicallefttop {#1}{}}} \unexpanded\def\chemicalbottomright #1{\chemicalright{\chemicalleftbottom {#1}{}}} % \unexpanded\def\chemicalcentered #1{\hbox to \fontcharwd\font`C{\setstrut\strut\hss#1\hss}} % \unexpanded\def\chemicalleftcentered #1{\hbox to \fontcharwd\font`C{\setstrut\strut #1\hss}} % \unexpanded\def\chemicalrightcentered#1{\hbox to \fontcharwd\font`C{\setstrut\strut\hss#1}} % \let\chemicalsmashedmiddle\chemicalcentered % \let\chemicalsmashedleft \chemicalleftcentered % \let\chemicalsmashedright \chemicalrightcentered \unexpanded\def\chemicalalignedtext#1#2#3% {\dontleavehmode \begingroup \usechemicalstyleandcolor\c!style\c!color \hbox to \fontcharwd\font`C{\setstrut\strut#1\molecule{#3}#2}% \endgroup} \unexpanded\def\chemicalcentered {\chemicalalignedtext\hss \hss } \unexpanded\def\chemicalleftcentered {\chemicalalignedtext\relax\hss } \unexpanded\def\chemicalrightcentered{\chemicalalignedtext\hss \relax} \let\chemicalsmashedmiddle\chemicalcentered \let\chemicalsmashedleft \chemicalleftcentered \let\chemicalsmashedright \chemicalrightcentered \unexpanded\def\chemicaloxidation#1#2#3% {\chemicaltop{\txx\ifcase#2\relax0\else#1\convertnumber{I}{#2}\fi}{#3}} \unexpanded\def\chemicaloxidationplus {\dotriplegroupempty\chemicaloxidation{\textplus }} % {} needed! \unexpanded\def\chemicaloxidationminus{\dotriplegroupempty\chemicaloxidation{\textminus}} % {} needed! \unexpanded\def\chemicalforeveropen {\dotriplegroupempty\chemicalleft {$\big[$}} % {} needed! \unexpanded\def\chemicalforeverclose {\dotriplegroupempty\chemicalright {$\big]$}} % {} needed! \unexpanded\def\chemicaloxidationone {\chemicaloxidation\relax1} \unexpanded\def\chemicaloxidationtwo {\chemicaloxidation\relax2} \unexpanded\def\chemicaloxidationthree{\chemicaloxidation\relax3} \unexpanded\def\chemicaloxidationfour {\chemicaloxidation\relax4} \unexpanded\def\chemicaloxidationfive {\chemicaloxidation\relax5} \unexpanded\def\chemicaloxidationsix {\chemicaloxidation\relax6} \unexpanded\def\chemicaloxidationseven{\chemicaloxidation\relax7} \appendtoks \let \+\chemicaloxidationplus \let \-\chemicaloxidationminus \let \[\chemicalforeveropen \let \]\chemicalforeverclose \let \1\chemicaloxidationone \let \2\chemicaloxidationtwo \let \3\chemicaloxidationthree \let \4\chemicaloxidationfour \let \5\chemicaloxidationfive \let \6\chemicaloxidationsix \let \7\chemicaloxidationseven \let \X\chemicaltighttext \let \T\chemicaltop \let \B\chemicalbottom \let \L\chemicalleft \let\LC\chemicalleftcentered \let \R\chemicalright \let\RC\chemicalrightcentered \let\TL\chemicaltopleft \let\BL\chemicalbottomleft \let\TR\chemicaltopright \let\BR\chemicalbottomright \let\LT\chemicallefttop \let\LB\chemicalleftbottom \let\RT\chemicalrighttop \let\RB\chemicalrightbottom \let\SL\chemicalsmashedleft \let\SM\chemicalsmashedmiddle \let\SR\chemicalsmashedright \to \everychemical \appendtoks \the\everychemical \to \everystructurechemical % inline \unexpanded\def\chemical {\ifinformula \expandafter\displaychemical \else \expandafter\inlinechemical \fi} \unexpanded\def\displaychemical {\dotriplegroupempty\chem_display} \def\chem_display#1#2#3% todo: {\the\everychemical \everychemical\emptytoks \quad \vcenter\bgroup \usechemicalstyleandcolor\c!style\c!color \ifthirdargument \ifsecondargument \halign{&\hss##\hss\cr#2\cr\molecule{#1}\cr#3\cr}% \else \halign{&\hss##\hss\cr\molecule{#1}\cr#2\cr}% \fi \else \hbox{\molecule{#1}}% \fi \egroup \quad} \unexpanded\def\inlinechemical#1% {\dontleavehmode \hbox{\usechemicalstyleandcolor\c!style\c!color\ctxlua{chemicals.inline(\!!bs#1\!!es)}}} \unexpanded\def\chemicalbondrule {\hbox{\vrule\!!height.75ex\!!depth-\dimexpr.75ex-\linewidth\relax\!!width1em\relax}} \definechemicalsymbol[i:space] [\enspace\quad\enspace] \definechemicalsymbol[i:plus] [\enspace\mathematics{+}\enspace] \definechemicalsymbol[i:minus] [\enspace\mathematics{-}\enspace] \definechemicalsymbol[i:gives] [\enspace\mathematics{\xrightarrow{}{}}\enspace] \definechemicalsymbol[i:equilibrium] [\enspace\mathematics{\xrightpverleftarrow{}{}}\enspace] \definechemicalsymbol[i:mesomeric] [\enspace\mathematics{\xleftrightarrow{}{}}\enspace] \definechemicalsymbol[i:single] [\chemicalbondrule] \definechemicalsymbol[i:tripple] [\hbox{\lower.5ex\chemicalbondrule\hskip-1em\raise.5ex\chemicalbondrule}] \definechemicalsymbol[i:double] [\hbox{\chemicalbondrule\hskip-1em\lower.5ex\chemicalbondrule\hskip-1em\raise.5ex\chemicalbondrule}] \unexpanded\def\chemicalsinglebond {\chemicalsymbol[i:single]} \unexpanded\def\chemicaldoublebond {\chemicalsymbol[i:tripple]} \unexpanded\def\chemicaltriplebond {\chemicalsymbol[i:double]} \unexpanded\def\chemicalgives {\chemicalsymbol[i:gives]} \unexpanded\def\chemicalmesomeric {\chemicalsymbol[i:mesomeric]} \unexpanded\def\chemicalequilibrium{\chemicalsymbol[i:equilibrium]} \unexpanded\def\chemicalsplus {\chemicalsymbol[i:plus]} \unexpanded\def\chemicalsminus {\chemicalsymbol[i:minus]} \unexpanded\def\chemicalsspace {\chemicalsymbol[i:space]} \unexpanded\def\chemicalinline #1{#1} % display \newconditional\c_chem_has_top \newconditional\c_chem_has_bot \newtoks\t_chem_top \newtoks\t_chem_mid \newtoks\t_chem_bot \newif\ifinchemicalformula \unexpanded\def\startchemicalformula {\mathortext\vcenter\vbox\bgroup \forgetall \inchemicalformulatrue \the\everychemical \everychemical\emptytoks \t_chem_top\emptytoks % not needed \t_chem_mid\emptytoks % not needed \t_chem_bot\emptytoks % not needed \let\chemical\formulachemical \setfalse\c_chem_has_top \setfalse\c_chem_has_bot} \unexpanded\def\stopchemicalformula {\tabskip1em\relax \nointerlineskip \ifconditional\c_chem_has_top \ifconditional\c_chem_has_bot \halign{&\hss\usechemicalstyleandcolor\c!style\c!color##\hss\cr\the\t_chem_top\cr\the\t_chem_mid\cr\the\t_chem_bot\cr}% \else \halign{&\hss\usechemicalstyleandcolor\c!style\c!color##\hss\cr\the\t_chem_top\cr\the\t_chem_mid\cr}% \fi \else \ifconditional\c_chem_has_bot \halign{&\hss\usechemicalstyleandcolor\c!style\c!color##\hss\cr\the\t_chem_mid\cr\the\t_chem_bot\cr}% \else \halign{&\hss\usechemicalstyleandcolor\c!style\c!color##\hss\cr\the\t_chem_mid\cr}% \fi \fi \egroup} \unexpanded\def\formulachemical {\relax\dotriplegroupempty\chem_formula} \def\chem_formula#1#2#3% we could do hboxes and measure {\ifcsname\??chemicalsymbol\detokenize{#1}\endcsname \t_chem_mid\expandafter{\the\t_chem_mid\chem_formula_mid{#1}{#2}{#3}}% \else \ifthirdargument \doifelsenothing{#2}\chem_formula_top_nop{\chem_formula_top_yes{#2}}% \doifelsenothing{#3}\chem_formula_bot_nop{\chem_formula_bot_yes{#3}}% \else\ifsecondargument \chem_formula_top_nop \doifelsenothing{#2}\chem_formula_bot_nop{\chem_formula_bot_yes{#2}}% \else \chem_formula_top_nop \chem_formula_bot_nop \fi\fi \t_chem_mid\expandafter{\the\t_chem_mid\molecule{#1}&}% \fi} \def\chem_formula_mid#1% {\csname\??chemicalsymbol\detokenize{#1}\endcsname} \def\chem_formula_top_nop {\t_chem_top\expandafter{\the\t_chem_top&}} \def\chem_formula_bot_nop {\t_chem_bot\expandafter{\the\t_chem_bot&}} \def\chem_formula_top_yes#1{\t_chem_top\expandafter{\the\t_chem_top\chem_formula_top_indeed{#1}&}\settrue\c_chem_has_top} \def\chem_formula_bot_yes#1{\t_chem_bot\expandafter{\the\t_chem_bot\chem_formula_bot_indeed{#1}&}\settrue\c_chem_has_bot} \def\chem_formula_top_indeed#1{\strut#1} \def\chem_formula_bot_indeed#1{\strut#1} % gone: state option resolution offset (now frame offset) alternative \setupchemicalframed [\c!align=\v!normal, \c!strut=\v!no, \c!offset=\v!overlay, \c!frame=off] \setupchemical [\c!frame=, \c!width=0, \c!height=0, \c!left=0, \c!right=0, \c!top=0, \c!bottom=0, \c!bodyfont=, \c!scale=\v!medium, \c!size=\v!medium, \c!textsize=\v!big, \c!axis=\v!off, \c!style=\rm, \c!location=, \c!color=, \c!rulethickness=\linewidth, \c!rulecolor=, \c!factor=1] \protect \endinput