filecontents replaces tabs with spaces

LaTeX and package filcontents define the filecontents environment with a TAB character active in order to change it into a space. To disable this feature, you have just to destroy this processus for TAB character. Then you have this code:

\documentclass{article}
\makeatletter
\chardef\other=12
\begingroup%
\catcode`\*=11 %
\catcode`\^^M\active%
\catcode`\^^L\active\let^^L\relax%
\catcode`\^^I\other%
\gdef\filec@ntents#1{%
  \openin\@inputcheck#1 %
  \ifeof\@inputcheck%
    \@latex@warning@no@line%
        {Writing file `\@currdir#1'}%
  \else %
    \@latex@warning@no@line%
        {Overwriting file `\@currdir#1'}%
  \fi %
  \closein\@inputcheck %
  \chardef\reserved@c15 %
  \ch@ck7\reserved@c\write%
  \immediate\openout\reserved@c#1\relax%
  \if@tempswa%
    \immediate\write\reserved@c{%
      \@percentchar\@percentchar\space%
          \expandafter\@gobble\string\LaTeX2e file `#1'^^J%
      \@percentchar\@percentchar\space  generated by the %
        `\@currenvir' \expandafter\@gobblefour\string\newenvironment^^J%
      \@percentchar\@percentchar\space from source `\jobname' on %
         \number\year/\two@digits\month/\two@digits\day.^^J%
      \@percentchar\@percentchar}%
  \fi%
  \let\do\@makeother\dospecials%
  \count0=128\relax %
  \loop %
    \catcode\count0=11\relax %
    \advance\count0 by 1\relax %
    \ifnum\count0<256 %
  \repeat %
  \edef\E{\@backslashchar end\string{\@currenvir\string}}%
  \edef\reserved@b{%
    \def\noexpand\reserved@b%
         ####1\E####2\E####3\relax}%
  \reserved@b{%
    \ifx\relax##3\relax%
      \immediate\write\reserved@c{##1}%
    \else%
      \edef^^M{\noexpand\end{\@currenvir}}%
      \ifx\relax##1\relax%
      \else%
          \@latex@warning{Writing text `##1' before %
             \string\end{\@currenvir}\MessageBreak as last line of #1}%
        \immediate\write\reserved@c{##1}%
      \fi%
      \ifx\relax##2\relax%
      \else%
         \@latex@warning{%
           Ignoring text `##2' after \string\end{\@currenvir}}%
      \fi%
    \fi%
    ^^M}%
  \catcode`\^^L\active%
  \let\L\@undefined%
  \def^^L{\@ifundefined L^^J^^J^^J}%
  \catcode`\^^I\other%
  \catcode`\^^M\active%
  \edef^^M##1^^M{%
    \noexpand\reserved@b##1\E\E\relax}}%
\endgroup%
\def\fc@no@preamblecmds#1\do\filecontents#2\do\filec@ntents#3\relax{%
  \gdef\@preamblecmds{#1#3}}
\expandafter\fc@no@preamblecmds\@preamblecmds\relax
\makeatother
\begin{document}
\begin{filecontents*}{textWithTabs.txt}
  Aa    Bb  Cc
  1 2   3
\end{filecontents*}
\end{document}

With the scontents package it is possible to save the TABs character in the <output file>.

\documentclass{article}
\usepackage{scontents}
\usepackage{xcolor,fvextra} % only for example
\pagestyle{empty}
\begin{document}
% Write real tabs
\begin{scontents}[write-out=textWithTabs.txt]
	Aa	Bb	Cc
	1	2	3
\end{scontents}
Show correct TABs in \verb|textWithTabs.txt|.
\VerbatimInput[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red,showspaces, spacecolor=blue]{textWithTabs.txt}
\end{document}
output

Tags:

Filecontents