5

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".
  • 1
    Just an FYI to anyone grabbing the MWE for testing: the site's interface converts all tab characters to spaces. – Paul Gessler Jun 16 '15 at 13:36
  • 1
    I'd guess it stems from there being no standardized definition of what a tab character means: most commonly 8 spaces, but editors often use 4 spaces, and it's commonly user-definable. I tend to set my editors to convert tabs into spaces automatically. If there's an alternative that meets your needs, I'd be interested to see it. – Mike Renfro Jun 16 '15 at 13:38
  • You might want to have a look at the listings package. – GuM Jun 16 '15 at 13:39
  • 1
    You can also use the fancyvrb package and the Verbatim environment. – Ulrike Fischer Jun 16 '15 at 13:41
  • @MikeRenfro I really dislike it when the indentation is done via spaces. I much prefer to keep the tab for the very reason that it is not standard and I can set my editor up the way I like it. I guess this is comparable to typing the letter "a" and to define how that looks by choosing a font, instead of somehow hard coding the look into the document, say by including a bitmap image of an "a". I like the tab as a single symbolic placeholder white space character thing, that is not replaced with spaces when editing. sorry if I am unclear, this is hard for me to describe – null Jun 16 '15 at 14:54
3

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.

  • 1
    emacs has the untabify command which is quite nice for converting tabs to spaces. (i haven't found a command to reverse the change reliably, but that doesn't mean there isn't one, only that i may not have looked hard enough.) – barbara beeton Jun 16 '15 at 14:23
  • I am a linux user. I piped the output of expand to pdflatex, which solves the problem. I added the results to my question. Do you know why the output is different when I use the pipe? (I see how this is off topic on tex, but you suggested it, that's why I ask) – null Jun 16 '15 at 14:38
  • @barbarabeeton - C-h f tabify – Arash Esbati Jun 16 '15 at 16:08
  • @ArashEsbati -- thanks! (sort of obvious if you think about it, but not something i would want to try on a large and important file when i'm in a hurry, without being absolutely sure. have now tried it on (a copy of) such a file, and it works beautifully!) – barbara beeton Jun 16 '15 at 16:19
7

Use the fancyvrb package:

\documentclass{article}
\usepackage{fancyvrb}

\fvset{tabsize=4}

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

enter image description here

The site is changing tabs to four spaces, so this might seem cheating, but in my file I have tabs.

  • There's no cheating involved, this works fine =) This looks cleaner than replacing the tabs. – null Jun 16 '15 at 14:44
  • 1
    @null He's saying that when you post code on the site the tabs are converted to spaces and so if you copy&paste the code from this answer you don't see any tab. This is sometimes an issue, especially in programming sites. That's why you should use spaces when you want to share the code here: if you use tabs the automatic conversion may change the behaviour of the program/output of the latex file. – Bakuriu Jun 16 '15 at 20:52
  • @Bakuriu thank you for the clarification, although somebody mentioned it in another comment already. I made this comment mainly to say that I tried this and it works, but this is good when somebody with a similar problem finds this question later. – null Jun 16 '15 at 21:31

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.