3

I am trying to recreate the following drawing with TikZ.

Small graph

I have explored many different options but encountered the following problems:

  • Haven't figured out how to make small "point-like" nodes.
  • I was able to highlight some paths in one color, but not two colors at the same time.

I'm not asking for a full creation of the drawing by one of the members, just a nudge in the right direction for the problems I have.

Thanks in advance!

EDIT:

Here's what I have so far.

 \begin{tikzpicture}

    \path [draw, fill] (0,0) circle (1pt);
    \path [draw, fill] (1,-1) circle (1pt);
    \path [draw, fill] (2,0) circle (1pt);
    \path [draw, fill] (3,-1) circle (1pt);
    \path [draw, fill] (4,0) circle (1pt);
    \path [draw, fill] (5,-1) circle (1pt);
    \path [draw, fill] (6,0) circle (1pt);
    \path [draw, fill] (7,0) circle (1pt);
    \path [draw, fill] (8,1) circle (1pt);
    \path [draw, fill] (9,2) circle (1pt);

    \draw [red] (0,0) -- (1,-1) -- (2,0) -- (3,-1)  -- (4,0) -- (5,-1) -- (6,0) -- (7,0) -- (8,1) -- (9,2) -- cycle;

    \draw [green] (0,0) -- (1,-1) -- (2,0) -- (3,-1)  -- (9,2) -- (8,1) -- (7,0) -- (6,0) -- (5,-1) -- (4,0) -- cycle;

\end{tikzpicture}

Which leads to this: First try!

Problems:

  • How do I label points that were drawn with \path ?
  • The two colored paths' colors blend together.
3
  • 1
    Could you post the code you've got so far? It is easier to nudge if we can see exactly where to nudge from. For the second colour, you will need to draw the path a second time, I think. You could use e.g. preaction or postaction for this. For the points, \path [draw, fill] (0,0) circle (.5pt); or similar.
    – cfr
    Sep 23, 2015 at 23:53
  • @cfr Edited the answer with a small code snippet using what you suggested. Don't know the "preaction" and "postaction" instructions, could you expand on that?
    – Balaam
    Sep 24, 2015 at 0:20
  • There's probably a better way. I can do it using scopes and shift but it is ridiculously fiddly.
    – cfr
    Sep 24, 2015 at 1:15

1 Answer 1

5

Here's one option:

enter image description here

The code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,backgrounds}

\newcommand\Twocolor[4]{
\path 
  let 
  \p1=($ #1 - #2 $ ),
  \p2=#1,
  \p3=#2
  in 
  node[
    draw,
    outer sep=0pt,
    inner sep=0pt,
    minimum height=3pt,
    text width={veclen(\x1,\y1)},
    rotate={atan((\y3-\y2)/(\x3-\x2))},
    anchor=west
  ] (mydouble) at #1 {};
  \draw[#3,line width=1pt] (mydouble.north west) -- (mydouble.north east); 
  \draw[#4,line width=1pt] (mydouble.south west) -- (mydouble.south east); 
}

\begin{document}

\begin{tikzpicture}[
  mydot/.style={
    fill,
    circle,
    inner sep=2pt
  },
]
\node[mydot,label={left:$v_{0}$}] (v0) {};
\node[mydot,below right=of v0] (v1) {};
\node[mydot,above right=of v1] (v2) {};
\node[mydot,below right=of v2,label={below:$v_{i}$}] (v3) {};
\node[mydot,above right=of v3,label={right:$v_{i+1}$}] (v4) {};
\node[mydot,below right=of v4] (v5) {};
\node[mydot,above right=of v5] (v6) {};
\node[mydot,right=of v6] (v7) {};
\node[mydot,above right=of v7] (v8) {};
\node[mydot,above right=of v8,label={right:$v_{k}$}] (v9) {};
\begin{pgfonlayer}{background}
\Twocolor{(v0)}{(v1)}{red}{green}
\Twocolor{(v1)}{(v2)}{red}{green}
\Twocolor{(v2)}{(v3)}{red}{green}
\Twocolor{(v4)}{(v5)}{green}{red}
\Twocolor{(v5)}{(v6)}{green}{red}
\Twocolor{(v6)}{(v7)}{green}{red}
\Twocolor{(v7)}{(v8)}{green}{red}
\Twocolor{(v8)}{(v9)}{green}{red}
\end{pgfonlayer}
\draw[red,line width=1pt] 
  (v3) -- (v4);
\draw[green,line width=1pt] 
  (v0) to[out=45,in=135] (v4)
  (v3) to[out=-45,in=-20,looseness=1] (v9);
\end{tikzpicture}

\end{document}

The two-color lines are drawn using a command

\Twocolor{<start-coord>}{<end-coord>}{<upper color>}{<lower color>}
1
  • This looks absolutely spot-on! Thank you very much!
    – Balaam
    Sep 24, 2015 at 9:41

You must log in to answer this question.

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