Welcome to our community

Be a part of something great, join today!

TikZ pictures

  • Thread starter
  • Admin
  • #1

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
9,308
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:
LaTeX:
\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 TikZ Live Editor 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:



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
LaTeX:
\begin{tikzpicture}
  %preamble \usepackage{amsmath}
  %preamble \usetikzlibrary{arrows}
  ...
\end{tikzpicture}
the resulting document is:
LaTeX:
\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:

LaTeX:
\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

LaTeX:
\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

LaTeX:
\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

LaTeX:
\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

LaTeX:
\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:

mathlearn

Active member
Jul 24, 2016
341
Congratulations MHB (Party)(Party)(Party)(Cake) on achieving another milestone !!!
 

caffeinemachine

Well-known member
MHB Math Scholar
Mar 10, 2012
835
This should be advertised on the first page of MHB and not just in a separate post. Anybody visiting the forum should see that he/she can post using tikz!
 

Opalg

MHB Oldtimer
Staff member
Feb 7, 2012
2,770
This is really impressive. The only disappointment is that the Safari browser does not seem to support it. On my iMac the TikZ images display correctly if I use Firefox, Opera or Chrome, but not with Safari. It looks as though I'll have to switch to Firefox as my default browser.
 
  • Thread starter
  • Admin
  • #5

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
9,308
This is really impressive. The only disappointment is that the Safari browser does not seem to support it. On my iMac the TikZ images display correctly if I use Firefox, Opera or Chrome, but not with Safari. It looks as though I'll have to switch to Firefox as my default browser.
Hi Opalg , thanks for the feedback!

Please try again. It should work for Safari now.
 

Opalg

MHB Oldtimer
Staff member
Feb 7, 2012
2,770

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
The feature is being improved, and so it is down for the time being. Sorry for any inconvenience.

edit: The changes are now complete and the feature is up again. (Yes)
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
This should be advertised on the first page of MHB and not just in a separate post. Anybody visiting the forum should see that he/she can post using tikz!
Good suggestion! (Yes)

I have added this thread to our "Featured Threads" list, and put it in the top slot. (Cool)
 

ZaidAlyafey

Well-known member
MHB Math Helper
Jan 17, 2013
1,667
Very nice. Anybody interested for a contour integration tutorial? Should be simpler now.
 

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
9,308
Since a common application on MHB will probably be to show the graph of some function, here's another example.

[LATEXS]\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}[/LATEXS]
\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.

I've added this example to the opening post as well.
 

mathlearn

Active member
Jul 24, 2016
341
So now which makes us to to decide between Desmos & TIkz for graphs. Anyway (Clapping) glad to have both of them!!

Soon we will see who brings the first problem with TIkz diagrams..
 

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
9,308
So now which makes us to to decide between Desmos & TIkz for graphs. Anyway (Clapping) glad to have both of them!!

Soon we will see who brings the first problem with TIkz diagrams..
Heh. Desmos gives us a bit more of a framework-out-of-the-box for regular graphs, but TikZ gives us much more fine grained control.
We'll have to wait and see!
 

mathlearn

Active member
Jul 24, 2016
341
(Sun) Is there some kind of visual editor you can use to draw Tikz images? or how should you be drawing one? , like without knowing the code, is it possible?
 

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
9,308
(Sun) Is there some kind of visual editor you can use to draw Tikz images? or how should you be drawing one? , like without knowing the code, is it possible?
Well, we can use the 'Preview' button to see what it looks like - and if it's actually correct.
Typically we'd start with an existing example and try to modify it for our personal needs.
As yet it doesn't work in the Live preview (yet).
Alternatively, we can download a latex program, such as MikTeX for Windows, that allows us to put a tikzpicture into a regular latex document. It does not have a visual editor though.
Maybe there's some visual WYSIWYG (what you see is what you get) editor on the web somewhere.
Or we may come up with some other preview utility that could in particular give more information about what's wrong.
 

Evgeny.Makarov

Well-known member
MHB Math Scholar
Jan 30, 2012
2,526
I belatedly thank the staff for adding this wonderful feature! I also have some comments and questions.

