3

The answer by Marmot to How can I use rotate inside a defined TikZ command? calls one use of scope in TikZ a quick and dirty fix. The fix works. But the answer suggests to avoid this use of scope in a TikZ command definition, and use newcommand instead.

To me as non-expert this particular use of scope does seem odd. It merely seems to re-iterate an argument that I had already declared, since TikZ was ignoring some (but not all) values of that argument until I re-iterated it. But that issue may not be to the point at all.

Are there general reasons to try to avoid using scope, in favor of newcommand?

  • 1
    I guess what marmot meant was to use \newcommand instead of \def. There is a question on this site about the advantages/disadvantages. – TeXnician Jan 09 '18 at 12:39
  • @TeXnician I will look into that. But the answer to the existing question on \newcommand does not seem to bear on my question about why some values of one declared argument were being ignored. – Colin McLarty Jan 09 '18 at 12:58
  • 1
    marmot definitely meant \newcommand vs. \def to define the command in the first place, scope and \newcommand do different things. So this entire question seems based on a misunderstanding. I can't explain why, but rotation of a line doesn't work when drawn between named coordinates, e.g. with \coordinate (a) at (0,0);\coordinate (b) at (1,0); \draw [rotate=30] (a) -- (b); \draw[blue,rotate=30] (0,0) -- (1,0); only the blue line is rotated. – Torbjørn T. Jan 09 '18 at 13:46
  • @TorbjørnT. If that is the actual question I can answer that. – percusse Jan 09 '18 at 16:06
  • @TorbjørnT. It works with \def when I use the scope environment. Do you mean it would also work in a \newcommand without the scope environment? – Colin McLarty Jan 09 '18 at 16:08
  • @ColinMcLarty A scope basically is the simplest choice to limit the transformation effect. Otherwise rotate is applied to everything. Or if you want to apply things to many things at once you can also put things inside a scope. Instead of avoiding use it as much as you like. – percusse Jan 09 '18 at 16:12
  • 1
    No, \def and \newcommand are for the same purpose, but the latter is usually recommended because it checks for existing macros, and throws an error if you try to use a macro name that is already in use. You can use \newcommand to define the \line macro (though the syntax would have to be slightly different) with a scope environment in the definition. See https://tex.stackexchange.com/questions/655/what-is-the-difference-between-def-and-newcommand for \def vs. \newcommand. – Torbjørn T. Jan 09 '18 at 16:13
  • @percusse Perhaps that answer would fit better at https://tex.stackexchange.com/questions/409312/, which is where the problem is demonstrated. – Torbjørn T. Jan 09 '18 at 16:14
  • 1
    I have added an answer to the previous question. There's no need to use scope here and the use of rotate in the previous question should not work because it does not make sense to rotate a line between two predefined coordinates (A) and (B). –  Jan 09 '18 at 16:30
  • @TorbjørnT. Do you want to answer or should this be closed as a duplicate of something or what do you think? – cfr Jan 10 '18 at 01:39
  • 1
    @cfr I added an answer. – Torbjørn T. Jan 10 '18 at 11:56

1 Answers1

3

Your question seems based on a misunderstanding, scope and \newcommand are not "competitors", their purpose are completely different.

What marmot referred to in his answer when he recommended \newcommand, was the use of \def. Both \def and \newcommand are for defining new macros, but there are differences between them. \newcommand is usually recommended, largely (I think) because it checks whether a command exists first, while \def will silently overwrite the existing command. For more on the differences, see What is the difference between \def and \newcommand?

Further, as Andrew mentioned in his answer to your other question, there is already a macro called \line, and indeed, if you do \newcommand\line{foo} you will get an error saying ! LaTeX Error: Command \line already defined. When redefining an existing command, you need to be sure that it doesn't break anything, so such an error probably indicates that you should select a different name for your macro.

The scope environment on the other hand is only for use within a tikzpicture environment, and the purpose of it is to be able to apply TikZ settings locally, for example changing the default colour, or line width, or rotation.

Torbjørn T.
  • 206,688