Chapter 5 – Using styles and pics

Here are the Code examples of this chapter. You can compile them online right on this web page by pressing the Typeset / Compile button. You can also edit them for testing, and compile again.

For a better view with the online compiler, I use \documentclass[border=10pt]{standalone} instead of \documentclass{article}. Instead of having a big letter/A4 page, the standalone class crops the paper to see just the visible text without an empty rest of a page.

Any question about a code example? Post it on LaTeX.org, I will answer. As forum admin I read every single question there. (profile link).

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}

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}

xxxxxxxx

xxxxxxxxxx

xxxxxxxx

xxxxxxxxxx

xxxxxxxx

xxxxxxxxxx

xxxxxxxx

xxxxxxxxxx

xxxxxxxx

xxxxxxxxxx

xxxxxxxx

xxxxxxxxxx