# How to remove trailing spaces at the end of each line in a block of code?

NOTE: I have to adapt this question to reflect Martin's answer (that actually not 100% solves the original problem) so I can accept it. :-)

If I work without standalone document class, I usually put the following setting

\newsavebox\IBox
\savebox\IBox{\raisebox{\depth}[\totalheight]{XeTeX runs very slowly!}}
% trimmed for simplicity


in the preamble.

However, when using standalone document class (to divide my project into one main input file & several sub-files), I am forced to move a setting related to each sub-file to its body as follows.

% sub-file.tex
\documentclass{standalone}
\usepackage{pstricks}

\begin{document}
\newsavebox\IBox%
\savebox\IBox{\raisebox{\depth}[\totalheight]{XeTeX runs very slowly!}}%
% trimmed for simplicity

\begin{pspicture}(\LEFT,\BOTTOM)(\RIGHT,\TOP)
% trimmed for simplicity
\end{pspicture}%
\end{document}


Adding a trailing % to the end of each line of setting code is cumbersome so I need a way not to do this manually. The following code shows what I need; There is a \RemoveTrailingSpaceInThisBlock macro to do the job.

% sub-file.tex
\documentclass{standalone}
\usepackage{pstricks}

\begin{document}
\RemoveTrailingSpaceInThisBlock% to remove trailing space in the current block
\newsavebox\IBox
\savebox\IBox{\raisebox{\depth}[\totalheight]{XeTeX runs very slowly!}}
% trimmed for simplicity

\begin{pspicture}(\LEFT,\BOTTOM)(\RIGHT,\TOP)
% trimmed for simplicity
\end{pspicture}
\end{document}


Is there such a macro to remove the trailing spaces at the end of each line in a code block?

-
xport, put the \fbox{..} inside the environment, not the other way around. –  Lev Bishop Aug 2 '11 at 7:10
Indeed. Like catcode changes the change of \endlinechar doesn't work inside an macro environment. –  Martin Scharrer Aug 2 '11 at 9:04

You could use \endlinechar=-1\relax to make TeX ignore all line endings and not take them as spaces. If you want to make this an environment place it in the begin code of it. Otherwise you could just place it inside \begin{document} and it will be valid till the end of that environment, even when the file is processed as sub-file.

Note that the \relax is just an end-marker to terminate the number. You can also use a space or use \m@ne instead of -1. This needs \makeatletter of course.

However, this will not ignore any real spaces at the end of any line, just the space insert by the end-of-line character.

The LaTeX3 package expl3 also provides \ExplSyntaxOff and \ExplSyntaxOn which will switch into a similar mode as the one of the preamble and back again. This mode AFAIK ignores spaces completely. It also allows _ and : to be used in macro names which will interfere with the normal usage of this characters (e.g. in math expressions). Therefore you shouldn't use it for all of your document, just for the preamble-like stuff.

-
+1 but it did not work for making an environment. –  xport Aug 2 '11 at 1:26
"Otherwise you could just place it inside \begin{document} and it will be valid till the end of that environment, even when the file is processed as sub-file." is only true for standalone class. Other classes used by sub-files (that will be imported by a main file using docmute) do not localize the effect of \endlinechar=-1\relax. –  xport Aug 3 '11 at 2:10
@xport: Yes, I was only talking about standalone (however about the package which is used to read the sub-files, not the class; I know it can be confusing.) –  Martin Scharrer Aug 3 '11 at 7:44
\documentclass{standalone}
\usepackage{pstricks}
\makeatletter

+1 but it only removes the trailing space of \endpspicture. :-) –  xport Aug 2 '11 at 5:20
\ignorespacesafterend can be avoided if we use \pspicture...\endpspicture instead of \begin{pspicture}...\end{pspicture}. –  xport Aug 4 '11 at 23:33