Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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, amoung normal font in the rest of a table. If there's a preferable solution, I'm open to ideas there.)

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

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
1  
Thank you - \string{ is the one for me! –  Chris May 29 at 18:47
add comment

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.

share|improve this answer
    
This is good -- totally forgot about the ability to change the delimiter with \verb. –  cslstr May 29 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 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 at 19:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.