\(\newcommand{\I}{\mathrm{i}} \newcommand{\E}{\mathrm{e}} \newcommand{\D}{\mathop{}\!\mathrm{d}} \newcommand{\bra}[1]{\langle{#1}|} \newcommand{\ket}[1]{|{#1}\rangle} \newcommand{\braket}[1]{\langle{#1}\rangle}\)
Workshop on scientific typesetting.
Graphics tools
You may find a large choice of graphic python libraries:
matplotlib
is the basic package to plot functions in 1D and 2D, basic histograms, contour and density plots, including text tools (you may use latex for the labels). See the gallery of examples. It can be associated with other libraries suchpandas
for data analysis, ornetworkx
for graph theory.holoviews
which aggregates many graphic tools and allow for interacting pictures integrated injupiter notebooks
.
There are also graphic libraries oriented to be used in \(\TeX\) documents:
TikZ
is the best solution to insert figures directly using the text source; it has a large variety of libraries to be used in various domains, as for instance elementary geometry, graph theory, networks or quantum circuits, see the gallery.Asymptote
a sophisticated graphic language, which includes 3D tools.
The python libraries are included in the anaconda distribution. The \(\TeX\) tools are included in the TeX Live distribution, you do not need to install them.
Matplotlib example
The Lennard Jones potential depends on two parameters, the hard core length \(r_0\) and the binding energy \(\epsilon\):
x = linspace(0.95, 2.5, 128) # x-points
y = 4*(1/x**12 - 1/x**6) # LJ
# plot
plot(x, y, 'k-')
plot([0,2.7], [0,0], ':', color = '0.35')
plot([0.8,1.4], [-1,-1], ':', color = '0.35')
plot([1,1], [-0.7,0.7], ':', color = '0.35')
# labels and text
xlabel(r'$r/r_0$')
ylabel(r'$w(r)/\epsilon$')
text(1.05,0.13, r"$r_0$")
text(0.6,-1, r"$-\epsilon$")
savefig('lj.pdf') # save figure in pdf format
Note that it is possible to embed python code directly in the tex source file using the package pythontex
(included in the texlive distribution).
In order to obtain compatible styles between matplotlib graphics and latex text, you may configure matplotlib to use a pgf
backend:
# numpy
import numpy as np
# matplotlib
import matplotlib
import matplotlib.pylab as plt
from matplotlib.colors import LogNorm
# figures (jupyter)
plt.rcParams.update({
"font.family": "serif",
"font.serif": "STIX Two Text",
"mathtext.fontset": "stix",
"font.size": 16,
"axes.titlesize": 24,
"axes.labelsize": 22,
"figure.figsize": (6.0,4.5),
"lines.linewidth": 0.5,
"xtick.top": True,
"ytick.right": True,
"savefig.bbox": "tight",
"image.cmap": "cubehelix",
})
# pgf backend
from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)
# latex font (pgf)
plt.rcParams.update({
"pgf.rcfonts": False,
"pgf.texsystem": "lualatex",
"pgf.preamble": "\n".join([
r"\usepackage{amsmath,trace}",
r"\usepackage{lualatex-math}",
r"\usepackage{mathtools}",
r"\usepackage{libertinus-otf}",
])
})
The first part of the code (“jupyter figure”) standardizes your figures for general use, for instance to create a .svg
figure with plt.savefig("figure.svg")
and use it in your web pages; the second part (“pgf backend”) writes a .pgf
file (plt.savefig("test.pgf")
) that you can input in your latex source:
\scalebox{0.7}{
\input{test.pgf}
}
here scaled to 70% of its original size.
TikZ ist kein Zeichenprogramm
TikZ and pgfplots are popular graphics packages. Tikz is specially useful for schematic drawing, diagrams, graphs with nodes and links, geometry, and in general it is perfect to illustrate mathematical and physical concepts. At variance, pgfplots needs often external programs (like gnuplot) and can be replaced by standard graphical programming libraries (matplotlib is an example).
tikz. The package is loaded with \usepackage{tikz}
you may also need to load specialized libraries like: \usetikzlibrary{calc,positioning,scopes,shapes}
.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,scopes,shapes}
...
%
\begin{document}
...
\begin{tikzpicture}
...
\end{tikzpicture}
...
\end{document}
tikzpicture. The picture environment, where you put your tikz code, is tikzpicture
. You may choose many options, for instance scale=2
or node distance=5mm
, etc. The style of the pictures can be globally defined by \tikzset{every picture/.style={line width=2pt, color=red!80}}
; if instead of every picture
, which is pre-defined, you use a name, this name is globally available; styles can also be defined as option of tikzpicture; options can also be defined inside the tikzpicture using the scope
environment; a convenient way to use scope
is to load the scopes
library.
path. The drawing commands are \path
, \draw
, \fill
and \clip
. Path and clip do not draw anything by themselves, but for example \path[draw]
is equivalent to \draw
. A path is basically a sequence of coordinates; the linking operator can be --
(segment) or ..
(curve).
Coordinates can be given as an ordered pair \((x,y)\) (canvas) or \((\phi:r)\) (canvas polar), or by name (node name)
; relative coordinates with respect to the current position are given bi \(+(,)\) (without update) or \(++(,)\) (with position update):
-
to draw a rectangle:
\path[draw] (0,0)--(0,1)--(1,1)--(1,0)--cycle
(‘cycles’ ensures a closed path), or a shifted one\draw (1,3) -- +(0,1) -- +(1,1) -- +(1,0) --cycle
, or equivalently\draw (1,3) -- ++(0,1) -- ++(1,0) -- ++(0,-1) --cycle
\begin{tikzpicture}
\path[draw] (0,0)--(0,1)--(1,1)--(1,0)--cycle;
\draw[fill=blue!60] (1,1.2) -- +(0,1) -- +(1,1) -- +(1,0) --cycle;
\draw[fill=red!60] (1,-1.2) -- ++(0,1) -- ++(1,0) -- ++(0,-1) --cycle;
\end{tikzpicture}
node. It is the tikz basic construction. It can be specified by \node['options'] ('name') at 'coordinate' {'label'}
, or as a part of a path:
\path (0,0) node {A} (1,0) node {B} (1,1) node {C} (1,-1) node {D};
A more complicate example, using fine positioning:
\begin{tikzpicture}[
every node/.style={draw, minimum size=1cm, fill=gray!25, rounded corners},
]
\node[minimum width=5cm] (M) {$M$};
\node[above=of M.north west, anchor=south west] (A) {$a$};
\node[above=of M] (B) {$b$};
\node[above=of M.north east, anchor=south east] (C) {$c$};
\draw[->] (M.north -| A) -- (A);
\draw[->] (M) -- (B);
\draw[->] (M.north -| C) -- (C);
\draw[-] (M.west) -- +(-1,0);
\draw[-] (M.east) -- +(1,0);
\end{tikzpicture}
Observe the relative positions in this case:
\begin{tikzpicture}
\fill[gray!50] (0,0) circle (2pt)
node[anchor=south,black] {$S$}
node[anchor=north] {$N$}
node[anchor=east] {$E$}
node[anchor=west] {$W$};
\end{tikzpicture}
You see that the dot (filled circle), which is the drawn object, is located to the south of the node labeled \(S\), the node is then anchored to the north of the object, and similarly for the other nodes.
Some node options are: shape (rectangle, circle, etc.), anchor (above, below, left, right, etc.), every node/.style={...}
, inner sep, outer sep (separation), minimum height, minimum width, and many others!
Nodes can be used as ‘coordinates’, this is useful to connect them by paths and edges:
\begin{tikzpicture}
\path (-1,0) node[circle,draw] (a) {$\uparrow$}
(0,1) node[circle,draw] (b) {$\downarrow$}
(1,0) node[rectangle,draw,red!80!black] (c) {$\uparrow$};
\path[<->] (a) edge node[above left] {$-J$} (b)
edge[red!50!black] node[below] {$+J$} (c)
(b) edge node[above right] {$-J$} (c);
\end{tikzpicture}
edge
inherits the options form the surrounding path
A sequence of similar path commands can be obtained using the foreach operation, \path ... foreach 'variables' ['options'] in {'path commands'} ... ;
\begin{tikzpicture}
\foreach \x in {0,1,2,3}
\draw (\x,0) -- (\x,3);
\foreach \y in {0,1,2,3}
\draw (0,\y) -- (3,\y);
\foreach \x in {0,1,2,3}
\foreach \y in {0,1,2,3}
\path (\x,\y) node[circle, fill=gray!20,
rotate=180*(0.5*rand+1),
draw] {$\rightarrow$} ;
\end{tikzpicture}
where we used rand
for a random orientation of the node arrows.
Example: quantum circuit
The following circuit describes the Deutsch algorithm, which allows determining if the boolean function is balanced, using a single evaluation of \(f(x)\). We use the package quantikz
by Alastair Kay:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{external,quantikz}
\tikzexternalize % create pdf of figures with the
% external library
% compile with the -shell-escape option
%
\begin{document}
\tikzset{ slice/.append style={gray}}
\begin{quantikz}
\lstick{$\ket{0}^{\otimes n}$} &
\gate{H^{\otimes n}} \slice{$\ket{\psi_1}$} &
\ctrl{1} \slice{$\ket{\psi_2}$} &
\gate{H^{\otimes n}} \slice{$\ket{\psi_3}$} &
\qw\\
\lstick{$\ket{1}$} &
\gate{H} &
\gate{y \otimes f(x)} &
\qw &
\qw
\end{quantikz}
\end{document}