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.
5 Answers
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.
- Great! Didn't know that
verbatimhad an inline shortcut. That is really handy!Thomas James– Thomas James2013-09-04 23:22:14 +00:00Commented Sep 4, 2013 at 23:22 - 10There is also
\texttt{printf("Hello world!\string\n");}.2013-09-04 23:37:16 +00:00Commented Sep 4, 2013 at 23:37 - 2Warning:
\verbdoes not work inside the argument of another command!dvitek– dvitek2021-09-22 17:47:03 +00:00Commented Sep 22, 2021 at 17:47
You can use \textbackslash rather than $\backslash$ to keep the current font. With $..$ you change to math font.
\texttt{This is a backslash: \textbackslash.} - 5I 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.Thomas James– Thomas James2013-09-05 19:42:42 +00:00Commented Sep 5, 2013 at 19:42
- 5With 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}Kevin Bader– Kevin Bader2013-09-06 07:59:54 +00:00Commented 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.Thomas James– Thomas James2013-09-06 13:35:38 +00:00Commented Sep 6, 2013 at 13:35
- 1Kevin, could you explain what does "the symbol is not available for the standard typewriter font" actually mean?
\verband\typetttseems to use the same font, so it's not clear to me how the monospaced backslash can exist for\verband not exist for\typettt.user90726– user907262021-08-27 10:33:27 +00:00Commented Aug 27, 2021 at 10:33 - 3If you use T1 encoding this will use the expected font, it only uses the math font if the legacy OT1 encoding is used.David Carlisle– David Carlisle2021-09-22 18:57:53 +00:00Commented Sep 22, 2021 at 18:57
or you can try: \texttt{\char`\\} (for me it looks better than \texttt{\textbackslash})
- 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.Henri Menke– Henri Menke2014-08-07 07:52:12 +00:00Commented Aug 7, 2014 at 7:52
- @ChrisPer Welcome to TeX.SX!LaRiFaRi– LaRiFaRi2014-08-07 07:55:09 +00:00Commented 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
\verbcannot be used). For example\texttt{\char`\{},\texttt{\char`\^}etc.wipet– wipet2014-08-07 08:03:24 +00:00Commented Aug 7, 2014 at 8:03
\textbackslash{} looks the best choice. It may be used alone as well as in \texttt{'\textbackslash{}xAA'} (to get '\xAA') and alikes.
Use \symbol{92}. The same trick works for $, & and other special characters.
- 1
\symbol{`\\}is easier (no lookup in ASCII table).egreg– egreg2023-02-01 10:06:43 +00:00Commented Feb 1, 2023 at 10:06