37

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

23

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! Sep 4, 2013 at 23:22
  • 9
    There is also \texttt{printf("Hello world!\string\n");}.
    – Werner
    Sep 4, 2013 at 23:37
  • 1
    Warning: \verb does not work inside the argument of another command!
    – dvitek
    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
  • 4
    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. 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} 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. 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.
    – user90726
    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. Sep 22, 2021 at 18:57
23

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. Aug 7, 2014 at 7:52
  • @ChrisPer Welcome to TeX.SX!
    – LaRiFaRi
    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.
    – wipet
    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.

1

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

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

You must log in to answer this question.

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