4

I am trying to reproduce this image http://www.ats.ucla.edu/stat/stata/faq/model5s.png from this page http://www.ats.ucla.edu/stat/stata/faq/modmed.htm using tikz-cd. I have the following code and diagram. How do I add an arrow from W to the arrow of X to M or to the arrow from M to Y?

I am not sure if it is possible since the arrows themselves are not nodes. Also, is it possible to include an oval or rectangular border around a node? I may not be using the right package. I appreciate any suggestions.

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

\begin{tikzcd}
    & W \\
    & M \arrow{dr} \\
    X \arrow{ur} \arrow{rr} & & Y
\end{tikzcd}

\end{document}

enter image description here

Thanks, ksint

4

One possibility using the name key and then using those names to draw the arrows:

enter image description here

The code:

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

\begin{document}

\begin{tikzcd}
& A 
\\
B\arrow[r,""{name=B}] & C\arrow[r,""{name=C}] & D 
\arrow[from=1-2,to=B]\arrow[from=1-2,to=C]
\end{tikzcd}

\begin{tikzcd}
    & W \\
    & M \arrow[dr,""{name=R}] \\
    X \arrow[ur,""{name=L}] \arrow{rr} & & Y
\arrow[from=1-2,to=L]\arrow[from=1-2,to=R]
\end{tikzcd}

\end{document}
  • Thanks! name key is what I needed. – ksint Dec 3 '14 at 17:47
3

Try this:

\documentclass[10pt,a4paper]{article}
\usepackage{tikz-cd}
\begin{document}
    \begin{tikzcd}
        &  W  \\
        & M \arrow{dr} \\
        X \arrow[ur, ""{name=Z}]{} \arrow{rr} & & Y
        \arrow[from=1-2, to=Z, bend right]
    \end{tikzcd}
\end{document}

Output:

enter image description here

2

Without tikz-cd

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[node distance=1cm,>=latex]
\node[draw] (X) {X};
\node[draw, right=of X] (M) {M};
\node[draw, right=of M] (Y) {Y};
\node[draw, above=of M, fill=yellow!30] (W) {W};
\draw[->] (X)--(M) coordinate[midway] (aux){};
\draw[->] (M)--(Y) coordinate[midway] (aux2){};
\draw[->] (W)--(aux);
\draw[->] (W)--(aux2);
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks! tikz looks a lot more flexible but tikz-cd seems more concise and readable. Do you know of any software that could convert a tikz-cd snippet to tikz? – ksint Dec 3 '14 at 17:57

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.