11

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? – Stephan Lehmke Apr 29 '12 at 13:42
7

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)

15

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:

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

For tabs you may want to use fancyvrb facilities:

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\begin{Verbatim}[commandchars=\\\{\},tabsize=8,showtabs]
This    has tabs
\textit{even}   with \textit{italic}
\end{Verbatim}
\end{document}

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 '12 at 20:18

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.