21

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.)

  • Something like \texttt{This has \{brackets\} in it}? – cslstr May 29 '14 at 18:37
  • Nope. Apologies, should have added what I'd tried! – Chris May 29 '14 at 18:38
  • It's not too late to add a MWE :) – cslstr May 29 '14 at 18:38
  • @Chris what you tried will work unless you are using 7bit OT1 encoded fonts which don't have { (except in typewriter) there is very little reason to ever be using those now. If you use T1 encoded fonts \{ works as shown in Ian's answer – David Carlisle May 29 '14 at 18:45
  • In your edit you say \texttt{\{} produces errors. That should not produce an error (but may use the wrong font if you use OT1) Please always post a complete document that demonstrates the problem – David Carlisle May 29 '14 at 18:48
23

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.

  • 1
    Thank you - \string{ is the one for me! – Chris May 29 '14 at 18:47
10

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.

  • 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 4 '18 at 15:47

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.