4

I am drawing some nodes with tikz in multiple tikzpicture environments. It is the exact same nodes, so I thought it is possible to create a tikz command to draw the same 5 nodes again and again?

Would it be best practice to just use

\newcommand{\samefivenodes}{%
  \node (a) {a};
  \node[right=of a] (b) {b};
  \node[right=of b] (c) {c};
  \node[right=of c] (d) {d};
  \node[right=of d] (e) {e};
};
3
  • did you check the chains library of TikZ in the manual ? – percusse Dec 26 '16 at 10:57
  • 1
    It depends on how you want to use these five nodes. If it is just meant as a shorthand, then \newcommand is fine. If it is a sub-picture, tikz offers the \pic command, or you can define a new shape (like rectangles, circles, diamonds) with anchors which will automatically be used when connecting the shape with other nodes. – gernot Dec 26 '16 at 12:00
  • It is a tree, so you could use Forest which can copy sub-trees multiple times. I am sorry, by the way. When I commented on your earlier question, I thought that you would obviously already know how to provide a minimal example. However, seeing this new question, I realise that I may have been mistaken and that you may so far not have been directed to the guidance for creating a Minimal Working Example (MWE). Just FYI, there is another one for questions about bibliographies. – cfr Dec 26 '16 at 21:11
3

With Forest:

\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\begin{document}
\forestset{
  declare toks={split five}{},
  make five/.style={
    before typesetting nodes={append'=a, split five=#1, delay={split option={split five}{:}{before five,five and,just five}}},
  },
  just five/.style={
    for descendants={#1},
  },
  five and/.style={
    for tree={#1},
  },
  before five/.style={
    #1,
  },
}
\begin{forest}
  delay={
    for tree={
      if content={}{phantom}{
        name/.option=content,
        delay={content/.wrap value=\strut#1},
      },
      grow'=0,
    },
  },
  before typesetting nodes={
    for tree={
      font=\sffamily
    }
  }
  [[a[b[c,make five[d[e,make five=:{grow'=90}:make five]]]]][modified copy, make five={top color=black, bottom color=black, middle color=white, text=red, !1.edge=->}:{grow'=-90}:{inner color=blue!50!cyan, outer color=blue!30!cyan, text=white, thick, draw=blue!75!cyan, edge={blue!75!cyan, thick}, font=\sffamily\bfseries, circle} ]]
\end{forest}
\end{document}

will create many exactly similar copies, some rotated, and one highly modified version:

Forest repetition

2
  • If I have each node is a square with side equal to 1 or a circle radius 1. How can I make sequence of n - squares or n - cirlces? Please help me. – minhthien_2016 Dec 27 '16 at 23:26
  • @toandhsp That's a different question. Please ask a new question and include the code you are using to make the one node. – cfr Dec 27 '16 at 23:46

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.