[Tex/LaTex] newcommand defined on multiple lines creates extra white space

macrosspacing

When I define a \newcommand over multiple lines, I get unwanted white space in my output.

How do I make it so I can write a long command, but still make it readable, without getting the extra white spaces?

My current solution is just to write everything without line breaks, but that's not very sustainable.

Here is a MWE of this behaviour:

\documentclass{memoir}
\begin{document}

\newcommand{\lraA}{B}

\newcommand{\lraB}{
%lots of latex logic
%so this command is on multiple lines
B
}

\ \\
A\lraA{}C\\
A\lraB{}C
\end{document}

This produces:

ABC

A B C

Best Answer

New lines are treated as spaces. So in your code

\newcommand{\lraB}{     %  <--  you are putting a space here!
%lots of latex logic
%so this command is on multiple lines
B     %  <--  you are putting a space here!
}

If you want to avoid spurious spaces you should end the lines with a comment character %:

\newcommand{\lraB}{%  <--  HERE
%lots of latex logic
%so this command is on multiple lines
B%  <--  HERE
}

Here you can find more details.

Related Question