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.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityThe 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:
\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}
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.
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!
\obeyspaces
is for obeying spaces. About tabulation, I'm not sure what you mean, but you should be able to realise that withtabbing
?