I need to build one environment that obeys spaces, but not new lines, and also change tabulation into four spaces.

I can't use verbatim because I would also like to use macros in my environment.

  • 4
    \obeyspaces is for obeying spaces. About tabulation, I'm not sure what you mean, but you should be able to realise that with tabbing? Apr 29, 2012 at 13:42

3 Answers 3


The alltt environment from the package of the same name in the base distribution does most of that. For tab characters do you always want them to be four spaces (easy) or to tab to the next multiple of 4 (possible, but probably best handled by your editor)


I would tackle this by making spaces and tabs active. You could do this directly, but using the 'lower case trick' is my favoured approach:

\lccode`\@=`\ % At-sign is a space
\lccode`\!=9 % Exclamation mark is a tab
    \catcode`\ =\active % Space active
    \catcode 9 =\active % Tab active
    \def!{~~~~}% % Tab as four spaces
Some text with active   spaces  !% That's a tab just before the "!"
More text
Phew,   back   to normal!
  • Thanks but indeed the alltt package is the good one for my purpose.
    – projetmbc
    Apr 29, 2012 at 14:05

For tabs you may want to use fancyvrb facilities:

This    has tabs
\textit{even}   with \textit{italic}

The showtabs option is just to show the presence of tabs and is not necessary. The tabsize value can be any integer (default 8). You can also change the font used in the environment.

  • @projetmbc: What egreg showed you implicitly: You can use macros inside of fancyvrb, you only need to define the active chars first with commandchars, where they have to be escaped as seen in the example above. You can even use commands with an optional parameter (in square brackets), then!
    – Speravir
    Apr 29, 2012 at 20:18

You must log in to answer this question.

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