0

I'm trying to debug some \newcommand macros I'm working on, and I've got it down to this minimal example. All it does is define two lengths and then set their values - it doesn't use them for anything.

\documentclass{article}

\newlength{\xscale}
\newlength{\yscale}

\newcommand{\setscale}[2] {
    \setlength{\xscale}{#1}
    \setlength{\yscale}{#2}
}

\newcommand{\mystyle} {
    \setscale{0.25cm}{0.18cm}
}

\begin{document}

foo, \mystyle%
bar

foo, \setlength{\xscale}{0.25cm}\setlength{\yscale}{0.18cm}%
bar

\end{document}

The output looks like this:

enter image description here

Why is that large space being inserted when I wrap the \setlength commands inside \newcommand, but not when I don't? How can I define these commands so that they will set the lengths without inserting extra space into the document?

N. Virgo
  • 4,289
  • 2
  • 26
  • 41
  • 1
    The question might have been resolved quickly, but I don't think it makes sense to close a "how do I X?" question as a duplicate of a "what does command \Y do?" question. The answer to both questions might be "escape the newlines", but the questions are completely different, and there is no way someone with my question could have know to look up the supposed duplicate. – N. Virgo May 19 '20 at 12:05
  • If you want to post an educational question about importance of adding percent symbol before starting a newline of a command, you could have chosen a simpler question with a minimal answer. Is "setlength" relevant to this issue? – hesham May 19 '20 at 12:28
  • @hesham I didn't know the answer when I posted the question, and I believed that \setlength was the cause of the issue. How could I have known it wasn't? (Other than by a lucky guess, which is what actually happened just after I posted the question.) – N. Virgo May 19 '20 at 12:30
  • No problem, sorry if I got you wrong, it was just your comment below your question that implied so. – hesham May 19 '20 at 12:40
  • In fact I'm more interested to avoid this issue which i encountered long time ago. The necessity of that percent symbol to avoid extra spaces would still persist even if we replaced the braces for multiline command by bgroup/egroup? – hesham May 19 '20 at 12:45
  • While I understand your reservation about considering the question a duplicate, this is the kind of issue which arises fairly regularly here on TeX.SE; it has become more or less customary to close such questions as duplicates of the %-question. – campa May 19 '20 at 12:49
  • @campa fair enough - I guessed when I posted my own answer that this would be a common issue. But it would surely be better to have a canonical "why is my macro creating extra space?" question and close them as a duplicate of that instead. Closing as a duplicate of the % question comes across a little bit as saying "you should have known what the issue was". It's not really a big deal though. – N. Virgo May 19 '20 at 12:51
  • 2
    @Nathaniel -- In an attempt to make the identified duplicate easier to find, I have modified that question as originally stated by adding your suggested phrasing in parentheses. – barbara beeton May 19 '20 at 13:59
  • 1
    @hesham space after \bgrouo is ignored but none of the braces in the code here can be replaced by \bgroup \egroup. – David Carlisle May 19 '20 at 15:30
  • @David Carlisle Not even replaceable by begingroup/endgroup? – hesham May 20 '20 at 00:51
  • @hesham no. macro arguments are delimited by explicit {} tokens not grouping commands. The {} do not form groups in these contexts. – David Carlisle May 20 '20 at 00:57

1 Answers1

0

Here is a partial self-answer. If I change the \mystyle definition to

\newcommand{\mystyle} {
    \setlength{\xscale}{0.25cm}
    \setlength{\yscale}{0.18cm}
}

then I still get the issue, but if I change it to

\newcommand{\mystyle} {%
    \setlength{\xscale}{0.25cm}%
    \setlength{\yscale}{0.18cm}%
}

then it goes away. So it had to do with the newlines in my macro code. Serves me right for trying to write sensibly laid out code I suppose.

N. Virgo
  • 4,289
  • 2
  • 26
  • 41