4

How can I achieve to draw a line (using double sided arrows) between a node and the middle of a path?

\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\usepackage{tikzscale}
\usetikzlibrary{arrows,automata,shadows}

\begin{document}
    \centering
    \tikzstyle{vertex_basic}=[rectangle,thick,top color=white,bottom color=black!20, minimum size=50pt,scale=1]
    \tikzstyle{vertex}=[vertex_basic, draw=black!50]
    \tikzset{initial text={}}

    \begin{tikzpicture}[,>=stealth,ultra thick,black!50,text=black, scale=1.8,graphs/every graph/.style={edges=rounded corners},every new ->/.style={shorten >=1pt},auto]
    \matrix[row sep=20mm,column sep=2mm] {
        \node (X) [vertex,align=center] {X}; & & \node (Y) [vertex,align=center] {Y}; \\
        & \node (O) [vertex,align=center] {Outcome}; &\\
    };
    \path[<->] (X) edge node[align=center] {} (O);
    \path[<->] (Y) edge[densely dotted] node[align=left] {} (O);
    \path[<->] (X) edge[densely dotted] node[align=left] {} (Y);
    \end{tikzpicture}
\end{document}

The above coded produces the following image (without the red line). Desired result

How would it be possible to add a line using the style of the other lines at position of the red line? Thank you!

4

You've placed an empty node midway along the arrow from O to X, give it a name and you can draw a line to it. But if you don't use that node for anything, replace it with a coordinate as in

\path[<->] (X) edge coordinate (m)  (O);

Then you can draw the arrow you want with

\draw [<->] (m) -- (Y);

In the code below I suggest some other changes as well.

\tikzstyle is considered deprecated, so I moved the style definition into a \tikzset, cf. Should \tikzset or \tikzstyle be used to define TikZ styles?

To the matrix I added the matrix of nodes option, which requires the matrix library, and nodes={vertex} to set the vertex style for all the nodes in the matrix.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix} % <-- added 
\tikzset{
   vertex_basic/.style={
       rectangle,thick,
       top color=white,
       bottom color=black!20,
       minimum size=50pt},
  vertex/.style={vertex_basic, draw=black!50, align=center},
}
\begin{document}
\begin{tikzpicture}[
  >=stealth,
  ultra thick,
  black!50,
  text=black,
  scale=1.8
]

\matrix[
  row sep=20mm,column sep=2mm,
  matrix of nodes,nodes={vertex}
]
{
  |[alias=X]| X &                      & |[alias=Y]| Y \\
                & |[alias=O]| Outcome                  \\
};

\draw[<->]     (X) --coordinate(m) (O);
\draw[<->, densely dotted]     (Y) -- (O);
\draw[<->, densely dotted]     (X) -- (Y);
\draw[<->,red] (m) -- (Y);
\end{tikzpicture}
\end{document}
  • This looks completely different from what I did before, producing the same result :-) Would you mind to explain it further, especially using \draw instead of \path or the [alias=X] notation? – Robin Dec 7 '17 at 11:06
  • 1
    @Robin \draw (short for \path[draw]) is used because I used -- instead of edge, \path (a) -- (b); will create the path, but not draw it. The edge operation works a bit differently, see chapter 17.12 in the manual. See tex.stackexchange.com/a/163571 for an explanation of how a matrix of nodes work. alias is just a way of giving an additional name to a node. E.g. with \node [alias=a] (b) {foo}; you can use either \draw (a) -- .. or \draw (b) -- ... – Torbjørn T. Dec 7 '17 at 17:04
5

Something like this?

enter image description here

The trick is to use the tizlibrary calc to draw from (X)!0.5!(O) to (Y). That is, from half way between (X) and (O) to (Y).

Here's the full code:

\documentclass[border=10pt]{standalone}

\usepackage{tikz}
\usepackage{tikzscale}
\usetikzlibrary{arrows,automata,shadows,calc}

\begin{document}
    \centering
    \tikzstyle{vertex_basic}=[rectangle,thick,top color=white,bottom color=black!20, minimum size=50pt,scale=1]
    \tikzstyle{vertex}=[vertex_basic, draw=black!50]
    \tikzset{initial text={}}

    \begin{tikzpicture}[,>=stealth,ultra thick,black!50,text=black, scale=1.8,graphs/every graph/.style={edges=rounded corners},every new ->/.style={shorten >=1pt},auto]
    \matrix[row sep=20mm,column sep=2mm] {
        \node (X) [vertex,align=center] {X}; & & \node (Y) [vertex,align=center] {Y}; \\
        & \node (O) [vertex,align=center] {Outcome}; &\\
    };
    \path[<->] (X) edge node[align=center] {} (O);
    \path[<->] (Y) edge[densely dotted] node[align=left] {} (O);
    \path[<->] (X) edge[densely dotted] node[align=left] {} (Y);
    \draw[<->, red, very thick] ($ (X)!0.5!(O) $) -- (Y);
    \end{tikzpicture}
\end{document}

If you want to draw to the south west corner of Y then you can of course use

\draw[<->, red, very thick] ($ (X)!0.5!(O) $) -- (Y.south west);

to produce:

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.