# Thread: TikZ pictures

1. 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!

To add for instance a plot, we can now use:
Latex Code:

\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 "http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf"]main TikZ package documentation[/URL]. Specifically for plots we may want to use the pgfplots package (as in the example above), which is documented in the .

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 [LATEXS] 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 Code:  \begin{tikzpicture} %preamble \usepackage{amsmath} %preamble \usetikzlibrary{arrows} ... \end{tikzpicture} the resulting document is: Latex Code:  \documentclass[border=10pt]{standalone} \usepackage{tikz} \usepackage{amsmath} \usetikzlibrary{arrows} \begin{document} \begin{tikzpicture} ... \end{tikzpicture} \end{document} Note that if we specify at least one preamble directive, the default preamble is discarded except for the tikz package. So we will have to specify all other needed packages or libraries as well. 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: we'll get the same picture. Example function graph Just to give a couple of additional examples: Latex 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} Note in particular that$\cos$takes degrees as a parameter instead of radians. Example state diagram Latex 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} Example graph of a hyperbola Latex 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=\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 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} Example astronomical drawing Latex 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} Happy TikZ drawing!! 2. 3. Congratulations MHB on achieving another milestone !!! 4. 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! 5. 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. 6. Thread Author Originally Posted by Opalg 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. 7. Originally Posted by I like Serena It should work for Safari now. It does! Excellent!! 8. 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. 9. Originally Posted by caffeinemachine 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! I have added this thread to our "Featured Threads" list, and put it in the top slot. 10. Very nice. Anybody interested for a contour integration tutorial? Should be simpler now. 11. Thread Author Since a common application on MHB will probably be to show the graph of some function, here's another example. Latex 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} Note in particular that$\cos\$ takes degrees as a parameter instead of radians.

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•