12

I want to draw something like this diagram in TikZ. My code is:

\documentclass[a4paper]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}    
\begin{tikzpicture}

    \node (wrijving)                           {wrijving};
    \node (lading)   [right=of wrijving]       {(onzichtbare) lading};
    \node (elkracht) [below=of lading]         {elektrische kracht};
    \node (afst)     [below left=of elkracht]  {afstoten};
    \node (aantr)    [below right=of elkracht] {aantrekken};

    \path (wrijving) edge[->] (lading)
          (lading)   edge[->] (elkracht)
          (elkracht) edge[-] (afst)
                     edge[-] (aantr);
\end{tikzpicture}
\end{document}

Which gives this: Diagram

Probably there's a better way to draw this, but I don't know how.

I also want "afstoten" and "aantrekken" to be closer. I know you can specify a value to below and left, but is is possible to have a branch with normal length, and just rotate it x deg?

2
  • Maybe look at the chains library? – cfr Sep 5 '15 at 11:58
  • 2
    @cfr Or the forest? :-) I think forest is the easiest! – user11232 Sep 5 '15 at 12:10
18

Since it's a tree, here's a possibility using the forest package; the code is considerably shorter now and you have the desired control over the separation of the two lower elements using, for exmaple, the calign options (see lines commented out in my example code):

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={edge={->},
%calign=fixed edge angles,
%calign primary angle=-40,calign secondary angle=40
}
[wrijving,grow=0
  [(onzichtbare) lading
    [elektrische kracht    
      [afstoten]
      [aantrekken]
    ]
  ]    
]
\end{forest}

\end{document}

enter image description here

7

This does not yet adjust the positioning but is more concise:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{chains,scopes}
\begin{document}
  \begin{tikzpicture}[start chain=main going right, every on chain/.append style={join}, every join/.append style={->}]
    \node [on chain] (wrijving) {wrijving};
    \node [on chain] (lading) {(onzichtbare) lading};
    \node [on chain=main going below] (elkracht) {elektrische kracht};
    {[start branch=split going below left]
      \node [on chain] (afst) {afstoten};}
    \node [on chain=main going below right] (aantr) {aantrekken};
\end{tikzpicture}
\end{document}

chains

EDIT

To alter the compactness of the diagram when it branches, you can specify a more detailed direction for the chain. For example, using the calc library:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{chains,scopes,calc}
\begin{document}
  \begin{tikzpicture}[start chain=main going right, every on chain/.append style={join}, every join/.append style={->}]
    \node [on chain] (wrijving) {wrijving};
    \node [on chain] (lading) {(onzichtbare) lading};
    \node [on chain=main going below] (elkracht) {elektrische kracht};
    \node [on chain={main placed {at=($(\tikzchainprevious.south)+(-50:1.5)$)}}] (aantr) {aantrekken};
    {[start chain=split]
      \chainin (elkracht);
      \node [on chain=placed {at=($(\tikzchainprevious.south)+(-130:1.5)$)}] (afst) {afstoten};}
\end{tikzpicture}
\end{document}

tweaked direction

Note that I'm a big fan of forest and, if your diagrams are really trees, it is great. If not - that is, if they change direction many times and do not necessarily have single roots - then forest is still an option, but chains may be easier for simple cases, where programmatic power is not a significant consideration.

7

I present an alternative, more concise solution using the tikz-cd package that was made for commutative diagrams and has (in my opinion) very clear and simple syntax. With a bit of naive trickery, the spacing is also easily adjusted to yield whatever one likes:

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}

\begin{tikzcd}
    \mathrm{wrijving} \arrow[rr]& &[-2cm] \mathrm{(onzichtbare)\ lading} \arrow[d] &[-2cm]\\
    & & \mathrm{elektrische\ kracht}\arrow[dl, no head]\arrow[dr, no head]\\
    & \mathrm{afstoten} & & \mathrm{aantrekken}
\end{tikzcd}

\end{document}

which produces the following:

enter image description here

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.