# Emitting LaTeX a “token” at a time, handling superscripts

I am outputting LaTeX from another source language, but one that only gives me things a source-token at a time. Thus, I may get a token corresponding to “begin superscript” and then I will later get one corresponding to “end superscript”.

I used to have

\newcommand{\mybeginsup}{\begin{math}{}\sp\bgroup\tt}
\newcommand{\myendsup}{\egroup\end{math}}


(The \tt is the way I want my superscripts to style, and I hope it’s irrelevant.)

The problem with this is that it doesn’t handle nested superscripts. My guess it that this causes a nesting of \begin{math} commands, and that it is this that causes the “Bad math environment delimiter.” error messages.

So, how should I do this?

I did try:

\usepackage{ifthen}
\newcounter{embeddedmaths}
\renewcommand{\mybeginsup}{%
\ifthenelse{\value{embeddedmaths} = 0}%
\renewcommand{\myendsup}{%
\ifthenelse{\value{embeddedmaths} = 0}{\end{math}}{}}


to try to count the number of times I had entered math mode, but this gives

! Missing } inserted.
<inserted text>


So now I’m stuck.

• Probably missing a \newcounter{embeddedmaths} somewhere. – Bruno Le Floch Jan 23 '12 at 1:29
• Gack. Yes, the error is slightly different if I include the \newcounter. I’ll update. – Michael Norrish Jan 23 '12 at 2:31

You could box the superscript content, since you know it will have to be in \scriptstyle:

\documentclass{article}
\newsavebox{\supbox}% Superscript box
\begin{document}
$x^{\mathtt{2 5 4 a}}$ \quad % Traditional input
$x\bsup 2 5 4 a\esup$ % "Tokenized" input
\end{document}


Also note that defining a command beginning with \end causes problems with LaTeX, since it is usually associated with an environment. As such, I've used \bsup and \esup.

• Thanks! Your example doesn’t demonstrate it, but this also handles nested superscripts, such as $x\bsup 2\bsup 3\esup\esup$ – Michael Norrish Jan 23 '12 at 2:39
• Ah. What about including within an alltt environment? If I do \begin{alltt} x\bsup{} 2\bsup{} 3\esup{}\esup{} \end{alltt} I get a Missing $inserted error. – Michael Norrish Jan 23 '12 at 2:49 • To answer my own follow-up question: the problem is the \scriptstyle, and/or the $. I've changed your solution to have \begin{lrbox}{\supbox}\tt\scriptsize for the begin, and to \end{lrbox}${}^{\usebox{\supbox}}$} for the end. This works, and given the constraint of being inside an alltt, is perhaps as good as can be done. Thanks again for the solution! – Michael Norrish Jan 23 '12 at 3:17
• @MichaelNorrish: Sure; it seems like you don't even know whether you're in math mode or not. This was not clear from the start. Also note that nesting the superscripts works, but will leave you with a fixed superscript size however deep you go. LaTeX actually resizes superscripts of a superscript (to \scriptscriptstyle). Not sure whether this is an additional problem you might have. – Werner Jan 23 '12 at 3:27
• Yeah, the fixed \scriptsize is a compromise I'm willing to make. It’s true that I don't know if I will be in math-mode or not (in fact, the expectation is that I won’t be). Is it worth re-editing the original question to make this clear? – Michael Norrish Jan 24 '12 at 4:19

the "shorthand" forms for \begingroup and \endgroup can help you here (stealing Werner's example):

\documentclass{article}
\newcommand{\bsup}{^\bgroup}
\newcommand{\esup}{\egroup}
\begin{document}
$x^{\mathtt{2^{678} 5 4 a}}$ \quad % Traditional input
$x\bsup 2 \bsup 6 7 8 \esup 5 4 a\esup$ % "Tokenized" input
\end{document}


works without having to specify a size, so it handles multiple levels of superscripts:

If you want to avoid problems with nested math use \ensuremath{...} the standard latex command for going in to math mode if you are not already in it.

Alternatively if this isn't really math and you are just using math mode to get the superscript functionality (which seems likely if you want monospaced text) use \textsuperscript instead

• The issue with \ensuremath is that I don’t have access to all of its argument at once (hence the “token at a time”). I didn’t test very extensively but attempting, for example, \newcommand{\squared}{\ensuremath\bgroup{}^2\egroup} \begin{alltt} x\squared \end{alltt} fails. Putting \textsuperscript in for \ensuremath and getting rid of {}^2 fails in the same way, sadly. – Michael Norrish Jan 24 '12 at 4:16
• you can't use bgroup/egroup to delimit TeX macro arguments you need to use real braces, however what's to stop you emitting \ensuremath{ at the start and } at the end (or similarly with \textsuperscript ). – David Carlisle Jan 24 '12 at 13:00
• Yes, you’re right. The only issue with that would be my desire to introduce an extra layer of indirection, so that instead of mapping directly to \ensuremath{\sp for beginning a superscript, I’d like to be able to map to \mybeginsup{}. But then I don’t know how to define \mybeginsup to have a body of \ensuremath{\sp because of the unbalanced braces. I do admit this is quibbling over niceties! – Michael Norrish Jan 27 '12 at 2:24