-
Notifications
You must be signed in to change notification settings - Fork 0
Line break preserving macros
In general LaTeX ignore single line breaks. It requires two line breaks in a row in order to end a line (paragraph). However, in certain cases you may wish to write blocks where the line breaks are preserved as they are written. An example of this is in the letter preamble when writing a multi-line address.
In inline LaTeX the \obeylines
macro can be used to cause LaTeX to break lines
on a single line break. Extending this idea it seems natural that the following
macro, which places \obeylines
before some text should also preserve line breaks.
\newcommand{\keepLineBreaks}[1]{\obeylines #1}
However, the \keepLineBreaks
macro does not in fact work. This is because LaTeX
reads the arguments to the macro in without \obeylines
active in order to do
the argument substitution into the macro. As a result, when it reads it the second
time (this time with \obeylines
active), all of the single line breaks have already
been lost.
In order to address this, the \obeylines
macro must be used, before the text is read.
As a result two macros are required to implement this functionality. The first macro
does not read in the arguments. It only invokes \obeylines
and a second macro
which will actually read in the arguments. Note that it starts with a
\begingroup
. This is to ensure that the effect of \obeylines
does not extend
beyond the expected range.
\newcommand{\keepLineBreaks}{\begingroup\obeylines\keepLineBreak@doStuff}
The second command reads in the arguments and actually does whatever processing
needs to happen in the context of \obeylines
. Note that it needs to end with
\endgroup
to close the group that was started by the first command.
\newcommand{\keepLineBreak@doStuff}[1]{#1\endgroup}