ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* the other blog (chemical stuff)
@ 2009-03-06  9:20 Hans Hagen
  0 siblings, 0 replies; only message in thread
From: Hans Hagen @ 2009-03-06  9:20 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Aditya Mahajan

Hi aditya,

some comments on the other bog item ... (others may wikify it)

you can do a quick and dirty solution as well (tested in mkiv):

\def\MoleculeC#1{$\enablesupersub\tf#1$}

now, concerning the lua solution, it looks pretty clean and ok, but 
there is some room for improvement ... the following version supports 
nested braces and numbers (we can let this evolve in a proper chemical
subsystem if needed since eventually i will replace ppchtex anyway)


\startluacode
local molecule = { }

local format, sprint = string.format, tex.sprint

function commands.justtext(one)
     sprint(tex.ctxcatcodes,one)
end

function commands.low(str)
     sprint(tex.ctxcatcodes,format("\\low{%s}",str))
end

function commands.high(str)
     sprint(tex.ctxcatcodes,format("\\high{%s}",str))
end

function commands.lowhigh(one,two)
     sprint(tex.ctxcatcodes,format("\\lohi{%s}{%s}",one,two))
end

function commands.highlow(one,two)
     sprint(tex.ctxcatcodes,format("\\hilo{%s}{%s}",one,two))
end

local lowercase   = lpeg.R("az")
local uppercase   = lpeg.R("AZ")

local plus        = lpeg.P("+")
local minus       = lpeg.P("-")
local digit       = lpeg.R("09")
local sign        = plus + minus
local cardinal    = digit^1
local integer     = sign^0 * cardinal

local leftbrace   = lpeg.P("{")
local rightbrace  = lpeg.P("}")
local nobrace     = 1 - (leftbrace + rightbrace)
local nested      = lpeg.P { leftbrace * (nobrace + lpeg.V(1))^0 * 
rightbrace }
local any         = lpeg.P(1)

local subscript   = lpeg.P("_")
local superscript = lpeg.P("^")

local content     = lpeg.C(nested + integer + any)

-- could be made more efficient

local lowhigh  = subscript   * content * superscript * content / 
commands.lowhigh
local highlow  = superscript * content * subscript   * content / 
commands.highlow
local low      = subscript   * content                         / 
commands.low
local high     = superscript * content                         / 
commands.high
local justtext = lpeg.C((1 - somescript)^1)                    / 
commands.justtext

local parser = (sometext + lowhigh + highlow + low + high)^0

function commands.molecule(string)
     parser:match(string)
end
\stopluacode


\def\molecule#1{\ctxlua{commands.molecule("#1")}}

\molecule{H_2SO_4^-2}
\molecule{H_2SO_4^{-2}}
\molecule{H_2SO_4^{-2{x}}}

Hans




-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : https://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-03-06  9:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-06  9:20 the other blog (chemical stuff) Hans Hagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).