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


(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:

\ifthenelse{\value{embeddedmaths} = 0}%
\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. Jan 23, 2012 at 1:29
  • 1
    Gack. Yes, the error is slightly different if I include the \newcounter. I’ll update. Jan 23, 2012 at 2:31

3 Answers 3


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

Grouping superscripts for processing later

\newsavebox{\supbox}% Superscript box
\newcommand{\bsup}{\begin{lrbox}{\supbox}$\tt\scriptstyle}% Superscript begin
\newcommand{\esup}{$\end{lrbox}{}^{\usebox{\supbox}}}% Superscript end
$x^{\mathtt{2 5 4 a}}$ \quad % Traditional input
$x\bsup 2 5 4 a\esup$ % "Tokenized" input

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.

  • 2
    Thanks! Your example doesn’t demonstrate it, but this also handles nested superscripts, such as $x\bsup 2\bsup 3\esup\esup$ Jan 23, 2012 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. Jan 23, 2012 at 2:49
  • 1
    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! Jan 23, 2012 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, 2012 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? Jan 24, 2012 at 4:19

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

$x^{\mathtt{2^{678} 5 4 a}}$ \quad % Traditional input
$x\bsup 2 \bsup 6 7 8 \esup 5 4 a\esup$ % "Tokenized" input

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

demonstration of tokenized superscript output


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. Jan 24, 2012 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 ). Jan 24, 2012 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! Jan 27, 2012 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.