Taco's \molecule fails in mkiv
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
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
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
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
On Mon, Feb 23, 2009 at 5:52 PM, Wolfgang Schuster
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
participants (3)
-
luigi scarso
-
Mojca Miklavec
-
Wolfgang Schuster