the other blog (chemical stuff)
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 -----------------------------------------------------------------
participants (1)
-
Hans Hagen