Chapter 5 – Using styles and pics

Here are the Code examples of this chapter. These pages will be updated over time (sectioning, adding pictures, captions, and possibly further examples). I will synchronize the content with GitHub. Finally, the chapter pages and Code examples will be moved to a dedicated website TikZ.org.

Small example

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \node (A) {A};
  \node[right= 4 cm of A] (B) {B};
  \draw (A) edge node [fill=white] {1} (B);
\end{tikzpicture}
\end{document}

Node with style

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \node [font = \sffamily\bfseries, text=white, shape = circle, ball color = blue] (A) {A};
\end{tikzpicture}
\end{document}

Small graph

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{vertex/.style = {font = \sffamily\bfseries, text = white,
      shape = circle, ball color = blue}}
\tikzset{bridge/.style = {thick, double = yellow,
      double distance = 1pt}}
\tikzset{number/.style = {font = \sffamily\bfseries, text = white,
      draw, fill = red}}
\begin{document}
\begin{tikzpicture}
  \node[vertex] (A) {A};
  \node[vertex, right = 4 cm of A] (B) {B};
  \draw (A) edge[bridge] node [number] {1} (B);
  \end{tikzpicture}
\end{document}

Konigsberg Bridges

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
  vertex/.style = {font = \sffamily\bfseries, text = white,
          shape = circle, ball color = blue},
  bridge/.style = {thick, double = yellow,
          double distance = 1pt},
  number/.style = {font = \sffamily\bfseries, text = white,
          draw, fill = red}}
\begin{document}
\begin{tikzpicture}
  \node[vertex] (A) {A};
  \node[vertex, right = 4 cm of A] (B) {B};
  \draw (A) edge [bridge] node [number] {1} (B);
  \node[vertex, below = 2cm of A] (C) {C};
  \node[vertex, above = 2cm of A] (D) {D};
  \tikzset{bridge/.append style = {bend right}}
  \draw (C) edge [bridge] node [number] {2} (B)
        (B) edge [bridge] node [number] {3} (D)
        (C) edge [bridge] node [number] {4} (A)
        (A) edge [bridge] node [number] {5} (C)
        (A) edge [bridge] node [number] {6} (D) 
        (D) edge [bridge] node [number] {7} (A);
\end{tikzpicture}
\end{document}

Inheriting styles

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
  mytext/.style = {font=\sffamily\bfseries, text=white},
  vertex/.style = {mytext, shape = circle,
                   ball color = blue},
  number/.style = {mytext, draw, fill = red}}
\tikzset{
  bridge/.style = {thick, double = yellow,
          double distance = 1pt}}
\begin{document}
\begin{tikzpicture}
  \node[vertex] (A) {A};
  \node[vertex, right = 4 cm of A] (B) {B};
  \draw (A) edge [bridge] node [number] {1} (B);
  \node[vertex, below = 2cm of A] (C) {C};
  \node[vertex, above = 2cm of A] (D) {D};
  \tikzset{bridge/.append style = {bend right}}
  \draw (C) edge [bridge] node [number] {2} (B)
        (B) edge [bridge] node [number] {3} (D)
        (C) edge [bridge] node [number] {4} (A)
        (A) edge [bridge] node [number] {5} (C)
        (A) edge [bridge] node [number] {6} (D) 
        (D) edge [bridge] node [number] {7} (A);
\end{tikzpicture}
\end{document}

Highlight vertex and label

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
  mytext/.style = {font=\sffamily\bfseries, text=white},
  vertex/.style = {mytext, shape = circle,
                   ball color = blue},
  number/.style = {mytext, draw, fill = red}}
\tikzset{
  bridge/.style = {thick, double = yellow,
          double distance = 1pt}}
\tikzset{highlight/.style = {draw=yellow, very thick, densely dotted},
         highlight vertex/.style = {vertex, highlight},
         highlight number/.style = {number, highlight}}
\begin{document}
\begin{tikzpicture}
  \node[highlight vertex] (A) {A};
  \node[vertex, right = 4 cm of A] (B) {B};
  \draw (A) edge [bridge] node [highlight number] {1} (B);
  \node[vertex, below = 2cm of A] (C) {C};
  \node[vertex, above = 2cm of A] (D) {D};
  \tikzset{bridge/.append style = {bend right}}
  \draw (C) edge [bridge] node [number] {2} (B)
        (B) edge [bridge] node [number] {3} (D)
        (C) edge [bridge] node [number] {4} (A)
        (A) edge [bridge] node [number] {5} (C)
        (A) edge [bridge] node [number] {6} (D) 
        (D) edge [bridge] node [number] {7} (A);
\end{tikzpicture}
\end{document}

One parameter

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{vertex/.style = {font = \sffamily\bfseries, text = white,
      shape = circle, ball color = #1}}
\tikzset{bridge/.style = {thick, double = yellow,
      double distance = 1pt}}
\tikzset{number/.style = {font = \sffamily\bfseries, text = white,
      draw, fill = red}}
\begin{document}
\begin{tikzpicture}
  \node[vertex=blue] (A) {A};
  \node[vertex=red, right = 4 cm of A] (B) {B};
  \draw (A) edge[bridge] node [number] {1} (B);
  \end{tikzpicture}
\end{document}

One parameter with default value

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{vertex/.style = {font = \sffamily\bfseries, text = white,
      shape = circle, ball color = #1},
      vertex/.default = blue}
\tikzset{bridge/.style = {thick, double = yellow,
      double distance = 1pt}}
\tikzset{number/.style = {font = \sffamily\bfseries, text = white,
      draw, fill = red}}
\begin{document}
\begin{tikzpicture}
  \node[vertex] (A) {A};
  \node[vertex=red, right = 4 cm of A] (B) {B};
  \draw (A) edge[bridge] node [number] {1} (B);
  \end{tikzpicture}
\end{document}

Smiley pics

\documentclass[tikz,border=10pt]{standalone}
\tikzset{smiley/.pic={
  \draw[shading=ball, ball color=yellow] (0,0)
    circle [radius=2];
  \draw[shading=ball, ball color=black] (-0.5,0.5,0)
    ellipse [x radius=0.2, y radius=0.4];
  \draw[shading=ball, ball color=black] (0.5,0.5,0)
    ellipse [x radius=0.2, y radius=0.4];
  \draw[very thick] (-1,-1) arc [start angle=185,
    end angle=355, x radius=1, y radius=0.5];}}
\begin{document}
\begin{tikzpicture}
\draw
  (0,0)    pic{smiley}
  (2,2)    pic [scale=0.5,  rotate=-30] {smiley} 
  (-2,1.5) pic [scale=0.3,  rotate= 30] {smiley} 
  (-1.6,2) pic [scale=0.15, rotate=-20] {smiley} 
  (0,2)    pic [scale=0.2,  rotate=-10] {smiley};
\end{tikzpicture}
\end{document}