0

I am writing a program that will allow users to write a LaTeX string, and then it will write it to a tex file, run latex to convert it to a DVI, and then render the DVI. I also want users to be able to split the string up arbitrarily to allow each substring to be manipulated separately in the program. This splitting needs to be able to split as fine as each individual glyph if requested. My idea for doing this was to add \special commands throughout the tex file, and then my DVI reader would use those to figure out which characters correspond to which sections. I have this mostly set up, but I'm having a difficult time programmatically inserting the \special commands in a way that still compiles and doesn't affect the typesetting.

As an example, let's say the user wanted to render a_b^c = \sqrt{d}, and that each glyph needs to be in a different section. (Assume for simplicity that the square root is a single glyph even though that's not the case in reality.) The first idea is this:

\newcommand{sectiona}[1]{\special{section#1}}
\sectiona{0}a \sectiona{1}_b \sectiona{2}^c \sectiona{3}= \sectiona{4}\sqrt \sectiona{5}d

There are two issues: First, the c is placed a little to the right. It seems that adding the command before the ^ is messing it up somehow. Second, it doesn't actually compile because the \sqrt just sees a \special as its argument.

The second idea is this:

\newcommand{sectiona}[1]{\special{section#1}}
\sectiona{0}a _\sectiona{1}b ^\sectiona{2}c \sectiona{3}= \sectiona{4}\sqrt \sectiona{5}d

This still doesn't compile. In addition to the \sqrt having a bad argument, the _ and ^ now do too.

The third idea is this:

\newcommand{sectionb}[2]{{\special{section#1}#2}}
\sectionb{0}a _\sectionb{1}b ^\sectionb{2}c \sectionb{3}= \sectionb{4}\sqrt \sectionb{5}d

By having the command eat the following token, the subscript and superscript work perfectly. But the \sqrt still doesn't work because with the new command, a } directly follows the \sqrt. In addition, The spacing around the = is also messed up.

Of course, the best solution would be something like this:

\newcommand{sectiona}[1]{\special{section#1}}
\newcommand{sectionb}[2]{{\special{section#1}#2}}
\sectiona{0}a _\sectionb{1}b ^\sectionb{2}c \sectiona{3}= \sectiona{4}\sqrt \sectionb{5}d

However, my program has no way of knowing whether what comes before a section takes an argument or if what comes after a section needs an argument, so this method doesn't seem feasible. Maybe there's some TeX way to figure that out but I have no idea what it is.

There is a similar question on this site here, but the solution presented there has the problems above where it doesn't play nice with the subscripts, superscripts, and square roots. I need a solution that can be inserted almost anywhere in the tex string.

sudgy
  • 133

0 Answers0