With the commands \openout
and \write
I output text into a file. How can i output a tab ?
\documentclass{article}
\begin{document}
\newwrite\tempfile
\immediate\openout\tempfile="filename.txt"
\immediate\write\tempfile{I want a tab at the beginning of this line}
\immediate\closeout\tempfile
\end{document}
EDIT1
Following egreg's solution, i use \catcode`\^^I=12
. This works if i use directly the code, but not if i enclose the code in a command :
\documentclass{article}
\begin{document}
\newwrite\tempfile
\immediate\openout\tempfile="filename.txt"
\begingroup
\catcode`\^^I=12 %
\immediate\write\tempfile{^^II want a tab at the beginning of this line}
\endgroup
\immediate\closeout\tempfile
\newcommand{\test}{
\newwrite\tempfilee
\begingroup\catcode`\^^I=12 %
\immediate\openout\tempfilee="filename2.txt"
\immediate\write\tempfilee{^^II want a tab at the beginning of this line}
\endgroup
}
\test
coucou
\end{document}
EDIT2 . Thanks again to egreg. The 2nd solution works, but not if i call \test
from inside another command, which is what i need :
\newwrite\tempfile
\immediate\openout\tempfile=\jobname.out
\def\test{\begingroup\catcode`\^^I=12 \testaux}
\def\testaux#1{\immediate\write\tempfile{#1}\endgroup}
\newcommand{\makeit}{
\test{^^I<-TAB again}
}
\test{ <-TAB} % WORKS
\test{^^I<-TAB} %WORkS
\makeit %DOES NOT WORK
\immediate\closeout\tempfile
EDIT 3. The second solution of egreg works ... but not if it is inside a AtEndDocument
:
\documentclass{article}
\AtEndDocument{\test{^^Ifin du document}
\immediate\closeout\tempfile }
\begin{document}
\newwrite\tempfile
\immediate\openout\tempfile=output.txt
\catcode`\^^I=\active
\def^^I{}
\def\test#1{\begingroup
\edef^^I{\string^^I}\immediate\write\tempfile{#1}\endgroup}
\test{^^I<-coucou} %% WORKS
\def\foo{\test{^^I<-Jojo}}
\foo %% WORKS
\end{document}
But the last sentence "fin du document" has no TAB.
^^I
character. Albeit I can give it as an answer I would like to be more generic in my answer, which I am not that good at yet. :)