5

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

  • \newcommand{\lraB}{% – user31729 Jun 9 '16 at 11:02
  • @JPi doesn't change anything in this case. – Atnas Jun 9 '16 at 11:10
  • True, I'd misread the question. – JPi Jun 9 '16 at 11:19
  • 1
    If you do programming in LaTeX, you will find % to be one of your best friends. Welcome to the site. – Steven B. Segletes Jun 9 '16 at 11:37
9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.