ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Taco's \molecule fails in mkiv
@ 2009-02-23 16:21 Mojca Miklavec
  2009-02-23 16:52 ` Wolfgang Schuster
  0 siblings, 1 reply; 5+ messages in thread
From: Mojca Miklavec @ 2009-02-23 16:21 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hello,

I did not try to analyze how the following macros work (they are too
complex), but they somehow fail in mkiv. (I don't need the "F"
kerning, so that portion of code may go away, but the macro itself is
very very handy.)

All I want is to have a macro that "converts" _{...} into \low{...}
and ^{...} into \high{...} (or possibly enable using both at the same
time).

\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}

\starttext
\molecule{CO_2}
\stoptext

Thanks for any hints,
    Mojca
___________________________________________________________________________________
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] 5+ messages in thread

* Re: Taco's \molecule fails in mkiv
  2009-02-23 16:21 Taco's \molecule fails in mkiv Mojca Miklavec
@ 2009-02-23 16:52 ` Wolfgang Schuster
  2009-02-23 17:14   ` Mojca Miklavec
  2009-02-23 18:19   ` luigi scarso
  0 siblings, 2 replies; 5+ messages in thread
From: Wolfgang Schuster @ 2009-02-23 16:52 UTC (permalink / raw)
  To: mailing list for ConTeXt users


Am 23.02.2009 um 17:21 schrieb Mojca Miklavec:

> Hello,
>
> I did not try to analyze how the following macros work (they are too
> complex), but they somehow fail in mkiv. (I don't need the "F"
> kerning, so that portion of code may go away, but the macro itself is
> very very handy.)
>
> All I want is to have a macro that "converts" _{...} into \low{...}
> and ^{...} into \high{...} (or possibly enable using both at the same
> time).

\startluacode

thirddata = thirddata or { }

function thirddata.molecule(text)
     text = string.gsub(text,"_","\\low")
     text = string.gsub(text,"%^","\\high")
     tex.sprint(text)
end

\stopluacode

\def\molecule#1{\ctxlua{thirddata.molecule('#1')}}

\starttext
\molecule{H_2SO_4}
\molecule{H_2^+}
\stoptext

Wolfgang

___________________________________________________________________________________
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] 5+ messages in thread

* Re: Taco's \molecule fails in mkiv
  2009-02-23 16:52 ` Wolfgang Schuster
@ 2009-02-23 17:14   ` Mojca Miklavec
  2009-02-24  0:21     ` Wolfgang Schuster
  2009-02-23 18:19   ` luigi scarso
  1 sibling, 1 reply; 5+ messages in thread
From: Mojca Miklavec @ 2009-02-23 17:14 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Mon, Feb 23, 2009 at 17:52, Wolfgang Schuster wrote:
>
> Am 23.02.2009 um 17:21 schrieb Mojca Miklavec:
>
>> Hello,
>>
>> I did not try to analyze how the following macros work (they are too
>> complex), but they somehow fail in mkiv. (I don't need the "F"
>> kerning, so that portion of code may go away, but the macro itself is
>> very very handy.)
>>
>> All I want is to have a macro that "converts" _{...} into \low{...}
>> and ^{...} into \high{...} (or possibly enable using both at the same
>> time).
>
> \startluacode
>
> thirddata = thirddata or { }
>
> function thirddata.molecule(text)
>    text = string.gsub(text,"_","\\low")
>    text = string.gsub(text,"%^","\\high")
>    tex.sprint(text)
> end
>
> \stopluacode
>
> \def\molecule#1{\ctxlua{thirddata.molecule('#1')}}
>
> \starttext
> \molecule{H_2SO_4}
> \molecule{H_2^+}
> \stoptext

Hello Wolfgang,

thanks a lot for the very nice code :) :) :)

The second example (H_2^+) does not return expected result - it should have been
   H\lohi{2}{+}
instead, but I don't require such cases for the current document, so I
guess that I'll just replace the old macro with this one for now in
order not to get distracted with TeX problems too much :)

Thanks again,
    Mojca
___________________________________________________________________________________
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] 5+ messages in thread

* Re: Taco's \molecule fails in mkiv
  2009-02-23 16:52 ` Wolfgang Schuster
  2009-02-23 17:14   ` Mojca Miklavec
