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

9
  • Something like \texttt{This has \{brackets\} in it}?
    – cslstr
    May 29, 2014 at 18:37
  • Nope. Apologies, should have added what I'd tried!
    – Chris
    May 29, 2014 at 18:38
  • It's not too late to add a MWE :)
    – cslstr
    May 29, 2014 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 May 29, 2014 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 May 29, 2014 at 18:48

2 Answers 2

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.

1
  • 2
    Thank you - \string{ is the one for me!
    – Chris
    May 29, 2014 at 18:47
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.

4
  • This is good -- totally forgot about the ability to change the delimiter with \verb.
    – cslstr
    May 29, 2014 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, 2014 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. May 29, 2014 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, 2018 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .