0

I'm trying to make a macro to print a TikZ diagram with "variable" labels. MWE:

\documentclass{standalone}
\usepackage{tikz}
\newcommand[3]{\myDiagram}{
  \begin{tikzpicture}
    \node[label={270:$1$}] (1) at (270:1) {};
    \node[label={30:$2$}] (2) at (30:1) {};
    \node[label={150:$3$}] (3) at (150:1) {};
    \draw
    (1) edge node[label={#1}] {} (2)
    (1) edge node[label={#2}] {} (3)
    (2) edge node[label={#3}] {} (3)
    ;
  \end{tikzpicture}
}
\begin{document}
\myDiagram{$1$}{$2$}{$3$}
\end{document}

Expected Behaviour: A diagram which substitutes labels from argument. I.e. My MWE's Output should be the same as this:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \node[label={270:$1$}] (1) at (270:1) {};
  \node[label={30:$2$}] (2) at (30:1) {};
  \node[label={150:$3$}] (3) at (150:1) {};
  \draw
  (1) edge node[label={$1$}] {} (2)
  (1) edge node[label={$2$}] {} (3)
  (2) edge node[label={$3$}] {} (3)
  ;
\end{tikzpicture}
\end{document}

Actual Behaviour: I keep getting a Missing \endcsname inserted.

What I've Tried: Variations on this question's answers (I couldn't find much else in the way of similar questions).

Please explain what I'm doing wrong here and how I can fix it. Thanks in advance.

2

I suggest the following way:

enter image description here

\documentclass[tikz]{standalone}
\begin{document}
\newcommand*{\myDiagram}[3]{%
\path
(-90:1) node (1) {$1$}
(30:1)  node (2) {$2$} 
(150:1) node (3) {$3$}
;
\draw[nodes={red,midway,scale=.6}] (1)
--(2) node[below right] {#1}
--(3) node[above] {#2}
--(1) node[below left] {#3}
;
}%  

\begin{tikzpicture}
\myDiagram{$1$}{$3$}{$2$} 
\end{tikzpicture}
\end{document}

Also you can define a pic for this task.

About the error in OP's code OP made a wrong order: \newcommand{\myDiagram}[3] instead of \newcommand[3]{\myDiagram}.

5
  • This is a decent workaround, but doesn't address the "why is this not working" part of my question... Also, why \newcommand* here (it doesn't affect the output on my machine)?
    – User12345
    Jul 11 at 18:38
  • @User12345 I see the picture, then caring how to code with simplicity. I don't care much about the error in your code. although I think that is easy to explain. Maybe someone can help you, or you can explain yourself later
    – Black Mild
    Jul 11 at 18:59
  • @User12345 aha, you made a wrong order: \newcommand{\myDiagram}[3] instead of \newcommand[3]{\myDiagram}
    – Black Mild
    Jul 11 at 19:01
  • yup, silly me. Thanks.
    – User12345
    Jul 11 at 19:19
  • It happens sometimes ^^ Cheer up! I suppose that you can upvote and accept my updated answer now
    – Black Mild
    Jul 11 at 19:57
0

This behaviour is the result of a ID-10t error. As @Black Mild pointed out in a comment on their answer, I passed \newcommand[3]{\myDiagram} instead of \newcommand{\myDiagram}[3].

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.