@ 2009-02-23 18:19   ` luigi scarso
  1 sibling, 0 replies; 5+ messages in thread
From: luigi scarso @ 2009-02-23 18:19 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Mon, Feb 23, 2009 at 5:52 PM, Wolfgang Schuster
<schuster.wolfgang@googlemail.com> wrote:
>
> Am 23.02.2009 um 17:21 schrieb Mojca Miklavec:
>
>> Hello,
>>
>> I did not try to analyze how the following macros work (they are too
>> complex), but they somehow fail in mkiv. (I don't need the "F"
>> kerning, so that portion of code may go away, but the macro itself is
>> very very handy.)
>>
>> All I want is to have a macro that "converts" _{...} into \low{...}
>> and ^{...} into \high{...} (or possibly enable using both at the same
>> time).
>
> \startluacode
>
> thirddata = thirddata or { }
>
> function thirddata.molecule(text)
>    text = string.gsub(text,"_","\\low")
>    text = string.gsub(text,"%^","\\high")
>    tex.sprint(text)
> end
>
> \stopluacode
>
> \def\molecule#1{\ctxlua{thirddata.molecule('#1')}}
hmm
Isn't better to remember original \molecule ?
I mean, something like
\let\ORIGINALmolecule\molecule
\def\molecule#1{\ctxlua{thirddata.molecule('#1')}}

Sometimes in some contexts ORGINAL works and modified no, and you
haven't time to
find why .
-- 
luigi
___________________________________________________________________________________
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] 5+ messages in thread

* Re: Taco's \molecule fails in mkiv
  2009-02-23 17:14   ` Mojca Miklavec
@ 2009-02-24  0:21     ` Wolfgang Schuster
  0 siblings, 0 replies; 5+ messages in thread
From: Wolfgang Schuster @ 2009-02-24  0:21 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: molecule_lpeg.tex --]
[-- Type: application/octet-stream, Size: 2018 bytes --]

\startluacode

do

    thirddata = thirddata or { }

    local molecule = { }
    
    function molecule.text(one)
        tex.sprint(string.format("%s",one))
    end

    function molecule.low(one,two)
        tex.sprint(string.format("%s\\low{%s}",one,two))
    end

    function molecule.high(one,two)
        tex.sprint(string.format("%s\\high{%s}",one,two))
    end

    function molecule.lowhigh(one,two,three)
        tex.sprint(string.format("%s\\lohi{%s}{%s}",one,two,three))
    end

    function molecule.highlow(one,two,three)
        tex.sprint(string.format("%s\\lohi{%s}{%s}",one,two,three))
    end

    local plus         = lpeg.P("+")
    local minus        = lpeg.P("-")
    local lowercase    = lpeg.R("az")
    local uppercase    = lpeg.R("AZ")
    local number       = lpeg.R("09")
    local subscript    = lpeg.P("_")
    local superscript  = lpeg.P("^")
    local leftbrace    = lpeg.P("{")
    local rightbrace   = lpeg.P("}")

    local single    = lowercase + number + plus + minus
    local multiple  = leftbrace * single^1 * rightbrace
    local content   = single + multiple 

    local text    = lpeg.C(uppercase^1)                                                                 / molecule.text
    local low     = lpeg.C(uppercase^1) * subscript   * lpeg.C(content)                                 / molecule.low
    local high    = lpeg.C(uppercase^1) * superscript * lpeg.C(content)                                 / molecule.high
    local lowhigh = lpeg.C(uppercase^1) * subscript   * lpeg.C(content) * superscript * lpeg.C(content) / molecule.lowhigh
    local highlow = lpeg.C(uppercase^1) * superscript * lpeg.C(content) * subscript   * lpeg.C(content) / molecule.highlow

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

    function thirddata.molecule(string)
        parser:match(string)
    end

end

\stopluacode

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

\starttext
\molecule{CO_2}
\molecule{H^+}
\molecule{H_2^+}
\molecule{C_nH_{2n+1}OH}
\stoptext

[-- Attachment #2: Type: text/plain, Size: 478 bytes --]


Am 23.02.2009 um 18:14 schrieb Mojca Miklavec:

> thanks a lot for the very nice code :) :) :)
>
> The second example (H_2^+) does not return expected result - it  
> should have been
>   H\lohi{2}{+}
> instead, but I don't require such cases for the current document, so I
> guess that I'll just replace the old macro with this one for now in
> order not to get distracted with TeX problems too much :)

You can try the attatched, it use lpeg to parse the content.

Wolfgang


[-- Attachment #3: Type: text/plain, Size: 487 bytes --]

___________________________________________________________________________________
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] 5+ messages in thread

end of thread, other threads:[~2009-02-24  0:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-23 16:21 Taco's \molecule fails in mkiv Mojca Miklavec
2009-02-23 16:52 ` Wolfgang Schuster
2009-02-23 17:14   ` Mojca Miklavec
2009-02-24  0:21     ` Wolfgang Schuster
2009-02-23 18:19   ` luigi scarso

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).