Difference between revisions of "Chemistry"

From Wiki
Jump to navigation Jump to search
(added some examples of chemical structures)
(add link to publications manual)
 
(14 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
< [[Math]], [[XML]], [[Graphics]], [[MetaPost]] >
 
< [[Math]], [[XML]], [[Graphics]], [[MetaPost]] >
  
ConTeXt comes with the extensive chemistry module [[PPCHTeX]] which allows the drawing of chemical structures and the typesetting of reactions.
+
ConTeXt always came with extensive chemistry support including drawing of chemical structures and the typesetting of reactions.
  
[[PPCHTeX]] works with plain TeX, LaTeX and - of course - with ConTeXt.
+
In MkII this was called PPCHTeX, which works also with plain TeX and LaTeX.
  
* [http://www.pragma-ade.com/ppchtex.htm PPCHTeX (general)] by Pragma
+
There doesn’t seem to be any current documentation (all previous links were long dead), while chemistry support is still part of the distribution, but currently broken. (2023-10-01: part of the math upgrade, please wait until it becomes stable.) There are a few working examples in the [[manual:mkiv-publications.pdf|publications]] manual (chapter 10.2), i.e. in the file [https://source.contextgarden.net/doc/context/sources/general/manuals/publications/C6H6O.bib C6H6O.bib].
* [http://www.pragma-ade.com/document.htm PPCHTeX documentation] by Pragma
 
* [http://www.ntg.nl/mailman/listinfo/ntg-ppchtex PPCHTeX mailing list]
 
* [http://www.ntg.nl/pipermail/ntg-ppchtex/ PPCHTeX mailing list archive]
 
 
 
Some other PPCHTeX links:
 
* [http://mitglied.lycos.de/ppchtex/00index.html PPCHTeX structure database]
 
* [http://www.mit.edu/afs/athena/contrib/tex-contrib/Chem2/ppchtex/doc/fh-ch-en.htm PPCHTeX FAQ] and [http://www.pragma-ade.com/general/faqs/ PPCHTeX FAQ in PDF](rather old)
 
* [http://www.tug.org/TUGboat/Articles/tb17-1/tb50hage.pdf TUG Boat article on PPCHTeX] by Hans Hagen and A.F Otten
 
  
 
Some other links about chemistry in ConTeXt:
 
Some other links about chemistry in ConTeXt:
 +
* [[manual:mkiv-publications.pdf|publications]] manual – use the bibliography database for chemicals
 
* [[Chromatograms]] macro
 
* [[Chromatograms]] macro
* [[manual:xchemml-p.pdf|ChemML]] ([[manual:xchemml-s.pdf|screen]]), [[MathML]] extension for chemistry
+
* [[manual:xchemml-p.pdf|ChemML]] ([[manual:xchemml-s.pdf|screen]]), [[MathML]] extension for chemistry.
  
=== Formulae with chemic module ===
+
== Using {{cmd|chemical}} ==
  
<texcode>
+
This is the built-in version of the previous `chemic` module.
\usemodule[chemic]
 
\chemical{HSO_4^{-}}
 
</texcode>
 
  
<context>  
+
<context source="yes">  
\usemodule[chemic]
 
 
\chemical{HSO_4^{-}}
 
\chemical{HSO_4^{-}}
 
</context>
 
</context>
  
* It can also be used to typeset chemical ''reactions''
+
You can also typeset chemical ''reactions'':
  
<texcode>
+
<context source="yes">  
\usemodule[chemic]
 
 
\startformula
 
\startformula
 
  \chemical{2H_2,+,O_2,->,2H_2O}
 
  \chemical{2H_2,+,O_2,->,2H_2O}
 
\stopformula
 
\stopformula
</texcode>
+
</context>
 +
 
 +
== Chemical structures ==
 +
 
 +
(The “good” images are old; the broken ones are the result of the current code.)
 +
 
 +
<context source="yes">
 +
\startchemical
 +
        \chemical[FIVE,FRONT,BB125,+SB3,-SB4,Z4][O]
 +
        \chemical[FIVE,FRONT,+R1235,+RZ1235][H,H,\SR{HOH_2C},OH]
 +
        \chemical[FIVE,FRONT,-R1235,-RZ1235][H,OH,H,H]
 +
\stopchemical
 +
</context>
 +
 
 +
[[Image:Chemistry_example1.png]]
  
 +
<context source="yes">
 +
\setupchemical[width=fit]
  
<context>
+
\startchemical
\usemodule[chemic]
+
        \chemical[SIX,B,C,MOV2,B,C,R234,RZ234,MOV5,MOV6,B,C,R561,RZ561]
\startformula
+
        [R,R,R,R,R,R]
\chemical{2H_2,+,O_2,->,2H_2O}
+
\stopchemical
\stopformula
 
 
</context>
 
</context>
  
=== Simple Formulae in Text Mode ===
+
[[Image:chemistry_example2.png]]
  
An alternative to using the chemistry module or mathmode for typesetting simple formulae are the three ConTeXt commands <cmd>high</cmd>, <cmd>low</cmd> and <cmd>lohi</cmd>. This might be useful if you want to typset the formula with the same font as the surrounding text (in titles or slanted fonts, although that might not be desired in all situations).
+
To draw this structure horizontally:
 +
<context source="yes">  
 +
\setupchemical[width=fit]
  
<texcode>
+
\def\RotR{\rotate[rotation=270]{\ix R}}
\ss H\low{2}O also consists of H\high{+} and OH\high{--}.
 
There are two main carbon isotopes: \lohi{6}{12}C and \lohi[left]{6}{13}C.
 
</texcode>
 
  
<context>
+
\rotate[rotation=90]{
\ss H\low{2}O also consists of H\high{+} and OH\high{--}.\crlf
+
\startchemical
There are two main carbon isotopes: \lohi{6}{12}C and \lohi[left]{6}{13}C.
+
        \chemical[SIX,B,C,MOV2,B,C,R234,RZ234,MOV5,MOV6,B,C,R561,RZ561]
 +
        [\RotR,\RotR,\RotR,\RotR,\RotR,\RotR]
 +
\stopchemical
 +
}
 
</context>
 
</context>
  
Taco's macros implement this approach [http://archive.contextgarden.net/thread/20060702.084423.a201a280.en.html]:
+
[[Image:chemistry_example3.png]]
 +
 
 +
An example of an amino acid: Histidine (Ace-His-Nme).
 +
The rest of the amino acids are shown in [http://tt4sci.wordpress.com/2009/08/05/drawing-organic-molecules-in-latex-ii-amino-acids/ TT4Sci]
 +
<context source="yes">
 +
\definechemical[radical]
 +
{\chemical[SAVE,ONE,SB768,Z068][C,H,H]
 +
\chemical[MOV7,ONE,Z0][C]
 +
\chemical[MOV7,FIVE,ROT4,SB12345,EB42,Z1235,SR125,RZ125,RESTORE][N,C,N,C,H,H,H]}
 +
\startchemical
 +
[scale=big,size=big,left=2600,right=4200,bottom=2800,top=4500]
 +
\bottext{Histidine}
 +
\chemical[SIX,SB23]
 +
\chemical[PB:Z1,SAVE,radical,RESTORE,PE]
 +
\chemical[PB:Z2,ONE,SB37,Z03,PE][\SL{C^{\alpha}},H^{\alpha 1}]
 +
\chemical[PB:Z3,SIX,Z5,SB4,Z4,PE][N,H]
 +
{\color{blue}
 +
\chemical[PB:Z4,SIX,Z2,SB2,DB1,Z31,PE][C{\text{\tiny{-1}}},\SR{H_3C},O{\text{\tiny{-1}}}]}
 +
\chemical[SIX,MOV1,SB23]
 +
\chemical[PB:Z3,SIX,Z5,DB4,Z4,PE][C,O]
 +
{\color{red}
 +
\chemical[PB:Z2,SIX,Z4,SB34,Z53,PE][N{\text{\tiny{+1}}},H{\text{\tiny{+1}}},\SL{CH_3}]}
 +
\stopchemical
 +
</context>
  
<texcode>
+
[[Image:histidine.png]]
\newbox\chemlowbox
 
\def\chemlow#1%
 
  {\setbox\chemlowbox\hbox{{\switchtobodyfont[small]#1}}}
 
  
\def\chemhigh#1%
 
  {\ifvoid\chemlowbox \high{{\switchtobodyfont[small]#1}}%
 
  \else \/\lohi{\box\chemlowbox}{{\switchtobodyfont[small]#1}}\fi }
 
  
\def\finishchem%
+
== Simple Formulae in Text Mode ==
  {\ifvoid\chemlowbox \else
 
    \iffluor \fluorfalse \kern-.1em \fi\low{\box\chemlowbox}\fi}
 
  
% for "kerning" after F
+
An alternative to using the chemistry module or mathmode for typesetting simple formulae are the ConTeXt commands {{cmd|high}}, {{cmd|low}}, {{cmd|lohi}}, and {{cmd|himilo}}. This might be useful if you want to typset the formula with the same font as the surrounding text (in titles or slanted fonts, although that might not be desired in all situations).
\newif\iffluor
 
  
\unexpanded\def\molecule%
+
<context source="yes">
  {\bgroup
+
\ss H\low{2}O also consists of H\high{+} and OH\high{--}.\crlf
  \catcode`\_=\active \uccode`\~=`\_ \uppercase{\let~\chemlow}%
+
There are two main carbon isotopes: \lohi{6}{12}C and \lohi[left]{6}{13}C.
  \catcode`\^=\active \uccode`\~=`\^ \uppercase{\let~\chemhigh}%
+
</context>
  \dostepwiserecurse {65}{90}{1}
 
      {\catcode \recurselevel = \active
 
      \uccode`\~=\recurselevel
 
      \uppercase{\edef~{\noexpand\finishchem
 
                        \rawcharacter{\recurselevel}}}}%
 
  \uccode `\~=`\F \uppercase{\def~{\finishchem F\fluortrue}}%
 
  \catcode`\-=\active \uccode`\~=`\- \uppercase{\def~{--}}%
 
  \loggingall
 
  \domolecule }%
 
  
\def\domolecule#1%
+
Taco’s macros implement this approach [http://archive.contextgarden.net/thread/20060702.084423.a201a280.en.html]:
  {\expandafter\scantokens\expandafter
 
        {\detokenize{#1\finishchem}}\egroup}
 
</texcode>
 
  
<texcode>
+
'''This is broken code from 2006!'''
\bs Bold slanted molecules: \molecule{HSO_4^-} and \molecule{SF_6}
 
</texcode>
 
  
<context>
+
<context source="yes">  
 
\newbox\chemlowbox
 
\newbox\chemlowbox
 
\def\chemlow#1%
 
\def\chemlow#1%
Line 137: Line 142:
 
         {\detokenize{#1\finishchem}}\egroup}
 
         {\detokenize{#1\finishchem}}\egroup}
  
{\bsx Bold slanted molecules: \molecule{HSO_4^-} and \molecule{SF_6}}
+
\bs Bold slanted molecules: \molecule{HSO_4^-} and \molecule{SF_6}
 
 
 
</context>
 
</context>
  
The main difference with the official chemic module is that the <code>\modlecule</code> from this example uses the same font as the one in surrounding text, while the official module always uses roman font (which is probably the proper way if you're writing dozens of formulas). Hans's module is also more powerful (can do much more trickery than a simple conversion of subscripts and superscripts). See the manuals listed above.
+
The main difference with the built-in chemistry is that the <code>\molecule</code> from this example uses the same font as the one in surrounding text, while the official module always uses the (mostly roman) math font (which is probably the proper way if you’re writing dozens of formulas).
 
 
== Chemical structures ==
 
 
 
(see also [http://mitglied.lycos.de/ppchtex/00index.html PPCHTeX structure database])
 
 
 
<texcode>
 
\usemodule[chemic]                                                                                           
 
 
 
\starttext
 
 
 
\startchemical
 
        \chemical[FIVE,FRONT,BB125,+SB3,-SB4,Z4][O]
 
        \chemical[FIVE,FRONT,+R1235,+RZ1235][H,H,\SR{HOH_2C},OH]
 
        \chemical[FIVE,FRONT,-R1235,-RZ1235][H,OH,H,H]
 
\stopchemical
 
 
 
\stoptext
 
</texcode>
 
  
[[Image:Chemistry_example1.png]]
+
Alternatively you can setup the <code>style</code> parameter used for chemical formulas manually:
  
 
<texcode>
 
<texcode>
\usemodule[chemic]
+
\setupchemical[style=\bs]
\setupchemical[width=fit]
 
 
 
\starttext
 
 
 
\startchemical
 
        \chemical[SIX,B,C,MOV2,B,C,R234,RZ234,MOV5,MOV6,B,C,R561,RZ561]
 
        [R,R,R,R,R,R]
 
\stopchemical
 
 
 
\stoptext
 
 
</texcode>
 
</texcode>
  
[[Image:chemistry_example2.png]]
+
== Old Links ==
 
 
To draw this structure horizontally:
 
<texcode>
 
\usemodule[chemic]
 
\setupchemical[width=fit]
 
  
\starttext
+
* [http://www.mit.edu/afs/athena/contrib/tex-contrib/Chem2/ppchtex/doc/fh-ch-en.htm PPCHTeX FAQ] (1997)
 +
* [http://www.tug.org/TUGboat/Articles/tb17-1/tb50hage.pdf TUG Boat article on PPCHTeX] by Hans Hagen and A.F. Otten 1996
  
\rotate[rotation=90]{
 
\startchemical
 
        \chemical[SIX,B,C,MOV2,B,C,R234,RZ234,MOV5,MOV6,B,C,R561,RZ561]
 
        [\RotR,\RotR,\RotR,\RotR,\RotR,\RotR]
 
\stopchemical
 
}
 
 
/stoptext
 
</texcode>
 
 
[[Image:chemistry_example3.png]]
 
  
[[Category:Graphics]]
+
[[Category:Sciences]]
[[Category:Chemistry]]
 

Latest revision as of 19:40, 1 October 2023

< Math, XML, Graphics, MetaPost >

ConTeXt always came with extensive chemistry support including drawing of chemical structures and the typesetting of reactions.

In MkII this was called PPCHTeX, which works also with plain TeX and LaTeX.

There doesn’t seem to be any current documentation (all previous links were long dead), while chemistry support is still part of the distribution, but currently broken. (2023-10-01: part of the math upgrade, please wait until it becomes stable.) There are a few working examples in the publications manual (chapter 10.2), i.e. in the file C6H6O.bib.

Some other links about chemistry in ConTeXt:

Using \chemical

This is the built-in version of the previous chemic module.

 
\chemical{HSO_4^{-}}

You can also typeset chemical reactions:

 
\startformula
 \chemical{2H_2,+,O_2,->,2H_2O}
\stopformula

Chemical structures

(The “good” images are old; the broken ones are the result of the current code.)

 
\startchemical
        \chemical[FIVE,FRONT,BB125,+SB3,-SB4,Z4][O]
        \chemical[FIVE,FRONT,+R1235,+RZ1235][H,H,\SR{HOH_2C},OH]
        \chemical[FIVE,FRONT,-R1235,-RZ1235][H,OH,H,H]
\stopchemical

Chemistry example1.png

 
\setupchemical[width=fit]

\startchemical
        \chemical[SIX,B,C,MOV2,B,C,R234,RZ234,MOV5,MOV6,B,C,R561,RZ561]
        [R,R,R,R,R,R]
\stopchemical

Chemistry example2.png

To draw this structure horizontally:

 
\setupchemical[width=fit]

\def\RotR{\rotate[rotation=270]{\ix R}}

\rotate[rotation=90]{
\startchemical
        \chemical[SIX,B,C,MOV2,B,C,R234,RZ234,MOV5,MOV6,B,C,R561,RZ561]
        [\RotR,\RotR,\RotR,\RotR,\RotR,\RotR]
\stopchemical
}

Chemistry example3.png

An example of an amino acid: Histidine (Ace-His-Nme). The rest of the amino acids are shown in TT4Sci

 
\definechemical[radical]
{\chemical[SAVE,ONE,SB768,Z068][C,H,H]
\chemical[MOV7,ONE,Z0][C]
\chemical[MOV7,FIVE,ROT4,SB12345,EB42,Z1235,SR125,RZ125,RESTORE][N,C,N,C,H,H,H]}
\startchemical
[scale=big,size=big,left=2600,right=4200,bottom=2800,top=4500]
\bottext{Histidine}
\chemical[SIX,SB23]
\chemical[PB:Z1,SAVE,radical,RESTORE,PE]
\chemical[PB:Z2,ONE,SB37,Z03,PE][\SL{C^{\alpha}},H^{\alpha 1}]
\chemical[PB:Z3,SIX,Z5,SB4,Z4,PE][N,H]
{\color{blue}
\chemical[PB:Z4,SIX,Z2,SB2,DB1,Z31,PE][C{\text{\tiny{-1}}},\SR{H_3C},O{\text{\tiny{-1}}}]}
\chemical[SIX,MOV1,SB23]
\chemical[PB:Z3,SIX,Z5,DB4,Z4,PE][C,O]
{\color{red}
\chemical[PB:Z2,SIX,Z4,SB34,Z53,PE][N{\text{\tiny{+1}}},H{\text{\tiny{+1}}},\SL{CH_3}]}
\stopchemical

Histidine.png


Simple Formulae in Text Mode

An alternative to using the chemistry module or mathmode for typesetting simple formulae are the ConTeXt commands \high, \low, \lohi, and \himilo. This might be useful if you want to typset the formula with the same font as the surrounding text (in titles or slanted fonts, although that might not be desired in all situations).

 
\ss H\low{2}O also consists of H\high{+} and OH\high{--}.\crlf
There are two main carbon isotopes: \lohi{6}{12}C and \lohi[left]{6}{13}C.

Taco’s macros implement this approach [1]:

This is broken code from 2006!

 
\newbox\chemlowbox
\def\chemlow#1%
  {\setbox\chemlowbox\hbox{{\switchtobodyfont[small]#1}}}

\def\chemhigh#1%
  {\ifvoid\chemlowbox \high{{\switchtobodyfont[small]#1}}%
   \else \/\lohi{\box\chemlowbox}{{\switchtobodyfont[small]#1}}\fi }

\def\finishchem%
   {\ifvoid\chemlowbox \else
     \iffluor \fluorfalse \kern-.1em \fi\low{\box\chemlowbox}\fi}

% for "kerning" after F
\newif\iffluor

\unexpanded\def\molecule%
  {\bgroup
   \catcode`\_=\active \uccode`\~=`\_ \uppercase{\let~\chemlow}%
   \catcode`\^=\active \uccode`\~=`\^ \uppercase{\let~\chemhigh}%
   \dostepwiserecurse {65}{90}{1}
      {\catcode \recurselevel = \active
       \uccode`\~=\recurselevel
       \uppercase{\edef~{\noexpand\finishchem
                         \rawcharacter{\recurselevel}}}}%
   \uccode `\~=`\F \uppercase{\def~{\finishchem F\fluortrue}}%
   \catcode`\-=\active \uccode`\~=`\- \uppercase{\def~{--}}%
   \loggingall
   \domolecule }%

\def\domolecule#1%
  {\expandafter\scantokens\expandafter
        {\detokenize{#1\finishchem}}\egroup}

\bs Bold slanted molecules: \molecule{HSO_4^-} and \molecule{SF_6}

The main difference with the built-in chemistry is that the \molecule from this example uses the same font as the one in surrounding text, while the official module always uses the (mostly roman) math font (which is probably the proper way if you’re writing dozens of formulas).

Alternatively you can setup the style parameter used for chemical formulas manually:

\setupchemical[style=\bs]

Old Links