6

I am using verbatim to put some sample code in my document:

\begin{verbatim}  
[start]        printf{"Start command received"};
[stop]         printf{"Stop command received"};
\end{verbatim}

I would like latex to preserve the tabs but instead what I am getting as output is this:

[start] printf{"Start command received"};
[stop] printf{"Stop command received"};

As you can see the tab is converted to a space. Is it not defeating the entire purpose of writing something inside verbatim? Is this normal behaviour for the verbatim environment?

P.S. I am using TexLive 2014 and TexStudio 2.8.8

3
  • 1
    Depends on your point of view: the usual TeX position is that tabs are not a good plan as they are visually the same as spaces. I note that StackExchange agree: your 'literal' text here contains spaces :-)
    – Joseph Wright
    Commented Mar 3, 2015 at 10:25
  • so this means if I have a long code section I have to convert all the tabs to spaces? Commented Mar 3, 2015 at 10:27
  • No: there are fixes, don't worry (hence that being a comment not an answer!)
    – Joseph Wright
    Commented Mar 3, 2015 at 10:30

1 Answer 1

10

Use fancyvrb:

\documentclass{article}

\usepackage{fancyvrb}

\begin{document}

Some text to surround the verbatim
\begin{Verbatim}[obeytabs]
[start]         printf{"Start command received"};
[stop]          printf{"Stop command received"};
\end{Verbatim}
Some text to surround the verbatim
\begin{Verbatim}[obeytabs,tabsize=4]
[start]         printf{"Start command received"};
[stop]          printf{"Stop command received"};
\end{Verbatim}
Some text to surround the verbatim

\end{document}

enter image description here

(Note: the real input file has tabs, but StackExchange seems to convert them to spaces.)

8
  • Drat: was just writing much the same but with listings as well :-)
    – Joseph Wright
    Commented Mar 3, 2015 at 10:33
  • 1
    @SaprativaBhattacharjee I certainly wouldn't undertake the task. What's wrong in loading very stable packages like fancyvrb?
    – egreg
    Commented Mar 3, 2015 at 10:40
  • 1
    @SaprativaBhattacharjee the verbatim environment defined by the verbatim package can do this, but fancyvrb or listings have a lot more features Commented Mar 3, 2015 at 10:57
  • 1
    @SaprativaBhattacharjee As the standard verbatim environment does not do any parsing of the its content, it would be a major undertaking (relatively) to extend it to deal with the variable length requirement for tabs. The fancyvrb package has already done the work: what's the issue?
    – Joseph Wright
    Commented Mar 3, 2015 at 11:02
  • 1
    @SaprativaBhattacharjee -- the standard verbatim environment, as already noted, does not do any parsing. a trivial change, to only recognize a tab and "space over" a fixed distance, could leave the texts that you intend to be aligned, not aligned, since it would be additive. not satisfactory at all. Commented Mar 3, 2015 at 16:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .