38

How do you go about writing a backslash inside \texttt? The $\backslash$ trick makes the font inconsistent. The reason I am asking is that I am writing some introductory computer science material and need to talk about different escape characters in printf and other functions. The verbatim environment adds new lines while I would like my finished product to be strictly inline.

0

5 Answers 5

24

How about using \verb instead of \texttt?

The first exercise uses \verb$printf("Hello world!\n");$. 

You can use (almost) any character you like instead of $.

Better still, if you're typesetting a lot of code, use the listings package that provides a \lstinline macro, equivalent to \verb but with syntax highlighting.

3
  • Great! Didn't know that verbatim had an inline shortcut. That is really handy! Commented Sep 4, 2013 at 23:22
  • 10
    There is also \texttt{printf("Hello world!\string\n");}. Commented Sep 4, 2013 at 23:37
  • 2
    Warning: \verb does not work inside the argument of another command! Commented Sep 22, 2021 at 17:47
39

You can use \textbackslash rather than $\backslash$ to keep the current font. With $..$ you change to math font.

\texttt{This is a backslash: \textbackslash.} 
6
  • 5
    I actually noticed the the font changes from monospace to normal text when using pdftex at least. It was one of the first things I tried. Commented Sep 5, 2013 at 19:42
  • 5
    With pdf(la)tex the symbol is not available for the standard typewriter font. I tested with lualatex and the fontspec package: \documentclass{memoir} \usepackage{fontspec} \begin{document} \noindent \textbackslash\ \ldots the backslash in roman font.\ \texttt{\textbackslash\ \ldots the backslash in typewriter font.} \end{document} Commented Sep 6, 2013 at 7:59
  • This is good to know. I will start fiddling with other tex implementations. Also thank you for catching the pdf(la)tex issue! I somehow always forget that even when talking about it... which of course is bad seeing that it refers to something completely different. Commented Sep 6, 2013 at 13:35
  • 1
    Kevin, could you explain what does "the symbol is not available for the standard typewriter font" actually mean? \verb and \typettt seems to use the same font, so it's not clear to me how the monospaced backslash can exist for \verb and not exist for \typettt. Commented Aug 27, 2021 at 10:33
  • 3
    If you use T1 encoding this will use the expected font, it only uses the math font if the legacy OT1 encoding is used. Commented Sep 22, 2021 at 18:57
26

or you can try: \texttt{\char`\\} (for me it looks better than \texttt{\textbackslash})

3
  • 4
    @Downvoter Please do not downvote a new user before you haven't commented and waited for a response. Now that your vote has been made please leave a comment why you did so and a proposal on how to improve the answer. Commented Aug 7, 2014 at 7:52
  • @ChrisPer Welcome to TeX.SX! Commented Aug 7, 2014 at 7:55
  • 7
    +1 because this is a method applicable for any ASCII character (more or less special in TeX) and usable in macro parameters (where \verb cannot be used). For example \texttt{\char`\{}, \texttt{\char`\^} etc. Commented Aug 7, 2014 at 8:03
6

\textbackslash{} looks the best choice. It may be used alone as well as in \texttt{'\textbackslash{}xAA'} (to get '\xAA') and alikes.

2

Use \symbol{92}. The same trick works for $, & and other special characters.

1
  • 1
    \symbol{`\\} is easier (no lookup in ASCII table). Commented Feb 1, 2023 at 10:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.