TikZ Causal Graph Details (bold, arrow positions, text above paths)

graphsoverleaftikz-pgf

I am having a hard time to figure out how to design a causal graph with TikZ. The graph should look like this:

Causal graph

The code below is as far as I get, but it does not have some important details (see picture). First, how do I add text on top of the paths (and in red)? I have seen examples using \draw where {text} suffices, but if I add that to my graph it does not compile anymore. Second, how can I add bold text and line breaks? Third, ideally, the arrows would start and stop before/after the nodes, as it is in the picture, and not at the center of the node (X to Z and Z to Y).

\documentclass[10pt]{article}
\usepackage{color}
\usepackage{tikz}

\usetikzlibrary{shapes,decorations,arrows,calc,arrows.meta,fit,positioning}
\tikzset{
    -Latex,auto,node distance =1 cm and 1 cm,semithick,
    state/.style ={ellipse, draw, minimum width = 0.7 cm},
    point/.style = {circle, draw, inner sep=0.04cm,fill,node contents={}},
    bidirected/.style={Latex-Latex,dashed},
    el/.style = {inner sep=2pt, align=left, sloped}
}
\begin{document}

\begin{tikzpicture}
\begin{tikzpicture}
\usetikzlibrary{positioning}

    \node (1) at (6,0) {Some \ Text \ for \ X};
    \node (2) [right = 7cm of 1] {Some \ Text \ for \ Y};
    \node (3) [above right = 3cm of 1] {Some \ Text \ for \ Z};
    \node (t1) [above = 0.6cm of 3] {Text bold (with line break)};

    \draw (1) -- (2);
    \draw (3) -- (2) [anchor=west];
    \draw (1) -- (3);
    
    \node (4) at (6,6) {Some \ Text \ for \ X};
    \node (5) [right = 7cm of 4] {Some \ Text \ for \ Y};
    \node (6) [above right = 3cm of 4] {Some \ Text \ for \ Z};
    \node (t2) [above = 0.6cm of 6] {Text bold (with line break)};


    \draw (4) --  (5);
    \draw (6) -- (5);
    \draw (4) -- (6);
    
    
    \node (7) at (6,12) {Some \ Text \ for \ X};
    \node (8) [right = 7cm of 7] {Some \ Text \ for \ Y};
    \node (9) [above right = 3cm of 7] {Some \ Text \ for \ Z};
    \node (t2) [above = 0.6cm of 9] {Text bold (with line break)};


    \draw (7) --  (8);
    \draw (9) -- (8);
    \draw (7) -- (9);
\end{tikzpicture}

\end{tikzpicture}
\end{document} 

enter image description here

Best Answer

Let's see:

  • to have multiline nodes, you have to specify an align key; to have automatic line breaks, you need a text width
  • to change the font of the node you can use the key font
  • to add text around path, you can just put node in the middle of the path. By default it is horizontal and in the middle, but you can change that with sloped and pos keys.

Example (this is minimal, and do not have nested tikzpicture...)

\documentclass[10pt]{article}
\usepackage{color}
\usepackage{tikz}

\usetikzlibrary{shapes,decorations,arrows,calc,arrows.meta,fit,positioning}
\tikzset{
    -Latex,auto,node distance =1 cm and 1 cm,semithick,
    state/.style ={ellipse, draw, minimum width = 0.7 cm},
    point/.style = {circle, draw, inner sep=0.04cm,fill,node contents={}},
    bidirected/.style={Latex-Latex,dashed},
    el/.style = {inner sep=2pt, align=left, sloped}
}
\begin{document}

\begin{tikzpicture}

    \node (1) at (6,0) {Some \ Text \ for \ X};
    \node (2) [right = 7cm of 1] {Some \ Text \ for \ Y};
    \node (3) [above right = 3cm of 1] {Some \ Text \ for \ Z};
    \node (t1) [above = 0.6cm of 3, text width=4cm, align=center, font=\bfseries] {Text bold (with line break)};
    \draw (1) -- node[red, above ]{text in red} (2);
    \draw (3) -- node [red, above , sloped] {text in red, sloped} (2) [anchor=west];
    \draw (1) -- node [red, above left, pos=0.2, align=center] {text in red,\\ horizontal at 0.2} (3);
\end{tikzpicture}
\end{document}

result of the above snippet

Then there are several libraries to simplify the input of such kinds of diagrams; one nice starting point is the second tutorial in the TikZ manual.