LaTeX - Pgf/Tikz
Table of Contents
Introduction
Pgf/tikz is an excellent LaTeX package to plot high quality graphs. This post just summaries its basic usages.
Installation
Pgf/Tikz is a built-in package in the texlive. As long as texlive is ready, pgf/tikz is out-of-the-box.
Basic usage
Setting
\usepackage{tikz} ... \tikz ... \begin{tikzpicture} ... \end{tikzpicture}
Style
Create a new style
\tikzstyle{newstylename}=[option1=value1, option2=value2, ...]
For example, following styles can be defined for nodes in a flowchart.
\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, text width=5cm, draw=black, fill=red!30] \tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered, text width=5cm, draw=black, fill=blue!30] \tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, text width=5cm, draw=black, fill=orange!30] \tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, text width=5cm, draw=black, fill=green!30] \tikzstyle{arrow} = [thick, ->, >=stealth]
Modify an existing style
\tikzstyle{existingstylename}+=[option1=value1, option2=value2, ...]
Coordinate
(x, y)
(angle: radius)
List
% step length = 1 {a, ..., b} ... % step length = d {a, a + d, ..., b}
Arrow
- -
- ->
- <-
- <->
- ->>
Draw
Line
\draw (x1, y1) -- (x2, y2) -- (x3, y3) -- cycle
Curve
\draw (x1, y1) .. controls (x2, y2) and (x3, y3) .. (x4, y4); \draw (x1, y1) .. controls (x2, y2) .. (x3, y3);
Grid
\draw [step=s] (x1, y1) grid (x2, y2); \draw [xstep=x_s, ystep=y_s] (x1, y1) grid (x2, y2);
Arc
\draw (x1, y1) arc (start_angle: end_angle: radius);
Rectangle
\draw (x1, y1) rectangle (x2, y2); \fill [color] (x1, y1) rectangle (x2, y2); \filldraw [fill=color1, draw=color2] (x1, y1) rectangle (x2, y2); \shade [inner/top/left color=color1, outer/bottom/right color=color2] (x1, y1) rectangle (x2, y2); \shadedraw [inner/top/left color=color1, outer/bottom/right color=color2, draw=color3] (x1, y1) rectangle (x2, y2); \clip (x1, y1) rectangle (x2, y2);
Circle
\draw (x1, y1) circle (radius); \fill [color] (x1, y1) circle (radius); \filldraw [fill=color1, draw=color2] (x1, y1) circle (radius); \shade [inner/top/left color=color1, outer/bottom/right color=color2] (x1, y1) circle (radius); \shadedraw [inner/top/left color=color1, outer/bottom/right color=color2, draw=color3] (x1, y1) circle (radius); \clip (x1, y1) circle (radius);
Ellipse
\draw (x1, y1) ellipse (x_radius and y_radius); \fill [color] (x1, y1) ellipse (x_radius and y_radius); \filldraw [fill=color1, draw=color2] (x1, y1) ellipse (x_radius and y_radius); \shade [inner/top/left color=color1, outer/bottom/right color=color2] (x1, y1) ellipse (x_radius and y_radius); \shadedraw [inner/top/left color=color1, outer/bottom/right color=color2, draw=color3] (x1, y1) ellipse (x_radius and y_radius); \clip (x1, y1) ellipse (x_radius and y_radius);
Node
(x0, y0) node [anchor=north/south/east/west, shape=circle/rectangle/ellipse, draw=color1, fill=color2, label=angle:node_label_angle] (node_name) {node_label}; ... \node at (x0, y0) (node_name) {node_label};
Connecting nodes with
- Lines
\draw [arrow] (node_name_A) to (node_name_B); \draw (node_name_A) edge [arrow, color] (node_name_B);
- Curves
\draw [arrow] (node_name_A) to [bend left/right=30] (node_name_B); \draw [arrow] (node_name_A) to [in=30, out=60] (node_name_B); \draw (node_name_A) edge [arrow, color, in=30, out=60] (node_name_B); \draw (node_name_A) edge [arrow, color, bend left/right=30] (node_name_B);
Refer to nodes outside current picture
\tikzstyle{every picture}+=[remember picture] ... \tikz[overlay] \begin{tikzpicture}[overlay] ... \end{tikzpicture}
Mix tikz command with LaTeX
\tikz [baseline] ...
Libraries
Besides the basic tikz package, there are some libraries for dedicated purpose.
tikzcd
for commutative diagrams
Load the package
% Method-1 \usepackage{tikz} \usetikzlibrary{cd} % Method-2 \usepackage{tikz-cd}
- Usage
Environment for a commutative diagram
\begin{tikzcd}[OPTIONS] CONTENTS \end{tikzcd}
- The
CONTENTS
are structured in a matrix style, e.g., with&
and\\
to separate columns and rows respectively. - Everything inside
{tikz}
is typeset in math mode.
- The
Arrow In fact, an arrow object can follow either its starting or ending node (matrix element). But in my opinion, it is intuitionally natural to create an arrow object following its starting node.
\arrow[OPTIONS] \ar[OPTIONS]
where
OPTIONS
is a comma-separated list of options to specify an arrow, e.g.,- direction, consisting of a string of characters.
u
for upd
for downl
for leftr
for right
- label, quoted string on an arrow.
- An arrow can have an arbitrary number of labels, with common separated.
- If a label string consists of commas, then the label string should be enclosed in a pair of curly braces.
- Optionally label-specific options can follow a target label (without comma separated), e.g.,
- A color option, e.g.,
red
. - A location option, e.g.,
near start
,near end
. - An apostrophe (
'
) flips the label to the opposite side of the arrow. - A multiplicity of options should be separated with commas and enclosed in a pair of curly braces.
- A color option, e.g.,
- color
- line type, e.g.,
dashed
,dotted
, etc. - arrow type, e.g.,
leftarrow
,rightarrow
,leftrightarrow
,Leftarrow
,Rightarrow
,Leftrightarrow
, etc. - Fine tuning, e.g.,
shift left=1ex
,shift right=1ex
. crossing over
for 3-dimensional diagrams.marking
places a preceding label over the arrow.description
places a preceding label over the arrow with background filled.
- direction, consisting of a string of characters.
Global option
\tikzcdset{OPTIONS}
The options in
OPTIONS
are usually defined inevery diagram
,every cell
,every arrow
,every label
, etc. and reinitialized when the command above is called.every diagram
row sep=normal, column sep=normal, /tikz/baseline=0pt
every matrix
/tikz/inner sep=0pt
every cell
/tikz/shape=asymmetrical rectangle, /tikz/inner xsep=1ex, /tikz/inner ysep=0.85ex
every arrow
/tikz/draw, /tikz/line width=rule_thickness, rightarrow
every label
/tikz/auto, /tikz/font=something, /tikz/inner sep=0.5ex