[Tex/LaTex] How to replace tabs with spaces before compiling .tex file

indentationpdftexverbatim

I want to indent text in the verbatim environment. I want to do this with the tabulator. The problem is that verbatim does not work as advertised ("prints unmodified text"). It actually replaces the tabs with spaces and manages to condense them all together, see MWE below. To work around this, one can substitute the tabs with spaces.

\documentclass{article}

\begin{document}
I'm regular text
\begin{verbatim}
I have no tab
    I have 1 tab
        I have 2 tabs

I have no space
    I have 4 spaces
        I have 8 spaces

\end{verbatim}
\end{document}

MWE output

I much prefer workign with tab when editing the text. After all, this is its intended purpose. To keep working with tabs, but getting the desired result, which requires spaces, I somehow have to convert those tabs to spaces.

How can I do this?
Is there a command line parameter that I can pass to do such a replacement? I really like the simplicity of the verbatim environment, but maybe I am asking for too much with this and should instead use some other package? If so, which one?
Or can verbatim be configured to work with tabs somehow?

I use pdflatex --file-line-error-style "%f" to convert my tex to pdf, btw, where %f is the file


The expand command is exactly what I was looking for. I do not want to create an intermediate file, so I pipe the output into pdflatex

**before: ** Compiling the MWE above (first line is the used command) without expand:

pdflatex --file-line-error-style "tab test.tex"

This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian)
 restricted \write18 enabled.
entering extended mode
(./tab test.tex
LaTeX2e <2011/06/27>
Babel <3.9h> and hyphenation patterns for 9 languages loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo)) (./tab test.aux)
[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./tab test.aux) )</usr/
share/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb></usr/share/t
exlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmtt10.pfb>
Output written on "tab test.pdf" (1 page, 23531 bytes).
Transcript written on "tab test.log".

after: Compiling the MWE with expand, piped to pdflatex. For some reason the output is a bit longer, with what looks like empty lines, but the pdf is created with the proper name and the tabs are replaced. Awesome!

expand -t 2 tab\ test.tex | pdflatex --file-line-error-style -jobname="tab test"

This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian)
 restricted \write18 enabled.
**entering extended mode
LaTeX2e <2011/06/27>
Babel <3.9h> and hyphenation patterns for 9 languages loaded.

*(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(Please type a command or say `\end')
*(./tab test.aux)
*
*
*
*
*
*(Please type a command or say `\end')
*
*
*
*(Please type a command or say `\end')
*
*[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./tab test.aux)</usr/sh
are/texlive/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb></usr/share/tex
live/texmf-dist/fonts/type1/public/amsfonts/cm/cmtt10.pfb>
Output written on "tab test.pdf" (1 page, 23537 bytes).
Transcript written on "tab test.log".

Best Answer

As Mike Renfro points out in the comments, many text editors support converting between tabs and spaces; some will even make the change seamless and transparent by printing some number of blank spaces when you press the tab key. This is probably the easiest answer to your exact question.

Another solution if you happen to be a Linux (maybe OSX) user would be to use the expand command. On the command line if you run expand -t 4 file.tex > new_file.tex all of the tabs in file.tex will be converted to 4 spaces as the file is replicated in new_file.tex. More information is available on the expand command.

See also unexpand.