Tikzpicture: what you should know?

  • This post addresses questions like what is tikzpicture, how it can be used, how we add a caption to it, what alignment options we have and how to scale it and change its size!

Before going further with the post subject, let me share with you the TikZ meme code 😄😄😄

tikzpicture latex tikz

What is TikZpicture? 

  • TikZpicture is a LaTeX environment which regroups drawing commands from TikZ or different packages built on the top of PGF/TikZ package such as Pgfplots, CircuiTikZ, PGF-Pie and smartdiagram.

TikZpicture environment in LaTeX

 \documentclass{article}

% Loading TikZ Package
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[options]

	% Illustration code here

\end{tikzpicture}

\end{document}

All drawing commands have to be inside the tikzpicture environment. TikZ style commands like \tikzset and \tikzstyle could be added outside the tikzpicture environment. However, it is advisable to put \tikzstyle inside it to move the illustration without missing any defined styles. 

Graphic options are put between brackets as shown above and they are local to the tikzpicture to which they apply. Before the options, animation commands can be specified and we talk about it in a different post.

The following code presents an alternative to tikzpicture environment:

 \documentclass{article}

% Loading TikZ Package
\usepackage{tikz}

\begin{document}

\tikz[options]
{
	% Illustration code here
}

\end{document}

How to center a TikZpicture? 

A tikzpicture illustration can be centered by putting it inside a center environment as follows: 

 \documentclass{article}

% Loading TikZ Package
\usepackage{lipsum}

\begin{document}

\lipsum[2]

\begin{center}
\begin{tikzpicture}
	\draw[fill=orange] (0,0) circle(1);
\end{tikzpicture}
\end{center}

\lipsum[3]

\end{document}

Centerd TikZ illustration

Add a caption to a TikZpicture

To add a caption to a TikZpicture illustration, we can put it inside a figure environment and use \caption before or after the illustration depending on the desired position.

 \documentclass{article}

% Loading TikZ Package
\usepackage{lipsum}

\begin{document}

\lipsum[2]

\begin{figure}
\centering
	\begin{tikzpicture}
		\draw[fill=teal] (0,0) rectangle (2,1);
	\end{tikzpicture}
\caption{Rectangle in TikZ}
\end{figure}

\lipsum[3]

\end{document}

Tikzpicture title caption figure

How to scale a TikZpicture? 

There are several methods that can be used to scale a tikzpicture, check this interesting post about it in tex.stackexchange.com. For me, to resize or scale a tikz illustration, I prefer to put inside the resizebox as follows:    

 

\resizebox{Horizontal length}{vertical length}
{
	\begin{tikzpicture}
		% Illustration code
	\end{tikzpicture}
}

Providing horizontal and vertical lengths will force the tikz illustration to respect these sizes. However, specifying one of these parameters (horizontal length or vertivcal length) and adding an exclamation point ! to the other will preserve the aspect ration of the TikZpicture. 

 

\resizebox{4cm}{!}
{
	\begin{tikzpicture}
		% Illustration code
	\end{tikzpicture}
}

The above code will force the image width to 4cm and the height is computed to preserve the aspect ratio of the original image. 

  • We reached the end of this tutorial, and we will add more details in near future. If you have any questions or remarks, leave me a comment below or reach me via e-mail at admin@latexdraw.com, I will be happy to hear from you!