5

I'm trying to learn TikZ. I would like an edge between two nodes but vertically shifted down a bit.

Why is this not working? Seems to only shift down on one side.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations, arrows}

\begin{document}

\begin{tikzpicture}
 \draw (0,0) -- (10,0);

  \foreach \x in {0,2,4,7,9}
      \draw (\x cm,3pt) -- (\x cm,-3pt);

% draw nodes
\draw (0,0) node[below=3pt](A) {$ 0 $} node[above=3pt] {$   $};
\draw (1,0) node[below=3pt](B) {$  $} node[above=3pt] {$ Y_{i1} $};

\draw (2,0) node[below=3pt](C) {$ t_{i1} $} node[above=3pt] {$  $};
\draw (3,0) node[below=3pt](D) {$  $} node[above=3pt] {$ Y_{i2} $};

\draw (4,0) node[below=3pt](E) {$ t_{i2} $} node[above=3pt] {$  $};
\draw (5.5,0) node[below=3pt](F) {$  $} node[above=3pt] {$ Y_{i3} $};

\draw (7,0) node[below=3pt](G) {$ t_{i3} $} node[above=3pt] {$  $};
\draw (8,0) node[below=3pt](H) {$  $} node[above=3pt] {$ Y_{i3} $};

\draw (9,0) node[below=3pt](I) {$ t_{i4} $} node[above=3pt] {$  $};


%\draw[latex'-latex'] (A) -- (C);
\draw[latex'-latex'] ([yshift=-5cm]A) -- ([yshift=-5cm]C);
\draw[latex'-latex'] (C) -- (E);
\draw[latex'-latex'] (E) -- (G);

\end{tikzpicture}
\end{document}

enter image description here

2

2 Answers 2

1

For example, the arrow can be shifted the following way. As anchor point for the shift, the example uses the north * anchors, to avoid a slanted line because of the different heights of the nodes.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, arrows}

\begin{document}

\begin{tikzpicture}[
  >=latex',
]
  \draw
    (0,0) -- (10,0)

    \foreach \x in {0,2,4,7,9} {
      (\x cm,3pt) -- (\x cm,-3pt)
    }

    % draw nodes
    (0,0) node[below=3pt](A) {$ 0 $}
    (1,0) node[above=3pt] {$ Y_{i1} $}

    (2,0) node[below=3pt](C) {$ t_{i1} $}
    (3,0) node[above=3pt] {$ Y_{i2} $}

    (4,0) node[below=3pt](E) {$ t_{i2} $}
    (5.5,0) node[above=3pt] {$ Y_{i3} $}

    (7,0) node[below=3pt](G) {$ t_{i3} $}
    (8,0) node[above=3pt] {$ Y_{i3} $}

    (9,0) node[below=3pt](I) {$ t_{i4} $}
  ;

  %\draw[latex'-latex'] (A) -- (C);
  \draw[<->] ($(A.north east) - (0, 1em)$) -- ($(C.north west) - (0, 1em)$);
  \draw[<->] (C) -- (E);
  \draw[<->] (E) -- (G);

\end{tikzpicture}
\end{document}

Result

If you want to correct the first arrow and move the left side down to get a horizontal line, then this can be achieved much easier via a \vphantom:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}[
  >=latex',
]
  \draw
    (0,0) -- (10,0)

    \foreach \x in {0,2,4,7,9} {
      (\x cm,3pt) -- (\x cm,-3pt)
    }

    % draw nodes
    (0,0) node[below=3pt](A) {$ 0\vphantom{t_{i1}} $}
    % Now node A has the same height and depth as node C
    (1,0) node[above=3pt] {$ Y_{i1} $}

    (2,0) node[below=3pt](C) {$ t_{i1} $}
    (3,0) node[above=3pt] {$ Y_{i2} $}

    (4,0) node[below=3pt](E) {$ t_{i2} $}
    (5.5,0) node[above=3pt] {$ Y_{i3} $}

    (7,0) node[below=3pt](G) {$ t_{i3} $}
    (8,0) node[above=3pt] {$ Y_{i3} $}

    (9,0) node[below=3pt](I) {$ t_{i4} $}
  ;

  \draw[<->] (A) -- (C);
  \draw[<->] (C) -- (E);
  \draw[<->] (E) -- (G);

\end{tikzpicture}
\end{document}

Result

5

I ended up using this code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations, arrows}


\begin{document}

\begin{tikzpicture}
 \draw (0,0) -- (10,0);

  \foreach \x in {0,2,4,7,9}
      \draw (\x cm,3pt) -- (\x cm,-3pt);

    % draw nodes
    \node[below=3pt](A) at (0,0){$ 0 $};
    \node[above=3pt](B) at (1,0){$ Y_{i1} $};

    \node[below=3pt](C) at (2,0){$ t_{i1} $};
    \node[above=3pt](D) at (3,0){$ Y_{i2} $};

    \node[below=3pt](E) at (4,0){$ t_{i2} $};
    \node[above=3pt](F) at (5.5,0){$ Y_{i3} $};

    \node[below=3pt](G) at (7,0){$ t_{i3} $};
    \node[above=3pt](H) at (8,0){$ Y_{i4} $};

    \node[below=3pt](L) at (9,0){$ t_{i4} $};

    \draw[<->, transform canvas={yshift=-0.5cm}] (A) -- (C) node [midway, below] {$S_1$};
    \draw[<->, transform canvas={yshift=-0.5cm}] (C) -- (E) node [midway, below] {$S_2$};
    \draw[<->, transform canvas={yshift=-0.5cm}] (E) -- (G) node [midway, below] {$S_3$};
    \draw[<->, transform canvas={yshift=-0.5cm}] (G) -- (L) node [midway, below] {$S_4$};
\end{tikzpicture}


\end{document}

enter image description here

1
  • In tikz, when there are several commands that share the same options, you can use \begin{scope}[<options>] ... \end{scope} to save copy/paste.
    – jarauh
    Sep 13, 2018 at 15:17

You must log in to answer this question.

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