views:

2068

answers:

2

Hi,

I want to write a backslash character to a text file using LaTeX.

The first line of code below declares a variable 'file' which describes the file 'myfile.out'. The second line opens the file and the third one tries do write a backslash '\' to the file.

\documentclass{article}
\begin{document}
   \newwrite\file%
   \immediate\openout\file=myfile.out%
   \immediate\write\file{\}%
\end{document}

The third line does not work because LaTeX get confused with the backslash, anyone knows how can I make it work? I tried a lot of things including \textbackslash, $\backslash$ \char `\\ etc and nothing seems to work.

Thanks a lot

A: 

Sound like you want a backslash in text mode; since \backslash does not work, try \textbackslash.

EDIT: \symbol{92} should also work.

Martijn
\textbackslash gives weird error: TeX capacity exceeded, sorry
Helltone
Using \symbol{92}, I get a myfile.out which contains '\char 92\relax ' seems like it misses an expansion or something.
Helltone
Maybe there are some issues with writing to text files that I'm unfamiliar with -- didn't expect this to give an error.
Martijn
I'm surprised this doesn't work, as well. Explanation, anyone?
Charles Stewart
A tentative explanation is that I think that \symbol{92} is a macro that translates to \char`92, which asks to the compiler's backend to insert the character with given number in the current font. As there is no backend when writing to files, the \char`92 macro remains unexpanded in the output.
Helltone
+5  A: 

You can use \@backslashchar. The following works for me:

\documentclass{article}
\begin{document}
\newwrite\file
\immediate\openout\file=myfile.out
\makeatletter
\immediate\write\file{\@backslashchar}
\makeatother
\closeout\file
\end{document}
Alok
it works, thanks!
Helltone
\textbackslash also works for me
o_O Tync