2

I have this code (based on Andrew's answer at How can I use rotate inside a defined TikZ command?).

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xparse}
\NewDocumentCommand\Triangle{ O{} r()}{% Syntax: [draw options] (lefthand endpoint
  \draw[#1] (#2) node{$\bullet$} -- ++(1,1.5) node{$\bullet$};
  \draw[#1] (#2) -- ++(2,0) node{$\bullet$};
  \draw[#1] ($(#2)+(1,1.5)$) -- ++(1,-1.5);
}
\begin{document}
  \begin{center}
    \begin{tikzpicture}
        \Triangle[rotate=30](0,0);
        \Triangle[rotate=-10](2.5,0);
        \Triangle(5,0);
    \end{tikzpicture}
  \end{center}
\end{document}

Now I would like to add three more arguments as names replacing the bullets at the vertices. I have tried to follow the instructions on several blogs plus in the xparse documentation and i cannot get it to work. I'm sure the right way is simple once you know it. Can someone show it to me?

  • Assuming that they are mandatory arguments, \NewDocumentCommand\Triangle{ O{} r() m m m }{....macro code...}. (The space between the letters is for clarity only, it is not required.) Then you would have #1, #2, ..., #5 at your disposal in the macro code. – Andrew Jan 10 '18 at 15:50
  • If you only have one optional argument at the beginning, \newcommand can handle that. – John Kormylo Jan 10 '18 at 16:28
  • @JohnKormylo But it can't handle the () delimited first argument. – Torbjørn T. Jan 10 '18 at 18:31
  • @TorbjørnT. - Ah, I was wondering what that was for. – John Kormylo Jan 10 '18 at 19:28
  • 1
    I'm not sure what is really going into your \Triangle-like macros but another approach is to define a tikz pic, as described in section 18.2 of the tikz manual. There are many examples of these on TeX.SX such as my post tex.stackexchange.com/questions/408833/…, which defines a possibly relatedpic for drawing some triangles. – Andrew Jan 11 '18 at 9:25
5

To expand my comment: you can do the following:

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xparse}
\NewDocumentCommand\Triangle{ O{} mmm r()}{
  % Syntax: [draw options]{node1}{node2}{node3}(lefthand endpoint)
  \draw[#1] (#5) node{$#2$} -- ++(1,1.5) node{$#3$};
  \draw[#1] (#5) -- ++(2,0) node{$#4$};
  \draw[#1] ($(#5)+(1,1.5)$) -- ++(1,-1.5);
}
\begin{document}
  \begin{center}
    \begin{tikzpicture}
        \Triangle[rotate=30]{A}{B}{C}(0,0);
        \Triangle[rotate=-10]{D}{E}{F}(2.5,0);
        \Triangle{G}{H}{I}(5,0);
    \end{tikzpicture}
  \end{center}
\end{document}

to produce:

enter image description here

The arguments to \Triangle are specified as O{} mmm r(). In order, these mean:

  • O{} says that #1 is an optional argument, that is given as [#1], and which defaults to "nothing". This is used to give an optional argument to \draw via \draw[#1]
  • mmm says that #2, #3 and #4 are mandatory arguments (this could also be specified as m m m etc)
  • r() says that #5 is a required/mandatory argument that must be given in the form (#5)

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.