1

This code runs fine:

\documentclass{amsart}
\usepackage{tikz}
\newcommand{\putnode}[1]{\node at (#1,0) (A) {A};}
\begin{document}
\begin{tikzpicture}
\putnode(0);
\node at (0,1) (a) {a};
\node at (1,1) (b) {b};
\node at (2,1) (c) {c};
\node at (3,1) (d) {d};
\end{tikzpicture}
\end{document}

But no matter what argument I give to \putnode, it puts the A at (1,0). I would think the code above would put A at (0,0) since #1 is replaced by 0. What am I doing wrong?

2
  • 3
    No, \putnode(0) becomes \node at ((,0) (A) {A};0), which is quite wrong. The argument should be surrounded by braces: \putnode{0}. – egreg Jan 10 '18 at 22:06
  • You can't use () delimited arguments with macros defined by \newcommand. It is possible when using xparse features to define new macros as in your other question, if the argument is defined with r(). – Torbjørn T. Jan 10 '18 at 22:22
7
\documentclass{amsart}
\usepackage{tikz}
\newcommand{\putnode}[1]{\node at (#1,0) (A) {A};}
\begin{document}
\begin{tikzpicture}
\putnode{0}
\node at (0,1) (a) {a};
\node at (1,1) (b) {b};
\node at (2,1) (c) {c};
\node at (3,1) (d) {d};
\end{tikzpicture}
\end{document}

You had an extra ; and macro arguments are {} delimited not ().

2

If you really want the format \putnode(0); to be used you can use \def instead of \newcommand and provide a suitable argument text:

\def\putnode(#1);{\node at (#1,0) (A) {A};}

This defines \putnode to expect a ( after it, then it reads everything to the next ) as the first argument and finally expects a ;.

Note that not having the characters exactly like this will trigger an plainTeX error normal LaTeX users might find confusing. It's also not as rebust as the TikZ code and therefore does not allow a space between ) and ; for example. However, you could remove the two ; in the defintion, having \node picking up the ; after the macro usage.

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.