0

Does LaTeX contain a supported construct for ignoring whitespace during macro definitions?

The default situation

By default, whitespace inside macro definitions is preserved just like everywhere else. As a consequence, any whitespace used for code structuring (primarily line breaks) must be commented out. An exception are lines ending with a \commandToken, as whitespace after these is ignored.

E.g. in example_a.tex below, in the macro definition without comments I obtain an output

[[ world. ComQuo.everyone! ]] 

while with comments the desired output

[[world.ComQuo.everyone!]] 

is obtained, at the cost of increased editing effort and reduced readability.

exampla_a.tex

exampla_a.pdf

\documentclass{article} \usepackage[a6paper,landscape,scale=0.9]{geometry} \begin{document} \newcommand\hello[1]{#1} \newcommand\CommandsQuoteSubsequentWhitespace{ComQuo.} \newcommand\MyMacroSpaces{ \hello{world}. \CommandsQuoteSubsequentWhitespace \hello{everyone}! } \newcommand\MyMacroComments{% \hello{world}.% \CommandsQuoteSubsequentWhitespace% \hello{everyone}!% } \ttfamily SPC[[\MyMacroSpaces]] (space wanted before) CMT[[\MyMacroComments]] (space wanted before) \end{document} 

1 Answer 1

1

As mentioned in strongly related question “It's safe to ignore tabs and newlines when defining a macro?”, the most important change (i.e. ignoring line-breaks and subsequent indentation whitespace) can be obtained by using \endlinechar=-1 to supress line-break and subsequent whitespace. However, it needs to be reset afterwards, leading to a construct

\endlinechar=-1 % line-break and subsequent whitespace produce nothing (... MACRO DEFINITIONS ...) \endlinechar=13 % 

with which the example document gives the output

SPC[[world.ComQuo.everyone!]] (space wanted before) CMT[[world.ComQuo.everyone!]] (space wanted before) 

Caveats

As mentioned in the other question:

  • Be careful to reset \endlinechar before other people's code is executed (e.g. \usepackage).
  • Ignoring the line-break whitespace can have unintended consequences with some constructs, where the whitespace is needed as delimiter (see other question), but in those cases using a comment or explicit \space helps.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.