Graphics and media/Drawing graphs and diagrams/TikZ
< Modules | Graphics and media >
TikZ/pgf is a portable graphics package developed for LaTeX, also supporting ConTeXt. It is a possible alternative to Graphical programming with MetaPost and MetaFun.
TikZ is included as a (third party) module for ConTeXt.
Contents
Example
From the TikZ/pgf manual.
\setupcolors[state=start] \definecolor[darkgreen][g=.5] \usemodule[tikz] \starttext \starttikzpicture[ scale=3,line cap=round axes/.style=, important line/.style={very thick}, information text/.style={rounded corners,fill=red!10,inner sep=1ex} ] \draw[help lines,step=0.5cm] (-1.4,-1.4) grid (1.4,1.4); \draw (0,0) circle (1cm); \draw[->] (-1.5,0) -- (1.5,0) node[right] {$x$} coordinate(x axis); \draw[->] (0,-1.5) -- (0,1.5) node[above] {$y$} coordinate(y axis); \foreach \x/\xtext in {-1, -.5/-\frac{1}{2}, 1} \draw[xshift=\x cm] (0pt,1pt) -- (0pt,-1pt) node[below,fill=white] {$\xtext$}; \foreach \y/\ytext in {-1, -.5/-\frac{1}{2}, .5/\frac{1}{2}, 1} \draw[yshift=\y cm] (1pt,0pt) -- (-1pt,0pt) node[left,fill=white] {$\ytext$}; \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0pt) arc(0:30:3mm); \draw (15:2mm) node[green!50!black] {$\alpha$}; \draw[important line,red] (30:1cm) -- node[left=1pt,fill=white] {$\sin \alpha$} (30:1cm |- x axis); \draw[important line,blue] (30:1cm |- x axis) -- node[below=2pt,fill=white] {$\cos \alpha$} (0,0); \draw[important line,orange!80!black] (1,0) -- node[right=1pt,fill=white] { $\displaystyle \tan \alpha \black = \frac{{\red \sin \alpha}}{\blue \cos \alpha}$} (intersection of 0,0--30:1cm and 1,0--1,1) coordinate (t); \draw (0,0) -- (t); \draw[xshift=1.85cm] node[right,text width=6cm,information text] { The {\darkgreen angle $\alpha$} is $30^\circ$ in the example ($\pi/6$ in radians). The {\red sine of $\alpha$}, which is the height of the red line, is \startformula {\red \sin \alpha} = 1/2. \stopformula By the Theorem of Pythagoras ... }; \stoptikzpicture \stoptext
Placing TikZ graphics
You should wrap TikZ graphics inside \hbox when placing them, for example:
\placefigure[here][fig:myfig]{My figure.}{\hbox{\starttikzpicture \draw (0, 0) circle (3cm); \stoptikzpicture}}
Otherwise, positioning will not work (figure will not be centered). See this post by Aditya Mahajan for explanation.
Example for commutative diagrams
Hans Åberg wrote to the mailing list on 2016-10-06:
There is a tikz-cd package at CTAN: https://www.ctan.org/tex-archive/graphics/pgf/contrib/tikz-cd?lang=en
In order to use it, download the the file tikzlibrarycd.code.tex
and put it in CONTEXT_HOME/tex/texmf-modules/tex/generic/pgf/libraries/
. Then run
mtxrun --generate context --make
You should now be able to use tikz-cd.
Makes sure to follow the translation rules in the manual for ConTeXt. In addition to that, it seems that you have to replace the apostrophe with "below".
The first few examples in the manual, which compiled with latest ConTeXt:
\usemodule[tikz] \usetikzlibrary[cd] \starttext \starttikzcd A \arrow{rd} \arrow{r}{𝜑} \NC B \NR \NC C \stoptikzcd \starttikzcd A \arrow[r, "\phi"] \arrow[d, red] \NC B \arrow[d, "\psi" red] \NR C \arrow[r, red, "\eta" blue] \NC D \stoptikzcd \starttikzcd A \arrow[r, "\phi" near start, "\psi" below, "\eta" near end] \NC B \stoptikzcd \starttikzcd T \arrow[drr, bend left, "x"] \arrow[ddr, bend right, "y"] \arrow[dr, dotted, "{(x,y)}" description] \NC \NC \NR \NC X \times_Z Y \arrow[r, "p"] \arrow[d, "q"] \NC X \arrow[d, "f"] \NR \NC Y \arrow[r, "g"] \NC Z \stoptikzcd \starttikzcd[column sep=tiny] \NC \pi_1(U_1) \ar[dr] \ar[drr, "j_1", bend left=20] \NC \NC[1.5em] \NR \pi_1(U_1\cap U_2) \ar[ur, "i_1"] \ar[dr, "i_2" below] \NC \NC \pi_1(U_1) \ast_{ \pi_1(U_1\cap U_2)} \pi_1(U_2) \ar[r, dashed, "\simeq"] \NC \pi_1(X) \NR \NC \pi_1(U_2) \ar[ur]\ar[urr, "j_2" below, bend right=20] \NC \NC \stoptikzcd \stoptext
Tips for converting LaTeX TikZ diagrams to ConTeXt
TikZ has a vibrant and productive community, thanks to whom numerous examples an be found on the web. Most of them, however are written for LaTeX and must be slightly adapted to work with ConTeXt. Here we list a few tips to do the conversion:
- Replace
\usepackage{tikz}
with\usemodule[tikz]
and\usepackage{pgfplots}
with\usemodule[pgfplots]
. - Replace all instances of
\begin{XXX}
(whereXXX
is e.g.tikzpicture
,scope
, ...) with\startXXX
. - Replace all instances of
\end{XXX}
with\stopXXX
. - Modify calls to
\definecolor
to match the ConTeXt syntax (see \definecolor).
See also
Discussion on the ConTeXt user's list.