filecontents 将制表符替换为空格

filecontents 将制表符替换为空格

filecontents包将制表符替换为空格。 有没有解决方案可以在不更改以下代码的情况下保留生成的文件中的制表符?

\begin{filecontents*}{textWithTabs.txt}
Aa    Bb    Cc
1     2     3
\end{filecontents*}

生成的文件是:

Aa Bb Cc
1 2 3

所需文件是:

Aa    Bb    Cc
1     2     3

答案1

使用该scontents包可以将TABs 字符保存在 中<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}
输出

答案2

LaTeX 和 filcontents 包定义了 filecontents 环境,其中 TAB 字符处于活动状态,以便将其更改为空格。要禁用此功能,您只需销毁 TAB 字符的此进程。然后您有以下代码:

\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}

答案3

运行良好。像逐字一样运行。

\documentclass{article}
\usepackage{filecontents}
\begin{document}
\begin{filecontents*}{textWithTabs.txt}
Aa          Bb             Cc
1           2              3
\end{filecontents*}

\end{document}

我有

在此处输入图片描述

相关内容