From 69d2352af4b60929b37fc49f3bdb263977016244 Mon Sep 17 00:00:00 2001 From: Marius Date: Fri, 19 Oct 2012 01:20:13 +0300 Subject: stable 2012.05.30 11:26 --- tex/context/base/chem-str.mkiv | 216 +++++++++++++---------------------------- 1 file changed, 70 insertions(+), 146 deletions(-) (limited to 'tex/context/base/chem-str.mkiv') diff --git a/tex/context/base/chem-str.mkiv b/tex/context/base/chem-str.mkiv index c4b03dd1e..1d60a293e 100644 --- a/tex/context/base/chem-str.mkiv +++ b/tex/context/base/chem-str.mkiv @@ -2,7 +2,7 @@ %D [ file=chem-ini, %D version=2009.05.13, %D subtitle=Chemistry, -%D author=Hans Hagen \& Alan Braslau, +%D author=Hans Hagen, %D date=\currentdate, %D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}] %C @@ -10,10 +10,8 @@ %C therefore copyrighted by \PRAGMA. See mreadme.pdf for %C details. -%D The original \PPCHTEX\ code was written in pure \TEX\, although later we made -%D the move from \PICTEX\ to \METAPOST\. The current implementation is a mix between -%D \TEX\, \LUA\ and \METAPOST. Although the first objective is to get a compatible -%D but better implementation, later versions might provide more, +%D This module in incomplete and experimental. Eventually this code +%D will replace \PPCHTEX. \writestatus{loading}{ConTeXt Chemistry Macros / Structure} @@ -28,24 +26,24 @@ % Here we use chemicalformula instead, so no longer a mix: % % \startchemicalformula -% \chemical{2H_2}{top}{bottom} +% \chemical{H_2}{top}{bottom} % \chemical{PLUS}{top}{bottom} -% \chemical{O_2}{top}{bottom} +% \chemical{O}{top}{bottom} % \chemical{GIVES}{top}{bottom} -% \chemical{2H_2O}{top}{bottom} +% \chemical{H_2O}{top}{bottom} % \stopchemicalformula % % \startchemicalformula -% \chemical{2H_2} +% \chemical{H_2} % \chemical{PLUS} -% \chemical{O_2} +% \chemical{O} % \chemical{GIVES} -% \chemical{2H_2O} +% \chemical{H_2O} % \stopchemicalformula % % The inline variant has only one argument: % -% \chemical{2H_2,PLUS,O_2,GIVES,2H_2O} +% \chemical{H_2,PLUS,O,GIVES,H_2O} % todo: seven | eight | frontsix | fontfive | carbon | newmans | chair @@ -60,17 +58,11 @@ \let\setupchemicals\setupchemical -%D We use a dedicated framed macro instead of inheriting one. This is both -%D a historical and practical reason (like shared keys with different meaning -%D that could clash, e.g.\ align). - -\defineframed - [\??chemicalframed] - [\c!align=\v!normal, - \c!strut=\v!no] - \unexpanded\def\setupchemicalframed - {\setupframed[\??chemicalframed]} + {\dosingleempty\dosetupchemicalframed} + +\def\dosetupchemicalframed + {\getparameters[\??chemicalframed]} \unexpanded\def\definechemical % is global (so we don't use the commandhandler) {\dosingleargument\chem_define} @@ -79,7 +71,7 @@ {\startnointerference \edef\currentdefinedchemical{#1}% \let\chemical\chem_chemical_nested - \ctxcommand{undefinechemical("#1")}% + \ctxlua{chemicals.undefine("#1")}% #2% flush \stopnointerference} @@ -87,7 +79,7 @@ {\dodoubleempty\chem_chemical_nested_indeed} \def\chem_chemical_nested_indeed[#1][#2]% - {\ctxcommand{definechemical("\currentdefinedchemical",\!!bs#1\!!es,\!!bs\detokenize{#2}\!!es)}} + {\ctxlua{chemicals.define("\currentdefinedchemical",\!!bs#1\!!es,\!!bs\detokenize{#2}\!!es)}} % chemical symbols @@ -98,15 +90,20 @@ {\setvalue{\??chemicalsymbol#1}{#2}} \unexpanded\def\chemicalsymbol[#1]% - {\csname\??chemicalsymbol\ifcsname\??chemicalsymbol#1\endcsname#1\else\s!unknown\fi\endcsname} + {\csname\??chemicalsymbol\ifcsname\??chemicalsymbol#1\endcsname\s!unknown\else#1\fi\endcsname} -\definechemicalsymbol[\s!unknown][] % \char"FFFD empty +\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 @@ -150,7 +147,7 @@ \fi\fi \the\everystructurechemical \setbox\b_chem_result\hbox\bgroup - \ctxcommand{startchemical { + \ctxlua{chemicals.start { width = "\chemicalparameter\c!width", height = "\chemicalparameter\c!height", left = \chemicalparameter\c!left, @@ -165,7 +162,7 @@ \unexpanded\def\stopchemical {\stopnointerference - \ctxcommand{stopchemical()}% + \ctxlua{chemicals.stop()}% \egroup \d_chem_width \wd\b_chem_result \d_chem_height\ht\b_chem_result @@ -174,16 +171,15 @@ \doifelsenothing{\chemicalparameter\c!frame}\chem_framed_nop\chem_framed_yes \egroup} -\unexpanded\def\chem_framed_yes - {\localframedwithsettings +\def\chem_framed_yes + {\localframed% [\??chemicalframed]% - [\c!frame=\chemicalparameter\c!frame]% - {\vbox{\box\b_chem_result\vss}}} % remove depth + [\c!frame=\chemicalparameter\c!frame,\c!align=\v!normal,\c!strut=\v!no]{\vbox{\box\b_chem_result\vss}}} % remove depth -\unexpanded\def\chem_framed_nop - {\directlocalframed +\def\chem_framed_nop + {\localframed% [\??chemicalframed]% - {\vbox{\box\b_chem_result\vss}}} % remove depth + [\c!align=\v!normal,\c!strut=\v!no]{\vbox{\box\b_chem_result\vss}}} % remove depth \let\startstructurechemical\startchemical \let\stopstructurechemical \stopchemical @@ -204,14 +200,14 @@ \def\strc_chem_indeed_three[#1][#2][#3]% {\writestatus\m!chemicals{hyperlinked chemicals not yet supported}% todo reference, for the moment ignored - \ctxcommand{chemicalcomponent(\!!bs#2\!!es, \!!bs\detokenize{#3}\!!es, { % maybe also pass first two args this way + \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]% - {\ctxcommand{chemicalcomponent(\!!bs#1\!!es,\!!bs\detokenize{#2}\!!es, { % maybe also pass first two args this way + {\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 } ) }% @@ -293,8 +289,8 @@ \def\chem_arrow_construct#1#2#3% {\enspace \mathematics{#1% - {\strut\hbox \s!spread 2\emwidth{\hss\ctxcommand{inlinechemical(\!!bs#3\!!es)}\hss}}% {\strut\hbox \s!spread 2em{\hss#3\hss}}}% - {\strut\hbox \s!spread 2\emwidth{\hss\ctxcommand{inlinechemical(\!!bs#2\!!es)}\hss}}}% {\strut\hbox \s!spread 2em{\hss#2\hss}}% + {\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) @@ -432,9 +428,9 @@ \usechemicalstyleandcolor\c!style\c!color \ifthirdargument \ifsecondargument - \halign{\aligntab\hss\alignmark\alignmark\hss\cr#2\cr\molecule{#1}\cr#3\cr}% + \halign{&\hss##\hss\cr#2\cr\molecule{#1}\cr#3\cr}% \else - \halign{\aligntab\hss\alignmark\alignmark\hss \cr\molecule{#1}\cr#2\cr}% + \halign{&\hss##\hss\cr\molecule{#1}\cr#2\cr}% \fi \else \hbox{\molecule{#1}}% @@ -444,30 +440,30 @@ \unexpanded\def\inlinechemical#1% {\dontleavehmode - \hbox{\usechemicalstyleandcolor\c!style\c!color\ctxcommand{inlinechemical(\!!bs#1\!!es)}}} + \hbox{\usechemicalstyleandcolor\c!style\c!color\ctxlua{chemicals.inline(\!!bs#1\!!es)}}} \unexpanded\def\chemicalbondrule - {\hbox{\vrule\s!height.75\exheight\s!depth-\dimexpr.75\exheight-\linewidth\relax\s!width\emwidth\relax}} + {\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{\xrightoverleftarrow{}{}}\enspace] +\definechemicalsymbol[i:equilibrium] [\enspace\mathematics{\xrightpverleftarrow{}{}}\enspace] \definechemicalsymbol[i:mesomeric] [\enspace\mathematics{\xleftrightarrow{}{}}\enspace] \definechemicalsymbol[i:single] [\chemicalbondrule] -\definechemicalsymbol[i:double] [\hbox{\lower.5ex\chemicalbondrule\hskip-1em\raise.5ex\chemicalbondrule}] -\definechemicalsymbol[i:triple] [\hbox{\chemicalbondrule\hskip-1em\lower.5ex\chemicalbondrule\hskip-1em\raise.5ex\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:double]} -\unexpanded\def\chemicaltriplebond {\chemicalsymbol[i:triple]} +\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\chemicalplus {\chemicalsymbol[i:plus]} -\unexpanded\def\chemicalminus {\chemicalsymbol[i:minus]} -\unexpanded\def\chemicalspace {\chemicalsymbol[i:space]} +\unexpanded\def\chemicalsplus {\chemicalsymbol[i:plus]} +\unexpanded\def\chemicalsminus {\chemicalsymbol[i:minus]} +\unexpanded\def\chemicalsspace {\chemicalsymbol[i:space]} \unexpanded\def\chemicalinline #1{#1} % display @@ -495,133 +491,61 @@ \setfalse\c_chem_has_bot} \unexpanded\def\stopchemicalformula - {\tabskip\emwidth\relax + {\tabskip1em\relax \nointerlineskip \ifconditional\c_chem_has_top \ifconditional\c_chem_has_bot - \halign{\aligntab\hss\usechemicalstyleandcolor\c!style\c!color\alignmark\alignmark\hss\cr\the\t_chem_top\cr\the\t_chem_mid\cr\the\t_chem_bot\cr}% + \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{\aligntab\hss\usechemicalstyleandcolor\c!style\c!color\alignmark\alignmark\hss\cr\the\t_chem_top\cr\the\t_chem_mid\cr}% + \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{\aligntab\hss\usechemicalstyleandcolor\c!style\c!color\alignmark\alignmark\hss\cr\the\t_chem_mid\cr\the\t_chem_bot\cr}% + \halign{&\hss\usechemicalstyleandcolor\c!style\c!color##\hss\cr\the\t_chem_mid\cr\the\t_chem_bot\cr}% \else - \halign{\aligntab\hss\usechemicalstyleandcolor\c!style\c!color\alignmark\alignmark\hss\cr\the\t_chem_mid\cr}% + \halign{&\hss\usechemicalstyleandcolor\c!style\c!color##\hss\cr\the\t_chem_mid\cr}% \fi \fi \egroup} -% for the moment we have a special set - -\definechemicalsymbol[d:space] [\enspace\quad\enspace] -\definechemicalsymbol[d:plus] [\enspace+\enspace] -\definechemicalsymbol[d:minus] [\enspace-\enspace] -\definechemicalsymbol[d:gives] [\rightarrowfill] % \chem_arrow_construct\xrightarrow -\definechemicalsymbol[d:equilibrium] [\rightoverleftarrowfill] % \chem_arrow_construct\xrightoverleftarrow -\definechemicalsymbol[d:mesomeric] [\leftarrowfill] % \chem_arrow_construct\xleftrightarrow -\definechemicalsymbol[d:opencomplex] [\mathematics{\Bigg[}] % not yet ok -\definechemicalsymbol[d:closecomplex][\mathematics{\Bigg]}] % not yet ok - -\definechemicalsymbol[d:SPACE] [{\chemicalsymbol[d:space]}] -\definechemicalsymbol[d:PLUS] [{\chemicalsymbol[d:plus]}] -\definechemicalsymbol[d:MINUS] [{\chemicalsymbol[d:minus]}] -\definechemicalsymbol[d:GIVES] [{\chemicalsymbol[d:gives]}] -\definechemicalsymbol[d:EQUILIBRIUM] [{\chemicalsymbol[d:equilibrium]}] -\definechemicalsymbol[d:MESOMERIC] [{\chemicalsymbol[d:mesomeric]}] -\definechemicalsymbol[d:OPENCOMPLEX] [{\chemicalsymbol[d:opencomplex]}] -\definechemicalsymbol[d:CLOSECOMPLEX][{\chemicalsymbol[d:closecomplex]}] - \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}\aligntab}% -% \fi} - \def\chem_formula#1#2#3% we could do hboxes and measure - {\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}}% + {\ifcsname\??chemicalsymbol\detokenize{#1}\endcsname + \t_chem_mid\expandafter{\the\t_chem_mid\chem_formula_mid{#1}{#2}{#3}}% \else - \chem_formula_top_nop - \chem_formula_bot_nop - \fi\fi - \ifcsname\??chemicalsymbol d:\detokenize{#1}\endcsname - \t_chem_mid\expandafter{\the\t_chem_mid\chemicalsymbol[d:#1]\aligntab}% - \else - \t_chem_mid\expandafter{\the\t_chem_mid\molecule{#1}\aligntab}% + \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\aligntab}} -\def\chem_formula_bot_nop {\t_chem_bot\expandafter{\the\t_chem_bot\aligntab}} -\def\chem_formula_top_yes#1{\t_chem_top\expandafter{\the\t_chem_top\chem_formula_top_indeed{#1}\aligntab}\settrue\c_chem_has_top} -\def\chem_formula_bot_yes#1{\t_chem_bot\expandafter{\the\t_chem_bot\chem_formula_bot_indeed{#1}\aligntab}\settrue\c_chem_has_bot} +\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} -% Experimental: defaults might change. - -\definefloat - [\v!chemical] - [\v!chemicals] - -\setuplabeltext - [\v!chemical=] - -\setupfloat - [\v!chemical] - [\c!location=\v!here, - \c!inner=\hsize.8\textwidth\dontleavehmode, % brr - \c!align={\v!flushleft,\v!lohi}] - -\setupcaption - [\v!chemical] - [\c!location=\v!right, - \c!distance=\zeropoint, - \c!width=.2\textwidth, - \c!align=\v!flushright] - -% Can be used as for displayed math: \startplaceformula... to display a chemical formula -% or a chemical structure: -% -% \startplacechemical -% \startchemicalformula -% \chemical{2H_2} -% \chemical{PLUS} -% \chemical{O_2} -% \chemical{GIVES} -% \chemical{2H_2O} -% \stopchemicalformula -% \stopplacechemical - % gone: state option resolution offset (now frame offset) alternative \setupchemicalframed [\c!align=\v!normal, \c!strut=\v!no, \c!offset=\v!overlay, - \c!frame=\v!off] + \c!frame=off] \setupchemical [\c!frame=, -- cgit v1.2.3