3

I'm, trying to create an horizontal diagram (like the diagrams you can create with Visio but programatically) in R (to embed it in a LaTeX document) or directly in LaTeX. So far I've been reading about differnt options and I've tried with Tikz, creating nodes and childs...

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[grow = right,sibling distance=10em, 
every node/.style = {shape=rectangle, rounded corners, draw, align=center, top color=white, bottom color=blue!20}]]

  \node {Formulas}
    child { node {single-line} }
    child { node {multi-line}
      child { node {aligned at}
        child { node {relation sign} }
        child { node {several places} }
        child { node {center} } }
      child { node {first left} }
    };

\end{tikzpicture}

\end{document}

But I get this,

enter image description here

As you can see, It's not automatically fitted properly. Some things overlap, other are very spread...

How do you suggest to create this kind of diagrams/flowcharts, preferably with arrows. Maybe with the LaTeX package smartdiagram or with R diagrammer or with the package diagram? I'm looking for the simplest one. Smartdiagram seems nice but in the examples don't show hoy to create a horizontal tree or flowchart.

There is also a package called schemata that creates a different kind of diagrams, maybe not so beautiful. Anyway its difficulty is similar. enter image description here

1
  • Any solution with smartdiagram?
    – skan
    Dec 2, 2016 at 23:01

3 Answers 3

3

I have no idea what or who Visio is. Starting from Emma's answer and changing the structure to that produced by R, which I gather from your spamming is what you want, you can create a skan tree style which formats the tree accordingly.

There are several existing answers on this site illustrating different ways to draw trees with precisely this kind of structure and branches of this shape. Please look at those if you want a different approach.

Forest is the most powerful and flexible programme to draw trees in LaTeX as far as I know, with the exception of the tree layouts supported by the graph-drawing algorithms provided by TikZ itself. Those rely on LuaTeX and will automatically layout your tree, but they obviously require a different approach which may or may not be suitable for your work-flow. The TikZ manual documents thse facilities extensively.

For example,

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
% addaswyd o ateb Emma: https://tex.stackexchange.com/a/342102/
\forestset{
  skan tree/.style={
    for tree={
      grow=0,
      rounded corners,
      draw,
      top color=white,
      bottom color=blue!20,
      edge={Latex-},
      child anchor=parent,
      %parent anchor=children,
      anchor=parent,
      tier/.wrap pgfmath arg={tier ##1}{level()},
      s sep+=20pt,
      l sep+=20pt,
      edge path'={
        (.child anchor) -- ++(-20pt,0) -- (!u.parent anchor)
      },
    },
    before typesetting nodes={
      for tree={
        content/.wrap value={\strut ##1},
      },
    },
  },
}
\begin{forest}
  skan tree
  [Formulas
    [single-line]
    [multi-line
      [aligned at
        [relation sign]
        [several places]
        [center]
      ]
      [first left]
    ]
  ]
\end{forest}
\end{document}

skan tree

EDIT

You can override the alignments by overriding tier for particular nodes. For example,

\begin{forest}
  skan tree
  [Formulas
    [single-line
      [relation sign, delay={tier/.wrap pgfmath arg={tier #1}{level("!name=sp")}}]
    ]
    [multi-line
      [aligned at
        [several places, name=sp]
        [center]
      ]
      [first left]
    ]
  ]
\end{forest}

enforced alignment

or, with spacing using a phantom,

\begin{forest}
  skan tree
  [Formulas
    [single-line, for children={delay={tier/.wrap pgfmath arg={tier #1}{level("!name=sp")}}}
      [relation sign]
      [, phantom]
    ]
    [multi-line
      [aligned at
        [several places, name=sp]
        [center]
      ]
      [first left]
    ]
  ]
\end{forest}

forced alignment with spacing

8
  • In fact I want to write my article with LuaTeX, but your solution is generic and nice. Do you also know how to do it with smartdiagram?
    – skan
    Dec 3, 2016 at 10:43
  • And now just one questio more. Imagine you want the "relation sign" linked to the "single-line" node, but not located below "aligned at" but below "several places". How do you get it? I mean keeping relation sign there but linked to single-line instead of aligned-at.
    – skan
    Dec 3, 2016 at 10:59
  • @skan You can read smartdiagram's manual as well as I can. The easiest way is probably to make relation sign a child of single-line and add tier=tier 3 to override the default tier.
    – cfr
    Dec 3, 2016 at 16:11
  • 1
    @skan Please see edit above.
    – cfr
    Dec 3, 2016 at 16:22
  • @ cfr your solution works well if I change documentclass to article or something else. If I use standalone I getting many errors.
    – skan
    Dec 3, 2016 at 21:08
2

Here is the obligatory recommendation of the forest package for nice automatic tree layout:

\documentclass{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
  for tree={grow=east, s sep=10em, shape=rectangle, rounded corners, 
            draw, align=center, top color=white, bottom color=blue!20}
  [Formulas
    [single-line]
    [multi-line
      [aligned at
        [relation sign]
        [several places]
        [center]
      ]
      [first left]
    ]
  ]
\end{forest}
\end{document}

enter image description here

3
  • I would like a result like my answer with diagrammer. I've found how to use arrows instead, with the option edge={->} , I also found that parent anchor=east,child anchor=west sets the proper position for the arrows. I don't know how to get a smaller vertical distance. Changing sep=6em modifies everything, not only the vertical distance.
    – skan
    Dec 2, 2016 at 23:20
  • 1
    It would also be great to align the nodes depending on their depth.
    – skan
    Dec 2, 2016 at 23:29
  • Now one of 2 instances of the obligatory recommendation ... ;)
    – cfr
    Dec 3, 2016 at 0:12
1

I've found an answer with the R library Diagrammer:

mermaid(" graph LR
        V[Variable]-->A(Dependiente)
        V-->H(No dependiente)
        A-->B(Obligatorio)
        B-->D(Padre oblig.)
        B-->E(Padre NO oblig)
        A-->C(No obligatorio)
        C-->F(Padre oblig.)
        C-->G(Padre NO oblig.)
        H-->I(Obligatorio)
        H-->J(No obligatorio)
        ")

enter image description here It seems quite fast and automatic.

The problem is that it doesn't generate a LaTeX code. You can just capture the image. I would prefer a solution directly with LaTeX. Another thing I don't like is the starting position of the arrows emerging from the leftmost node.

There is also a command grViz used in a different way, creating nodes and later edges.

grViz("
digraph boxes_and_circles {
      graph [overlap = true, fontsize = 10]
      node [shape = circle, fixedsize = true, width = 0.9] // sets as circles
      V;A;B;C;D;E;F;G;H
      V->A V->H A->B B->D B->E A->C C->F C->G H->I H->j
            }
      ")

But with grViz I can only get tree from top to bottom and not from left to right.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .