- #1
- 16,336
- 258
Hi everyone! ;)
We are very excited to announce the introduction of TikZ pictures.
MHB is the very first math site ever to allow members to post TikZ pictures! (Emo)
To add for instance a plot, we can now use:
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}
\addplot coordinates {(0,1) (0.5,1) (1,1.2)};
\end{axis}
\end{tikzpicture}
Syntax
TikZ has a dedicated syntax for every area of expertise we want to make drawings for.
So dedicated, that it almost doesn't seem feasible to learn all of it.
The easiest way appears to google for a tikz example of a specific type of diagram, and modify it to our own needs.
TikZ is extensively documented all over the web.
Just google for instance for tikz state diagram to see how to create a state diagram.
In each area of expertise the syntax is designed to be as simple and intuitive as possible.
And if it's not clear how an example should be modified, we can check the main TikZ package documentation. Specifically for plots we may want to use the pgfplots package (as in the example above), which is documented in the pgfplots manual.
That said, there are a number of common and intuitive elements.
For instance, a point is just (x,y), and a line is 2 points connected by
If we want to modify an attribute, for instance use an arrow or use a bent line, we add something in square brackets, such as
As for what we should add, we'll have to look up what the options are, or deduce it from an example.
The general layout is $\LaTeX$ with its
Note that this latex should be standalone.
It should not be enclosed in latex tags, such as [MATH], \$\$, or \$.
As a side note, we can use [CODE=latex][/CODE] tags to quote latex with syntax highlighting, as we have done to quote the various latex fragments in this post.
TikZ Live Editor
To make things easier, we have an interactive https://tikzimages.mathhelpboards.com/tikz/tikzlive.html that creates pictures on the fly while you are typing.
It contains templates for commonly used pictures on MHB.
Copy and paste to get a picture in a post.
Toolbar button
To make things more visible, we've added a button on the toolbar with this icon:
http://mathhelpboards.com/images/markfl_bbcode/tikz.png
This will generate TIKZ tags which will be interpreted as the beginning and ending TikZ tags.
So if we enter:
we'll get the same picture.
Custom preamble
We have the option to specify the preamble with
It means we can include any package or library that has our fancy.
So if we create a picture of the form
the resulting document is:
Example function graph
Just to give a couple of additional examples:
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}[xmin=-1.5, xmax=1.5, samples=101]
\addplot[blue, ultra thick] (x, {cos(deg(x)) / (3*x^2 - pi^2)});
\end{axis}
\end{tikzpicture}
Note in particular that $\cos$ takes degrees as a parameter instead of radians.
Example state diagram
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,auto]
%preamble \usetikzlibrary{automata}
\node[state,initial] (q_0) {$q_0$};
\node[state,accepting] (q_1) [right of=q_0] {$q_1$};
\path[->] (q_0) edge [bend left] node {$a$} (q_1)
(q_1) edge [bend left] node {$b$} (q_0);
\end{tikzpicture}
Example graph of a hyperbola
\begin{tikzpicture}
%preamble \usepackage{amsmath}
\draw[gray!50, very thin,->] (-4,0) -- (4,-0); % x-axis
\draw[gray!50, very thin,->] (0,-3) -- (0,3); % y-axis
\draw[gray!50, very thin] (-2,-1.5) rectangle (2,1.5);
\draw[red,line width=2pt] (2.5,0) -- (2.5,1.125) node[right=1pt] {$\ell=\dfrac{b^2}{a}$}; % semi latus rectum
\draw[domain=-1.3:1.3,smooth,variable=\t,line width=2pt] plot ({2*cosh(\t)},{1.5*sinh(\t)});
\draw[domain=-1.3:1.3,smooth,variable=\t,line width=2pt] plot ({-2*cosh(\t)},{1.5*sinh(\t)});
\node at (-1.2,-2.5) {$\dfrac{x^2}{a^2} - \dfrac{y^2}{b^2}=1$ };
\node at (1.5,-2.5) {$r=\dfrac{b^2}{a - c \cos\theta}$ };
\node at (-1.5,2.5) {$(\pm a \cosh u, b \sinh u)$ };
\draw (-4,-3) -- (4,3);
\draw (4,-3) -- (-4,3);
\node at (-1.2,0.1) {a};
\node at (-1.85,0.75) {b};
\node at (-0.85,0.85) {c};
\draw[<->, green] (0,-0.3) -- (2.5,-0.3);
\node[green] at (1.25,-4pt) {c};
\fill (-2.5,0) circle (0.1); % focus
\fill (2.5,0) circle (0.1); % focus
\end{tikzpicture}
Example histogram
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}[ tiny,
width=6in,
xtick=data,
ymin=0 ]
\addplot+[
hist={bins=10, density},
fill=blue!20,
mark=none,
] table[ row sep=\\, y index=0 ] {%
data \\ 565 \\ 786 \\ 870 \\ 923 \\ 948 \\ 951 \\ 964 \\ 968 \\
997 \\1007 \\1013 \\1037 \\1040 \\1051 \\1056 \\1080 \\
1088 \\1090 \\1102 \\1103 \\1104 \\1120 \\1151 \\1159 \\
1165 \\1185 \\1189 \\1207 \\1216 \\1233 \\1251 \\1256 \\
1261 \\1292 \\1312 \\1317 \\1347 \\1358 \\1385 \\1416 \\
1477 \\1500 \\1514 \\1567 \\1592 \\1588 \\1615 \\1713 \\
2325 \\3168 \\
};
\end{axis}
\end{tikzpicture}Example astronomical drawing
\begin{tikzpicture}
\draw (-4,0) -- (4,0);
\draw (-4,0) circle (1.5);
\draw (-4,-1.5) -- (4,-.2);
\fill[draw=black!50,top color=blue!80,bottom color=black!40]
(-4,0) circle (.5) node {Earth};
\fill[draw=black!50,top color=orange!80,bottom color=black!40]
(4,0) circle (1) node {Sun};
\fill[draw=black!50,top color=gray,bottom color=black!20]
(-4,-1.5) circle (.1) node[below = 1pt] {Satellite};
\end{tikzpicture}Happy TikZ drawing! (Blush)
We are very excited to announce the introduction of TikZ pictures.
MHB is the very first math site ever to allow members to post TikZ pictures! (Emo)
To add for instance a plot, we can now use:
Code:
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}
\addplot coordinates {(0,1) (0.5,1) (1,1.2)};
\end{axis}
\end{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}
\addplot coordinates {(0,1) (0.5,1) (1,1.2)};
\end{axis}
\end{tikzpicture}
Syntax
TikZ has a dedicated syntax for every area of expertise we want to make drawings for.
So dedicated, that it almost doesn't seem feasible to learn all of it.
The easiest way appears to google for a tikz example of a specific type of diagram, and modify it to our own needs.
TikZ is extensively documented all over the web.
Just google for instance for tikz state diagram to see how to create a state diagram.
In each area of expertise the syntax is designed to be as simple and intuitive as possible.
And if it's not clear how an example should be modified, we can check the main TikZ package documentation. Specifically for plots we may want to use the pgfplots package (as in the example above), which is documented in the pgfplots manual.
That said, there are a number of common and intuitive elements.
For instance, a point is just (x,y), and a line is 2 points connected by
--
, such as (0,0) -- (1,0)
.If we want to modify an attribute, for instance use an arrow or use a bent line, we add something in square brackets, such as
[->]
respectively [bend left]
, as in \draw[bend left] (0,0) to (5,5);
As for what we should add, we'll have to look up what the options are, or deduce it from an example.
The general layout is $\LaTeX$ with its
\
directives and parameters that can be grouped with {...}
.Note that this latex should be standalone.
It should not be enclosed in latex tags, such as [MATH], \$\$, or \$.
As a side note, we can use [CODE=latex][/CODE] tags to quote latex with syntax highlighting, as we have done to quote the various latex fragments in this post.
TikZ Live Editor
To make things easier, we have an interactive https://tikzimages.mathhelpboards.com/tikz/tikzlive.html that creates pictures on the fly while you are typing.
It contains templates for commonly used pictures on MHB.
Copy and paste to get a picture in a post.
Toolbar button
To make things more visible, we've added a button on the toolbar with this icon:
http://mathhelpboards.com/images/markfl_bbcode/tikz.png
This will generate TIKZ tags which will be interpreted as the beginning and ending TikZ tags.
So if we enter:
Code:
[TIKZ]
%preamble \usepackage{pgfplots}
\begin{axis}
\addplot coordinates {(0,1) (0.5,1) (1,1.2)};
\end{axis}
[/TIKZ]
Custom preamble
We have the option to specify the preamble with
%preamble
directives.It means we can include any package or library that has our fancy.
So if we create a picture of the form
Code:
\begin{tikzpicture}
%preamble \usepackage{amsmath}
%preamble \usetikzlibrary{arrows}
...
\end{tikzpicture}
Code:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
...
\end{tikzpicture}
\end{document}
Example function graph
Just to give a couple of additional examples:
Code:
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}[xmin=-1.5, xmax=1.5, samples=101]
\addplot[blue, ultra thick] (x, {cos(deg(x)) / (3*x^2 - pi^2)});
\end{axis}
\end{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}[xmin=-1.5, xmax=1.5, samples=101]
\addplot[blue, ultra thick] (x, {cos(deg(x)) / (3*x^2 - pi^2)});
\end{axis}
\end{tikzpicture}
Note in particular that $\cos$ takes degrees as a parameter instead of radians.
Example state diagram
Code:
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,auto]
%preamble \usetikzlibrary{automata}
\node[state,initial] (q_0) {$q_0$};
\node[state,accepting] (q_1) [right of=q_0] {$q_1$};
\path[->] (q_0) edge [bend left] node {$a$} (q_1)
(q_1) edge [bend left] node {$b$} (q_0);
\end{tikzpicture}
%preamble \usetikzlibrary{automata}
\node[state,initial] (q_0) {$q_0$};
\node[state,accepting] (q_1) [right of=q_0] {$q_1$};
\path[->] (q_0) edge [bend left] node {$a$} (q_1)
(q_1) edge [bend left] node {$b$} (q_0);
\end{tikzpicture}
Example graph of a hyperbola
Code:
\begin{tikzpicture}
%preamble \usepackage{amsmath}
\draw[gray!50, very thin,->] (-4,0) -- (4,-0); % x-axis
\draw[gray!50, very thin,->] (0,-3) -- (0,3); % y-axis
\draw[gray!50, very thin] (-2,-1.5) rectangle (2,1.5);
\draw[red,line width=2pt] (2.5,0) -- (2.5,1.125) node[right=1pt] {$\ell=\frac{b^2}{a}$}; % semi latus rectum
\draw[domain=-1.3:1.3,smooth,variable=\t,line width=2pt] plot ({2*cosh(\t)},{1.5*sinh(\t)});
\draw[domain=-1.3:1.3,smooth,variable=\t,line width=2pt] plot ({-2*cosh(\t)},{1.5*sinh(\t)});
\node at (-1.2,-2.5) {$\frac{x^2}{a^2} - \frac{y^2}{b^2}=1$ };
\node at (1.5,-2.5) {$r=\frac{b^2}{a - c \cos\theta}$ };
\node at (-1.5,2.5) {$(\pm a \cosh u, b \sinh u)$ };
\draw (-4,-3) -- (4,3);
\draw (4,-3) -- (-4,3);
\node at (-1.2,0.1) {a};
\node at (-1.85,0.75) {b};
\node at (-0.85,0.85) {c};
\draw[<->, green] (0,-0.3) -- (2.5,-0.3);
\node[green] at (1.25,-4pt) {c};
\fill (-2.5,0) circle (0.1); % focus
\fill (2.5,0) circle (0.1); % focus
\end{tikzpicture}
%preamble \usepackage{amsmath}
\draw[gray!50, very thin,->] (-4,0) -- (4,-0); % x-axis
\draw[gray!50, very thin,->] (0,-3) -- (0,3); % y-axis
\draw[gray!50, very thin] (-2,-1.5) rectangle (2,1.5);
\draw[red,line width=2pt] (2.5,0) -- (2.5,1.125) node[right=1pt] {$\ell=\dfrac{b^2}{a}$}; % semi latus rectum
\draw[domain=-1.3:1.3,smooth,variable=\t,line width=2pt] plot ({2*cosh(\t)},{1.5*sinh(\t)});
\draw[domain=-1.3:1.3,smooth,variable=\t,line width=2pt] plot ({-2*cosh(\t)},{1.5*sinh(\t)});
\node at (-1.2,-2.5) {$\dfrac{x^2}{a^2} - \dfrac{y^2}{b^2}=1$ };
\node at (1.5,-2.5) {$r=\dfrac{b^2}{a - c \cos\theta}$ };
\node at (-1.5,2.5) {$(\pm a \cosh u, b \sinh u)$ };
\draw (-4,-3) -- (4,3);
\draw (4,-3) -- (-4,3);
\node at (-1.2,0.1) {a};
\node at (-1.85,0.75) {b};
\node at (-0.85,0.85) {c};
\draw[<->, green] (0,-0.3) -- (2.5,-0.3);
\node[green] at (1.25,-4pt) {c};
\fill (-2.5,0) circle (0.1); % focus
\fill (2.5,0) circle (0.1); % focus
\end{tikzpicture}
Example histogram
Code:
\begin{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}[ tiny,
width=6in,
xtick=data,
ymin=0 ]
\addplot+[
hist={bins=10, density},
fill=blue!20,
mark=none,
] table[ row sep=\\, y index=0 ] {%
data \\ 565 \\ 786 \\ 870 \\ 923 \\ 948 \\ 951 \\ 964 \\ 968 \\
997 \\1007 \\1013 \\1037 \\1040 \\1051 \\1056 \\1080 \\
1088 \\1090 \\1102 \\1103 \\1104 \\1120 \\1151 \\1159 \\
1165 \\1185 \\1189 \\1207 \\1216 \\1233 \\1251 \\1256 \\
1261 \\1292 \\1312 \\1317 \\1347 \\1358 \\1385 \\1416 \\
1477 \\1500 \\1514 \\1567 \\1592 \\1588 \\1615 \\1713 \\
2325 \\3168 \\
};
\end{axis}
\end{tikzpicture}
%preamble \usepackage{pgfplots}
\begin{axis}[ tiny,
width=6in,
xtick=data,
ymin=0 ]
\addplot+[
hist={bins=10, density},
fill=blue!20,
mark=none,
] table[ row sep=\\, y index=0 ] {%
data \\ 565 \\ 786 \\ 870 \\ 923 \\ 948 \\ 951 \\ 964 \\ 968 \\
997 \\1007 \\1013 \\1037 \\1040 \\1051 \\1056 \\1080 \\
1088 \\1090 \\1102 \\1103 \\1104 \\1120 \\1151 \\1159 \\
1165 \\1185 \\1189 \\1207 \\1216 \\1233 \\1251 \\1256 \\
1261 \\1292 \\1312 \\1317 \\1347 \\1358 \\1385 \\1416 \\
1477 \\1500 \\1514 \\1567 \\1592 \\1588 \\1615 \\1713 \\
2325 \\3168 \\
};
\end{axis}
\end{tikzpicture}Example astronomical drawing
Code:
\begin{tikzpicture}
\draw (-4,0) -- (4,0);
\draw (-4,0) circle (1.5);
\draw (-4,-1.5) -- (4,-.2);
\fill[draw=black!50,top color=blue!80,bottom color=black!40]
(-4,0) circle (.5) node {Earth};
\fill[draw=black!50,top color=orange!80,bottom color=black!40]
(4,0) circle (1) node {Sun};
\fill[draw=black!50,top color=gray,bottom color=black!20]
(-4,-1.5) circle (.1) node[below = 1pt] {Satellite};
\end{tikzpicture}
\draw (-4,0) -- (4,0);
\draw (-4,0) circle (1.5);
\draw (-4,-1.5) -- (4,-.2);
\fill[draw=black!50,top color=blue!80,bottom color=black!40]
(-4,0) circle (.5) node {Earth};
\fill[draw=black!50,top color=orange!80,bottom color=black!40]
(4,0) circle (1) node {Sun};
\fill[draw=black!50,top color=gray,bottom color=black!20]
(-4,-1.5) circle (.1) node[below = 1pt] {Satellite};
\end{tikzpicture}Happy TikZ drawing! (Blush)
Last edited: