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

  • 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 Mar 3 '15 at 10:25
  • so this means if I have a long code section I have to convert all the tabs to spaces? – Saprativa Bhattacharjee Mar 3 '15 at 10:27
  • No: there are fixes, don't worry (hence that being a comment not an answer!) – Joseph Wright Mar 3 '15 at 10:30
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.)

  • Drat: was just writing much the same but with listings as well :-) – Joseph Wright Mar 3 '15 at 10:33
  • 1
    @SaprativaBhattacharjee I certainly wouldn't undertake the task. What's wrong in loading very stable packages like fancyvrb? – egreg Mar 3 '15 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 – David Carlisle Mar 3 '15 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 Mar 3 '15 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. – barbara beeton Mar 3 '15 at 16:01

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.