MHB Introduces: Creating TikZ Pictures

  • MHB
  • Thread starter I like Serena
  • Start date
  • Tags
    Pictures
In summary, TikZ is a powerful tool for creating diagrams and graphs in LaTeX. Its syntax is designed to be intuitive and there are many resources available for learning how to use it. The TikZ Live Editor and toolbar button make it even easier to create and add TikZ pictures to posts on MHB.
  • #1
I like Serena
Homework Helper
MHB
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:
Code:
\begin{tikzpicture}
  %preamble \usepackage{pgfplots}
  \begin{axis}
    \addplot coordinates {(0,1) (0.5,1) (1,1.2)};
  \end{axis}
\end{tikzpicture}
\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 --, 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]
we'll get the same picture.

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}
the resulting document is:
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}
\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

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}
\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

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}
\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

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}
\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

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}
\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)
 
Last edited:
Physics news on Phys.org
  • #2


Hello there! I am a scientist and I am very excited about the introduction of TikZ pictures on this math site. It is great to see that MHB is the first site to allow members to post TikZ pictures.

For those who are not familiar with TikZ, it is a powerful tool for creating high-quality diagrams and figures in LaTeX. It has a dedicated syntax for every area of expertise, making it easy to create different types of diagrams. The best way to learn is to search for examples and modify them according to our own needs. TikZ is extensively documented on the web and the syntax is designed to be simple and intuitive.

To add a plot, we can use the tikzpicture environment and the pgfplots package. For example, we can plot a function using the coordinates (0,1), (0.5,1), and (1,1.2) by using the code \addplot coordinates {(0,1) (0.5,1) (1,1.2)} inside the axis environment.

There is also a TikZ Live Editor available on this site which allows us to create pictures on the fly while typing. It contains templates for commonly used pictures on MHB, making it easier for us to add them in our posts.

There is also a toolbar button with a TikZ icon which generates TIKZ tags for us. We can specify the preamble using %preamble directives, allowing us to include any package or library we need.

To give a few more examples, we can create a function graph, a state diagram, a hyperbola graph, and a histogram using TikZ. We can also use it to draw astronomical figures, as shown in the example.

I hope this helps in understanding how to use TikZ on this site. Happy drawing!
 

1. What is TikZ?

TikZ is a powerful package for creating high-quality graphics and diagrams within LaTeX documents. It is primarily used for creating technical and scientific illustrations, but can be used for any type of visual representation.

2. How do I install TikZ?

TikZ is automatically included in most LaTeX distributions, such as MiKTeX and TeX Live. If you do not already have it, you can download it from the CTAN website and install it manually.

3. What are the benefits of using TikZ for creating pictures?

TikZ allows for precise and customizable graphics, making it ideal for creating professional-looking diagrams and illustrations. It also integrates seamlessly with LaTeX, allowing for easy inclusion of graphics in documents.

4. Can I use TikZ even if I am not familiar with LaTeX?

While basic knowledge of LaTeX is helpful for using TikZ, it is not necessary. There are many resources available online for learning TikZ, and once you understand the basics, it is relatively easy to create complex diagrams.

5. What types of pictures can I create with TikZ?

TikZ can be used to create a wide range of pictures, including flowcharts, graphs, diagrams, and even 3D illustrations. The possibilities are nearly endless, and the final result is only limited by your imagination and skill level.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
969
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
887
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top