I just want a good way to make a backslash character (or whatever character(s) any given format uses as escape characters), suitable to \write to a file or pass to a pdfTeX primitive.

Let me emphasize that getting it to typeset is not the point of the question. In fact, if using cmr10/11/12, what I want would not be usable to typeset a backslash, since these fonts use that slot for something else for some reason.

An acceptable answer will say how to do this in general, not just in LaTeX, though format-specific techniques are also of interest (and will be upvoted).

Bonus points for explaining how to do this for all characters with special catcodes. (Not literal bonus points, only figurative ones. At least, not from me; I only have 296 rep at the moment!)

  • FYI, my personal motivation for wanting to do this was that I needed a backslash for a directory separator in order to give a working implementation in my answer to a question about how make a PDF hyperlink open a given file in a particular application, with the concrete example task "open c:\figure.bmp in MS Paint".
    – SamB
    Commented Dec 25, 2010 at 6:21
  • What I actually used there was taken from @TH.'s answer to a less targetted question, and is nearly the same as @Hendrik's answer here, except with curly braces instad of \begingroup and @endgroup, but that seems to have no actual impact on the meaning in this case, as far as I can see.
    – SamB
    Commented Dec 25, 2010 at 6:28
  • @Hendrik: True. I've been meaning to accept one, but hadn't felt ready to decide which to accept. I hope that's changed, because I've just accepted one :-).
    – SamB
    Commented Jan 21, 2011 at 17:34

5 Answers 5


Another way to do this kind of thing is to use a \lccode trick:


This avoids any need to change catcodes thanks to the special properties of \lowercase. Compared to the \expandafter\@gobble\string method, it's a bit more flexible as you can choose the catcode of the resulting character inside your macro. Compared to a change of catcodes, you don't have to use \global because of how \lowercase interacts with \endgroup.

Here's the detail of the code. The \begingroup is here to keep the lccode changes local. The


means that when lowercasing ! you will get the backslash \ (the choice of ! is arbitrary, you just need a normal character which will not appear elsewhere inside the \lowercase). The important thing is that the lowercase version of ! wille be a normal character since \lowercase doesn't change catcodes.

The code \lowercase{\endgroup\def\@backslashchar{!}} is thus equivalent to \endgroup\def\@backslashchar{\} but with \ not being special (so \ followed by } won't be interpreted as \} but as two separate entities).

At the end, the result is that you have defined \@backslashchar to be a \ with normal catcode. It can thus be used inside a \write command without causing the same problems as \ does.

This lccode trick works with all other special characters. For example if you want a space character (of course, for spaces, the macro \space works fine):

\begingroup\lccode`!=`\ \lowercase{\endgroup\def\@spacechar{!}}

If ever you break the code on two lines, just be careful with the end of lines and put a % after the \:

\begingroup\lccode`!=`\ %
  • That is nice and tidy. Now, you say I get to pick the catcode, but do I get to pick any catcode? As Hendrik and TH. mention, I can pick from at least 11 and 12 with the other technique already, so what options does this technique offer that that one does not?
    – SamB
    Commented Dec 25, 2010 at 6:13
  • 1
    @SamB: With the \expandafter\@gobble\string method you are limited to catcode 12. With the lccode method, you can obtain the catcodes of $, &, ^, _, ~ and of course 11 and 12. With the \catcode method, you can pick whichever catcode you want. But, unlike catcodes, lccode changes work inside the argument of a command so that's another advantage over the other methods. Commented Dec 26, 2010 at 19:51
  • I just tried \@backslashchar, and while it works great with \write (what I needed it for), then it seems to crash my file on second run of pdflatex - failing on \\ linebreaks (which previously worked as normal) with the error: "! Use of \\ doesn't match its definition." ... Any suggestions how I'd address this? Thanks - cheers!
    – sdaau
    Commented May 17, 2011 at 16:46
  • @myself :) - ok, turns out I had an error, that effectuated itself once this \@backslashchar started working - and that ended up spitting a "gdef\\..." in the .aux file, which redefined the usual \\ ... So all seems to be fine now :)
    – sdaau
    Commented May 17, 2011 at 17:33

There are many ways to do it depending on what you want to do. You don't always need to change the catcode. Some examples:

\textbackslash will simply typeset \

For writing a macro to a file use \string\macro. This can also be used to just typeset it, also.

You can also define it by using:

\newcommand{\bs}{\symbol{'134}} % a backslash in tt type in OT1/T1

The \lccode trick and changing \catcode has been explained by the other posters and I haven't added it.

And a minimal with these:

\newcommand{\bs}{\symbol{'134}}% a backslash in tt type in OT1/T1

\BS, \texttt{\string\test}, \texttt{\bs}
  • Hi @Yiannis Lazarides, thanks for the tip on \textbackslash! Just a small note: \typeout{==\textbackslash==} for me outputs "==\textbackslash ==" instead of ==\==. Cheers!
    – sdaau
    Commented May 17, 2011 at 16:19
  • @sdaau: Yiannis did only say that \textbackslash would typeset a backslash, so, what did you expect?
    – SamB
    Commented May 17, 2011 at 17:18
  • @SamB - that's correct, sorry, I wasn't paying too much attention when I read at first..
    – sdaau
    Commented May 17, 2011 at 17:35
  • @sdaau: (Belated, I know.) Hmm, I may need to work on "don't bite"; I wouldn't want to accidentally bite newcomers ...
    – SamB
    Commented Mar 24, 2014 at 17:23

Here's one possible way with changing catcodes (just saw that TH. proposed almost the same in this answer):


Note that I first give @ catcode 0 so that I can use it in place of \. For other special characters it's in fact easier; you don't need the @.

As Philippe pointed out, the drawback of the above approach is the use of \global. Here's another nice trick that avoids this:


The macro @makeliteralbackslash is expanded before the end of the group; one expansion happens while the catcode changes are still active. The definition of \backslash itself then is executed outside the group. I'm not claiming that this is the best possible approach: It's just to give an example of interesting TeX core tricks.

  • It probably doesn't make much difference, but I'd probably go with 12 instead of 11.
    – TH.
    Commented Dec 19, 2010 at 3:03
  • @TH.: I've thought about that, but indeed I didn't really see why I should prefer which. Of course the code work's with either. Commented Dec 19, 2010 at 7:26
  • Yeah, I'm not sure I can give a convincing reason for other over letter. Best I've got is \ isn't a letter.
    – TH.
    Commented Dec 19, 2010 at 13:04

LaTeX core appears to do this:


I don't properly understand how this works, but it does seem to do what you want in \write.

  • It doesn't seem to matter if <code>\\</code> is a macro or not.
    – TH.
    Commented Dec 18, 2010 at 23:37
  • You know what, that was from a previous iteration of experimenting, when I set the catcode of @ wrong; the complaint I got was "Argument of \\ has an extra }." Fixed.
    – zwol
    Commented Dec 18, 2010 at 23:48

expl3 solutions, for future references in case anyone come across this.

\iow_char:N \\

Semantically, it makes sense to use an iow function for the purpose of writing; although there's no guarantee on the number of expansion steps etc. or that you actually get a catcode-other backslash token.

(i.e. technically catcode letter/alignment/math superscript/math subscript works for \write as well, although it would be a rather weird choice to do that)

\cs_to_str:N \\

In practice, (at least in my version) \iow_char:N is identical to \cs_to_str:N. This expands to the result in 2 expansion steps.

\char_generate:nn {`\\} {12}

Again, guarantees to expand to a catcode-other backslash token in 2 expansion steps.

You must log in to answer this question.

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