# Teletype \textbackslash in alltt environment

The alltt package provides a beefed up verbatim environment where \ and {} are not treated literally. A literal backslash can be inserted with \textbackslash, however, it does not have the same teletype font as the rest of the verbatim text. I tried some variataions such as \texttt{\textbackslash} but these don't seem to help. Any ideas how to insert a backslash (or curly braces) in teletype font when using alltt?

This has nothing to do with alltt. You need T1-encoding. Without it \textbackslash is taken from the symbol font. Check the difference:

\documentclass{article}
%\usepackage[T1]{fontenc}
\pagestyle{empty}
\begin{document}
\ttfamily \textbackslash abc
\end{document}

• Although using a different font encoding is an interesting approach, Leo's answer has the less "global impact" and I hence accepted it as solution. – mavam Apr 28 '11 at 11:37
• \textbackslash is not the only symbol affected by the lack of chars in OT1. You will perhaps need similar definitions for braces (e.g. \{ or \} \textbraceleft. \textbraceright) and greater/less symbol (\textgreater, \textless)). I advise you to drop OT1. The encoding is obsolete. Load T1 and perhaps even TS1 (e.g. with \usepackage{textcomp}). – Ulrike Fischer Apr 28 '11 at 11:59
• Thanks for clarifying, I read a bit more on OT1 vs T1 (also stumbled over tex.stackexchange.com/questions/664/…). Is my understanding correct that T1 provides the necessary font symbols to render \textbackslash in all variants (such as \ttfamily, \sffamily, etc.) but the default font encoding OT1 does not? – mavam Apr 28 '11 at 22:55
• @Matthias: Yes OT1 encoding has only 128 positions, so Knuth had to sacrifice consistency. E.g. braces and greater/less symbols are only in the typewriter fonts, the other fonts have eg. quotes at this positions. And on position 36 there is a dollar sign in upshape fonts and a pound sign in itshape as you can try out: \char36 \itshape \char36. I don't see any reason to accept such a mess when consistent encodings with 256 positions like T1 exist. – Ulrike Fischer Apr 29 '11 at 8:22

In addition, you don't have to use T1 font encoding. Default OT1 is enough. In this case symbol \ is not available in roman font family, but it does be available for tyrewriter family.

You can use

\symbol{\\}


to get \ at any time. If it is available in the font, a backslash should be output properly. You can define a command for it:

\documentclass{article}
\usepackage{alltt}
\newcommand\bslash{\symbol{\\}}
\begin{document}

\begin{alltt}
\bslash{}^_^/
\end{alltt}

\end{document}


After all, it is a good manner to use T1 encoding for most documents.

while the suggestions to use a different encoding are good, this technique should work anywhere to get a monospace symbol if it's in the font being used.

preface the input character by

\char\


so that a backslash, open and close brace would be input as

\char\\ \char\{ \char\}


this is built into TeX itself, not defined by any particular "flavor" such as LaTeX or ConTeXt.

EDIT: the \char command will reference whatever glyph is in the ascii/utf-8/(relevant font encoding) position corresponding to its argument. so only if the backslash (or whatever) is actually in that location in the font will you get the desired shape. my original answer was meant to refer specifically to cmtt10 and other fonts encoded so that the input character exactly matches the glyph in that font location.

• Well cmsy10 does contain the symbols but you can't access them with your commands: \font\test=cmsy10 \test \char\{ \char\f (the code is messed up due to the highlighting). – Ulrike Fischer Apr 28 '11 at 12:38
• @Ulrike: see meta.tex.stackexchange.com/questions/863/… for how to get Markdown to display that correctly. – Caramdir Apr 28 '11 at 14:40
• @Caramdir Ah, thanks. So you can use either a backslash to escape a quote in the code: ab or double quotes outside: ab  – Ulrike Fischer Apr 28 '11 at 15:02
• @Ulrike -- when i said "monospace symbol", i was thinking specifically of cmtt10; wouldn't expect it to work quite the same way with cmsy10, and actually, wouldn't want to mix cmsy10 with cmtt10 -- that was the complaint of the original poster. @Caramdir -- thanks for the pointer; my answer would have been more compact if i had known about the double quote. – barbara beeton Apr 28 '11 at 15:26
• @barbara: I know that you mean cmtt10. But imho your answer is misleading. It makes the impression as if TeX somehow knows that you want a backslash when you type \char\\. But this command (like the \symbolcommand of Leo) is simply another way to write \char92` and it will give a backslash if and only if the font has a backslash at position 92 - which is the case amongst others for some OT1-encoded fonts and for all T1 encoded fonts. So I wanted to pointed out that it depends on the font if it works or not. – Ulrike Fischer Apr 28 '11 at 16:29