ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen <pragma@wxs.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
	Aditya Mahajan <adityam@umich.edu>
Subject: the other blog (chemical stuff)
Date: Fri, 06 Mar 2009 10:20:46 +0100	[thread overview]
Message-ID: <49B0EAEE.6040609@wxs.nl> (raw)

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
___________________________________________________________________________________


                 reply	other threads:[~2009-03-06  9:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=49B0EAEE.6040609@wxs.nl \
    --to=pragma@wxs.nl \
    --cc=adityam@umich.edu \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).