# Why does \texttt repeat and shrink my equation?

When I use \texttt with the caret symbol in an amsmath equation environment, I get a very weird result. Specifically, the text repeats itself four times while shrinking.

MWE:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{equation*}
\texttt{y^o}
\end{equation*}

\end{document}


Compiling this document with pdflatex produces the following result:

The same result occurs even if I remove the o character. To fix it I could modify the offending line to \texttt{y$^o$}, but why is this repeating happening in the first place?

• Welcome to TeX.SE! Your code produces tons of error messages. If you ignore them, you get a strange result. Lesson: don't ignore error messages. If you replace the equation code by \begin{equation*} \texttt{y}^{\texttt{o}} \end{equation*}, the errors are gone, and so is the strange result. (Explanation: you cannot use ^ in text mode, but with \texttt you switch to text mode.)
– user121799
Oct 14 '18 at 1:44
• Oct 14 '18 at 1:47
• @marmot Thank you for the reminder to check error messages. I would understand a fatal error, or refusing to display the equation environment altogether, but what causes repeating text? The error messages indicate that pdflatex stumbles while trying to correct the error, but how is it stumbling? (I suppose that I am more interested in why the strange result occurs than how to fix it.) Oct 14 '18 at 1:53
• This is actually quite an interesting question and I don't know the full story of why you get that, but the synopsis is you have to steamroller through what I think must be over a dozen errors. When TeX encounters an error it does try to fix it. When it encounters ^ - which can only be used in math mode - it inserts a missing $ to put you in math mode so that it can struggle on through, but then there's no closing $ of course and, well, eventually you just get a mess. Exactly how you get that mess I'd be interested to know too Oct 14 '18 at 2:12
• @Au101 I think what we're seeing here is \mathchoice at work (likely called in turn by \mathpalette) in a setting where it's not supposed to be used in the first place. After all, as part of its work, it typesets its arguments in display, text, script, and scriptscript mode, which is what we are seeing here. It is not common for all four to end up in the output, however! Chasing down the exact mechanism, though, is likely a task for masochists or those with too much time on their hands. Oct 14 '18 at 8:39

The markup should be \mathtt{y^{o}} as the content of the argument is math not text.

However to explain the output.

! Missing $inserted. <inserted text>$
l.7     \texttt{y^o}

?


You could/should stop there by typing x and fix the error however TeX's interface was designed for an era when processing a large document could take several hours and so it was better to try to collect all error in one run.

So if you scroll past that error by typing s or if you ran tex in scrollmode via a commandline option, it will try to recover from the missing $ by inserting one just before the ^ and then carrying on. Its recovery though (from any error) is just to insert whatever needs to be inserted to let it carry on and syntax check the document. If you get any pdf output at all after an error it is at best usefull for debugging but it is essentially arbitrary accidental output from the error recovery, So... typing s here we get ! Missing$ inserted.
<inserted text>
$l.7 \texttt{y^o} ? s OK, entering \scrollmode... ! Extra }, or forgotten$.
\textdef@ ...th {#1}\let \f@size #2\selectfont #3}
}
l.7     \texttt{y^o}

I've deleted a group-closing symbol because it seems to be
spurious, as in $x}$'. But perhaps the } is legitimate and
you forgot something else, as in \hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing I$}'.

! Extra }, or forgotten $. \textdef@ ...h {#1}\let \f@size #2\selectfont #3}} l.7 \texttt{y^o} I've deleted a group-closing symbol because it seems to be spurious, as in $x}$'. But perhaps the } is legitimate and you forgot something else, as in \hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing I$}'. ! Extra }, or forgotten$.
\text@ ...e {\textdef@ \displaystyle \f@size {#1}}
{\textdef@ \textstyle \f@s...
l.7     \texttt{y^o}

I've deleted a group-closing symbol because it seems to be
spurious, as in $x}$'. But perhaps the } is legitimate and
you forgot something else, as in \hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing I$}'.

! Missing $inserted. <inserted text>$
l.7     \texttt{y^o}

I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.

! Extra }, or forgotten $. \textdef@ ...th {#1}\let \f@size #2\selectfont #3} } l.7 \texttt{y^o} I've deleted a group-closing symbol because it seems to be spurious, as in $x}$'. But perhaps the } is legitimate and you forgot something else, as in \hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing I$}'. ! Extra }, or forgotten$.
\textdef@ ...h {#1}\let \f@size #2\selectfont #3}}

l.7     \texttt{y^o}

I've deleted a group-closing symbol because it seems to be
spurious, as in $x}$'. But perhaps the } is legitimate and
you forgot something else, as in \hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing I$}'.

! Extra }, or forgotten $. \text@ ...xtstyle \f@size {\firstchoice@false #1}} {\textdef@ \textstyle \sf@... l.7 \texttt{y^o} I've deleted a group-closing symbol because it seems to be spurious, as in $x}$'. But perhaps the } is legitimate and you forgot something else, as in \hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing I$}'. ! Missing$ inserted.
<inserted text>
$l.7 \texttt{y^o} I've inserted a begin-math/end-math symbol since I think you left one out. Proceed, with fingers crossed. ! Extra }, or forgotten$.
\textdef@ ...th {#1}\let \f@size #2\selectfont #3}
}
l.7     \texttt{y^o}

I've deleted a group-closing symbol because it seems to be
spurious, as in $x}$'. But perhaps the } is legitimate and
you forgot something else, as in \hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing I$}'.