TikZ has a dedicated syntax for every area of expertise we want to make drawings for.
It's important to understand that there is the main TikZ package originally written by Till Tantau and many packages by other developers that build on it. For example, there are packages for drawing 3D graphics and plots. The main package has two (or three) layers. TikZ is the user-friendly layer that people use in their LaTeX documents to create graphics. It is built on top of system and basic layers, which are called PGF. TikZ is a recursive acronym "TikZ ist kein Zeichenprogramm", which translates to "TikZ is not a drawing program", and PGF stands for "portable graphics format".

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 manual that lists all options.
I agree that the documentation is vast, but I would first refer to the main TikZ package documentation (PDF). It has over 1100 pages, but, on the other hand, it is very detailed and user-friendly. The link in the quote above is to the documentation for the [m]pgfplots[/m] package, which is distinct from TikZ. It has over 500 pages.

To add for instance a plot, we can now use:
[LATEXS]
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {(0,1) (0.5,1) (1,1.2)};
\end{axis}
\end{tikzpicture}
[/LATEXS]
This code requires that
[LATEXS]
\usepackage{pgfplots}
[/LATEXS]
has been done besides loading TikZ. What other packages are loaded by MHB?

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 [M]--[/M], such as [M](0,0) -- (1,0)[/M].
We want an arrow? Then we use [M]->[/M] instead of [M]--[/M].
Not exactly. A line is drawn by
[LATEXS]
\draw (0,0) -- (1,0);
[/LATEXS]
and a line with an arrow is drawn by
[LATEXS]
\draw[->] (0,0) -- (1,0);
[/LATEXS]
The arrow by default is the same as in the command [m]\to[/m] ($\to$). I personally prefer loading a library [m]arrows[/m] using the command
[LATEXS]
\usetikzlibrary{arrows}
[/LATEXS]
and saying
[LATEXS]
\begin{tikzpicture}[>=stealth']
...
\end{tikzpicture}
[/LATEXS]
to delimit pictures. The latest version of TikZ has many more options for arrows, but this works.

And if we want to modify an attribute, for instance use a bent line, we add something in square brackets, such as [M][bend left][/M].
This option does not have effect on the [m]--[/m] operation, which draws straight lines. It requires either [m]to[/m] or [m]edge[/m] operation.
[LATEXS]
\draw[bend left] (0,0) to (5,5);
\draw[bend left] (0,0) edge (5,5);
[/LATEXS]

Example state diagram
This code requires
[LATEXS]
\usetikzlibrary{automata}
[/LATEXS]
What other libraries are loaded by MHB?

Unfortunately, TikZ does have a steep learning curve, but the results are pretty rewarding.
 

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
9,308
Thanks!

I've updated the opening post to properly refer to the manual, and fix the reference to an arrow.

The current document preamble is:
[LATEXS]
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
[/LATEXS]
which is sufficient for the current examples and some more.

We can add more packages on request, or if it makes sense we can introduce a custom command like:
[LATEXS]
\begin{tikzpicture}
% preamble \usetikzlibrary{arrows}
...
\end{tikzpicture}
[/LATEXS]
 
Last edited:

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
We've added the ability to click our TikZ images to get a popup window containing the code used to generate the image. This will make it easier to get the code for an image you might want to modify for your own use. Here is an example image to demonstrate the popup feature:

\begin{tikzpicture}
\draw[blue,thick] (0,0) -- (5.196,0);
\draw[blue,thick] (5.196,0) -- (5.196,3);
\draw[blue,thick] (5.196,3) -- (0,0);
\draw[gray,thin] (5.196,0.25) -- (4.946,0.25);
\draw[gray,thin] (4.946,0.25) -- (4.946,0);
\node[below=5pt of {(2.598,0)}] {\large $\dfrac{\sqrt{3}}{2}$};
\node[right=5pt of {(5.196,1.5)}] {\large $\dfrac{1}{2}$};
\node[above=5pt of {(2.598,1.5)}] {\large $1$};
\node[below=10pt of {(5.196,3)},xshift=-10pt] {\large $60^{\circ}$};
\node[right=18pt of {(0,0)},yshift=7pt] {\large $30^{\circ}$};
\end{tikzpicture}
 

Ackbach

Indicium Physicus
Staff member
Jan 26, 2012
4,198
We've added the ability to click our TikZ images to get a popup window containing the code used to generate the image. This will make it easier to get the code for an image you might want to modify for your own use. Here is an example image to demonstrate the popup feature:

\begin{tikzpicture}
\draw[blue,thick] (0,0) -- (5.196,0);
\draw[blue,thick] (5.196,0) -- (5.196,3);
\draw[blue,thick] (5.196,3) -- (0,0);
\draw[gray,thin] (5.196,0.25) -- (4.946,0.25);
\draw[gray,thin] (4.946,0.25) -- (4.946,0);
\node[below=5pt of {(2.598,0)}] {\large $\dfrac{\sqrt{3}}{2}$};
\node[right=5pt of {(5.196,1.5)}] {\large $\dfrac{1}{2}$};
\node[above=5pt of {(2.598,1.5)}] {\large $1$};
\node[below=10pt of {(5.196,3)},xshift=-10pt] {\large $60^{\circ}$};
\node[right=18pt of {(0,0)},yshift=7pt] {\large $30^{\circ}$};
\end{tikzpicture}
That's really first-class. Well done!
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
That's really first-class. Well done!
I'm glad you like it, Adrian! But...I've changed it...(Bandit)

Instead of creating a browser popup window, the TikZ images now act sort of like a spoiler button, which toggles the display of the TikZ code block. This is much more efficient and quick because it is already loaded. When you click the image to see the code, the block appears directly above the image. The initial height of the code block has a value set in the adminCP...right now I have it at 150px. You may resize the block manually up to its full size or down to one line, or you may click the block header to toggle the height between the full and initial heights.

The code is now displayed in a monospace font (using a stack of web-safe fonts), as programming code should be.

If you wish to copy the code to your clipboard, you may double-click the code area to quickly and easily select all of the code, just like our 3 primary code related BBCodes.

All elements have mouseover tooltips (which are fully phrased) explaining what actions can be taken and how to take them.

The CSS is now in a cached custom template and the associated javascript is loaded via a plugin hooked to the footer template to minimize the impact on page loading speed. (Yes)
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
While it won't likely come up often, we have added support for TikZ images in private messages, and visitor messages. (Nerd)
 

Opalg

MHB Oldtimer
Staff member
Feb 7, 2012
2,770
While it won't likely come up often, we have added support for TikZ images in private messages, and visitor messages. (Nerd)
But I can't get it to work in a POTW solution.
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
But I can't get it to work in a POTW solution.
I will see about getting the preview to work...everything else already does. :)
 

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
Okay, you should now find that our TikZ image feature is fully integrated with our POTW submission form. (Yes)
 

Klaas van Aarsen

MHB Seeker
Staff member
Mar 5, 2012
9,308
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:
LaTeX:
\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.

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
LaTeX:
\begin{tikzpicture}
  %preamble \usepackage{amsmath}
  %preamble \usetikzlibrary{arrows}
  ...
\end{tikzpicture}
the resulting document is:
LaTeX:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\usepackage{amsmath}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}
  ...
\end{tikzpicture}
\end{document}
Toolbar button

To make things easier, and more visible, we've added a new button on the toolbar with this icon:



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.

TikZ Live Editor

To make it easier to create a picture, we have the TikZ Live Editor in which pictures are created on the fly while you are typing.
It contains templates for picture types that are often used on MHB.
Copy and paste to get the result in a post.

Example function graph

Just to give a couple of additional examples:

LaTeX:
\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

LaTeX:
\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

LaTeX:
\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

LaTeX:
\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

LaTeX:
\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:

MarkFL

Administrator
Staff member
Feb 24, 2012
13,775
That is a very exciting development that will add a great deal of functionality! (Yes)