filecontents package replaces tabs with spaces. Is there any solution to preserve tabs in the generated file without changing the following code?

Aa    Bb    Cc
1     2     3

Generated file is:

Aa Bb Cc
1 2 3

Desired file is:

Aa    Bb    Cc
1     2     3
  • 4
    That's a good question. texdoc filecontents says nothing about that point.
    – Clément
    Jul 9 '14 at 12:37
  • 6
    texdoc usrguide documents the original filecontents environment, before the environment is patched by the filecontents package. Quoting the document: Only normal ASCII text characters (7-bit visible text) should be included in a filecontents environment. Anything else, such as tab characters, form-feeds or 8-bit characters, should not be included in a filecontents environment. There is more text about the conversion to spaces. The document does not suggest any solution around this limitation.
    – mvkorpel
    Jul 9 '14 at 12:50
  • 1
    @mvkorpel: That is interesting information, but does the package circumvents this? I found also nothing about this, as Clément already stated. The package filecontents version claims about verbatim writing the content, in this case, the tab characters should be preserved.
    – user31729
    Jul 9 '14 at 12:58
  • 3
    Related (and showing why this is a problem): How to output a tabulation into a file
    – egreg
    Jul 9 '14 at 13:08
  • 1
    @mvkorpel: Sorry about verbatim writing... I meant, as stated in filecontents package manual filecontents works much like verbatim ... I misinterpreted this, that verbatim is used.
    – user31729
    Jul 9 '14 at 13:43

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

\usepackage{xcolor,fvextra} % only for example
% Write real tabs
	Aa	Bb	Cc
	1	2	3
Show correct TABs in \verb|textWithTabs.txt|.
\VerbatimInput[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red,showspaces, spacecolor=blue]{textWithTabs.txt}


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:

\catcode`\*=11 %
  \openin\@inputcheck#1 %
        {Writing file `\@currdir#1'}%
  \else %
        {Overwriting file `\@currdir#1'}%
  \fi %
  \closein\@inputcheck %
  \chardef\reserved@c15 %
          \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 %
  \count0=128\relax %
  \loop %
    \catcode\count0=11\relax %
    \advance\count0 by 1\relax %
    \ifnum\count0<256 %
  \repeat %
  \edef\E{\@backslashchar end\string{\@currenvir\string}}%
          \@latex@warning{Writing text `##1' before %
             \string\end{\@currenvir}\MessageBreak as last line of #1}%
           Ignoring text `##2' after \string\end{\@currenvir}}%
  \def^^L{\@ifundefined L^^J^^J^^J}%
  Aa    Bb  Cc
  1 2   3
  • Nice work, I can't see which lines have been modified, can you explain the modifications made?
    – AndréC
    Sep 4 '17 at 7:08
  • I have created the \other code (catcode 'other' is 12). The catcode of character ^^I has became \other (twice). I have removed the definition of ^^I since it was no more an active char (catcode 13). That's all. Sep 5 '17 at 16:49
  • I do not have a problem with the principle, but with its practical implementation in 71 lines of code. Which lines (numbers) are modified?
    – AndréC
    Sep 5 '17 at 17:27
  • 1
    line 3 is a created one. Lines 8 and 65 are changed. In original code there was some lines after line 65 which define ^^I. These lines are removed in my code. Sep 12 '17 at 21:35

Works fine. works like verbatim.

Aa          Bb             Cc
1           2              3


I got

enter image description here

  • 3
    This does not work, after replacing the spaces with tabs, on a mac with current texlive. Are you sure the tabs in the input file were not converted to spaces by the editor? Feb 6 '17 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.