! Extra }, or forgotten $. \textdef@ ...h {#1}\let \f@size #2\selectfont #3}} l.7 \texttt{y^o} I've deleted a group-closing symbol because it seems to be spurious, as in $x}$'. But perhaps the } is legitimate and you forgot something else, as in \hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing I$}'. ! Extra }, or forgotten$.
\text@ ...tstyle \sf@size {\firstchoice@false #1}}
{\textdef@ \textstyle \ssf...
l.7     \texttt{y^o}

I've deleted a group-closing symbol because it seems to be
spurious, as in $x}$'. But perhaps the } is legitimate and
you forgot something else, as in \hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing I$}'.

! Missing $inserted. <inserted text>$
l.7     \texttt{y^o}

I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.

! Extra }, or forgotten $. \textdef@ ...th {#1}\let \f@size #2\selectfont #3} } l.7 \texttt{y^o} I've deleted a group-closing symbol because it seems to be spurious, as in $x}$'. But perhaps the } is legitimate and you forgot something else, as in \hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing I$}'. ! Extra }, or forgotten$.
\textdef@ ...h {#1}\let \f@size #2\selectfont #3}}

l.7     \texttt{y^o}

I've deleted a group-closing symbol because it seems to be
spurious, as in $x}$'. But perhaps the } is legitimate and
you forgot something else, as in \hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing I$}'.

! Extra }, or forgotten $. \text@ ...style \ssf@size {\firstchoice@false #1}} \check@mathfonts } l.7 \texttt{y^o} I've deleted a group-closing symbol because it seems to be spurious, as in $x}$'. But perhaps the } is legitimate and you forgot something else, as in \hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing I$}'. ! Extra }, or forgotten$.
\text@ ...firstchoice@false #1}}\check@mathfonts }

l.7     \texttt{y^o}

I've deleted a group-closing symbol because it seems to be
spurious, as in $x}$'. But perhaps the } is legitimate and
you forgot something else, as in \hbox{$x}'. In such cases the way to recover is to insert both the forgotten and the deleted material, e.g., by typing I$}'.

! Missing $inserted. <inserted text>$
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing $inserted. <inserted text>$
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing $inserted. <inserted text>$
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing $inserted. <inserted text>$
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing { inserted.
\endgroup
l.8 \end{equation*}

A left brace was mandatory here, so I've put one in.
You might want to delete and/or insert some corrections
so that I will find a matching right brace soon.
(If you're confused by all this, try typing I}' now.)

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing { inserted.
\endgroup
l.8 \end{equation*}

A left brace was mandatory here, so I've put one in.
You might want to delete and/or insert some corrections
so that I will find a matching right brace soon.
(If you're confused by all this, try typing I}' now.)

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing { inserted.
\endgroup
l.8 \end{equation*}

A left brace was mandatory here, so I've put one in.
You might want to delete and/or insert some corrections
so that I will find a matching right brace soon.
(If you're confused by all this, try typing I}' now.)

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text>
}
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Missing $inserted. <inserted text>$
l.8 \end{equation*}

I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing 2' now; then
my insertion and my current dilemma will both disappear.

! Display math should end with $$. <to be read again> \endgroup l.8 \end{equation*} The ' that I just saw supposedly matches a previous $$'.
So I shall assume that you typed ' both times.


So that is 42 separate errors reported. All of them are essentially spurious as TeX tries to dig itself out of teh hole it makes by inserting $ to "recover" from the initial error. \texttt (if amsmath is loaded) is designed to use \text and that produces non-math text but at the right size in subscripts. It uses a \mathchoice primitive for this but the effect is essentially that \text{zzz} is \ifdisplaystyle \mbox{zzz} \else \iftextstyle \mbox{zzz} \else \ifscriptstyle \mbox{\scriptsize zzz} \else \mbox{\scriptscriptsize zzz} \fi\fi\fi  (using imagined \ifdisplaystyle tests) so in each of the four \mbox you hit a ^ in text mode so so tex inserts a $ then hits another error at the }as \mbox{y$^o} is an error as the math isn't closed. So you get multiple errors and as you fall out of math mode at the wrong time all four version send up being output. It is legitimate to use \texttt in math mode: \documentclass{article} \usepackage{amsmath} \begin{document} \makeatletter \def\foo{\texttt{\ifx\f@size\sf@size scriptsize\else \ifx\f@size\ssf@size subscriptsize\else normal\fi\fi}} \makeatother $\foo_{\foo^{\foo}}$ % $\mathchoice{\hbox{a^b}}{X}{Y}{Z}$ \end{document}  If you comment out loading of package amsmath you get: But of course what is illegitimate is to use math mode in the argument without explicitely entering math mode again. You can reproduce similar "repeating" pattern as you observed with $\mathchoice{\mbox{a^b}}{X}{Y}{Z}$  where all four arguments are typeset, the first one being buggy due to missing $'s.

No error with $\mathchoice{\mbox{a^b}}{X}{Y}{Z}$.

And with amsmath loaded \texttt in math mode ultimately uses \mathchoice.