# How to escape curly braces in \texttt inside math mode?

In a previous question was answered how to escape curly braces within \texttt{}. On answer suggest using the fontenc package.

\documentclass{article}
\usepackage[T1]{fontenc}

\begin{document}
$$a + \underbrace{\mbox{\tt\{c\}}}_d$$
\end{document}


Another answer suggest using \string{. However, in this case I got an error. Sorry, I do not get why it was raising an error. Example bellow works fine.

\documentclass{article}

\begin{document}
$$a + \underbrace{\mbox{\tt\string{c\string}}}_d$$
\end{document}


I now that in this case fontenc solves the problem. Thus, my question is how can I this without using fontenc. I do not see problems with fontenc is only curiosity.

• What do you want to do? In a normal document, there is no error with your code and I don't need fontenc for it. Do you want to write it to some file? – user31729 Oct 7 '14 at 14:28
• what output do you expect from this? It is hard to guess. given \texttt\string the argument to \texttt is \string which will then generate an error. You don't need \mbox around \texttt so what do you want that is different to \underbrace{\texttt{\{c\}}}} – David Carlisle Oct 7 '14 at 14:33
• I added the example that produces the error. I want to get the same result that I get with the code above with fontenc but without using it. – Daniel Hernández Oct 7 '14 at 14:35
• No, your examples can not be run on their own, if you want to demonstrate an error please make a complete document that makes the error, the code I posted in the previous comment should not generate an error – David Carlisle Oct 7 '14 at 14:36
• Thanks all. I see that my question was not clear enough. So, I will improve the question description. – Daniel Hernández Oct 7 '14 at 14:40

The problem with \{ and \} is that, if the current font encoding is OT1, the braces will be taken from the symbol font.

You have two strategies:

\documentclass{article}

\begin{document}

$\underbrace{\texttt{\string{c\string}}}$

\end{document}


or the simpler

\documentclass{article}
\usepackage[T1]{fontenc}

\begin{document}

$\underbrace{\texttt{\{c\}}}$

\end{document}


The output is the same (well mostly indistinguishable).

• Thanks, I do not get why I was getting that error message. Now my code also works. – Daniel Hernández Oct 7 '14 at 15:09
• @DanielHernández Don't use \tt under any circumstance; either \texttt{<text>} or \ttfamily <text> inside a group. – egreg Oct 7 '14 at 15:31
• Why do not use \tt? Any reference to related problems? – Daniel Hernández Oct 7 '14 at 23:40
• @DanielHernández Try \documentclass{memoir}\begin{document}\tt a\end{document}. The two letter commands for changing fonts have been deprecated for 20 years. – egreg Oct 8 '14 at 6:22

Please always post complete documents not fragments. \{ produces a { It is hard to guess from your code fragments what output you want but if it is

Then you just need

\documentclass{article}

\usepackage[T1]{fontenc}

\begin{document}

$\underbrace{\texttt{\{c\}}}$

\end{document}

• Sorry, my question was not clear enough. Also I do a mistake with my code. Now I do not get that error. – Daniel Hernández Oct 7 '14 at 15:10