Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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
add comment

3 Answers

up vote 7 down vote accepted

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)

share|improve this answer
add comment

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}
share|improve this answer
    
Thanks but indeed the alltt package is the good one for my purpose. –  projetmbc Apr 29 '12 at 14:05
add comment

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.

share|improve this answer
    
@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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.