leah blogs

May 2004

20may2004 · ppchTeX

Today I experimented with ppchTeX, a macro package for plain TeX, LaTeX and ConTeXt to typeset chemical formulas.

It’s stucture is very interesting; it uses an arcane syntax which in fact is a domain specific language. I’ll show some examples here.

Chemical formula of Ethene First, Ethene (CH2CH2).

The code of this looks like this (there is some stuff around it, but this is the code unique to this certain formula):

\chemical[ONE,SB46,DB1,Z0146,MOV1,SB28,Z28][C,C,H,H,H,H]

This reads like a simple program:

  • ONE: This is the command to introduce an “node” inside the formula.

  • SB46: Draw a Single Bond on position 4 and 6 of the current node. This is bottom-left and top-left.

  • DB1: Draw a Double Bond on position 1 of the current node. This is right-middle.

  • Z0146: Add the name of the atoms at position 0 (the middle of the node), 1, 4 and 6. These get the names from the data attached to the call; i.e. the current node gets named C, just like the right node, and the two left ones get H.

  • MOV1: Move to the node at position 1, that is, the right node.

  • SB28 and Z28: Draw the upper-right and bottom-right bonds, and name them accordingly.

This sounds quite compilicated, but once you got how to look at the formula, its quite easy to write.

Chemical formula of two
acetic acid molecules connected by hydrogen bonds

Now, a bit more complicated example, two acetic acid molecules connected by hydrogen bonds. The code:

\chemical[ONE,Z05,SB25,DB8][C,H_3C]
\chemical[PB:Z2,ONE,Z01,SB1,PE][O,H]
\chemical[PB:Z8,ONE,Z0,HB1,PE][O]
\chemical[SUB1,ONE,18OFF1,Z01,SB16,DB4][C,CH_3]
\chemical[PB:Z4,ONE,Z0,HB5,PE][O]
\chemical[PB:Z6,ONE,Z05,SB5,PE][O,H]

I’ll only explain the stuff not covered above:

  • PB:posPE: Start a subdiagram at position pos.

  • SUB1: move one structure in relation to another in the direction given.

  • 18OFF1: do 18 small steps in direction 1 (right).

Now, a quite tricky one, Guanine:

\chemical[FIVE,SB235,DB14,SR4,Z1245,RZ4][C,C,C,N,H]
\chemical[PB:Z3,ONE,Z0,SB3,CZ3,PE][N,H]
\chemical[ADJ1,SIX,SB1356,DB2,Z1236,DR6,RZ6][N,C,N,C,O]
\chemical[ADJ1,SIX,SB35,Z6][H,H]
\chemical[PB:Z3,ONE,Z0,SB28,CZ28,PE][N,H,H]

Chemical formula of Guanine

  • FIVE draws a five-ring.
  • RZ draws substituents using names from the data given.
  • ADJ1 moves another structure in the direction given adjacent to a bond.

Yeah, that’s it. Lots of fun and a source of endless frustration… :-)

NP: Grateful Dead—Mountains Of The Moon

Copyright © 2004–2018