28

How can I escape curly braces within the \texttt{} environment?

\texttt{ \{ } produces errors within my template, which can be found here: (link)

I saw this question for square braces, but the solution seems like a bit of a hack, and doesn't work for curly braces.

(FYI, the purpose of this is to include a regex statement in monospaced font, among normal font in the rest of a table. If there's a preferable solution, I'm open to ideas there.)

Chris
  • 549

2 Answers2

34

For "escaping" one typically use \, as in \{ and \}. However, for the typewriter font, you might be interested in \string{ and \string}:

enter image description here

\documentclass{article}
\begin{document}
Compare \texttt{\string{\string}} to \verb|{}| and \texttt{\{\}}
\end{document}

\string provides similar output to \verb, but is allowed in moving arguments.

Werner
  • 603,163
12

Use a short verbatim environment, or load the fontenc package.

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
\texttt{\{ Braces \}} \{ Braces \} \verb+ { Braces }+
\end{document}

braces

The braces in the middle version are 'ordinary' text; the ones you need are on the left and the right. Without fontenc, the first method will give the wrong symbols. This answer explains what's going on.

Ian Thompson
  • 43,767
  • This is good -- totally forgot about the ability to change the delimiter with \verb. – cslstr May 29 '14 at 18:39
  • Thank you - both are valid options. In this situation, I'll use Werners \string{ instead, as it appears \verb doesn't work within the tabular environment - and I presume I'm better to avoid loading a full package where possible. Thanks for your answer! – Chris May 29 '14 at 18:47
  • another technique that works is \textt{\char \{} (with a backtick between \char and the \{, which can't easily be shown here in comments) and similarly for the closing brace. it's a plain tex/"old style" convention, but that's what i learned first and old habits die hard. – barbara beeton May 29 '14 at 19:15
  • The stack-exchange comments are peculiar. I can surround the inline code with two backticks to include a single backtick in the code span: \textt{\char`\{}, but I can't surround the inline code with three backticks to show how it was done. – Witiko Mar 04 '18 at 15:47