Some notes on using TikZ

Created: November 13, 2020   Last Modified: November 20, 2020   Category: tex   Print this pageBack to Home

Summary

This post contains some notes I don’t want to forget when using TikZ. I will keep updating its contents as time goes by.

Resources

Scaling pictures including nodes

...
\begin{tikzpicture}[scale=0.5, every node/.style={transform shape}]
	% draw your picture here
\end{tikzpicture}
...

Aligning inline TikZ picture

Simply use [anchor=base, baseline] option, for example,

...
\usetikzlibrary{shapes}
...
A rectangle looks like \tikz [anchor=base, baseline, inner sep=0] \node[draw, rectangle, minimum height=2mm] {};
...

More details can be found here.

Define and reuse TikZ pictures

More details can be found here.

...
\tikzset{
	rec1/.pic={
		% draw your rec1 picture here
	}, 
	rec2/.pic={
		% draw your rec2 picture here
	}
	% and so on
}
...
% now we can reuse pictures 
\begin{tikzpicture}
	\pic[color=red]{rec1};
\end{tikzpicture}
...

A simple tree example

I got it from here. Just put here to remember how to draw a tree with TikZ. More examples can be found here.

% A simple Tree
% Author: Stefan Kottwitz
% https://www.packtpub.com/hardware-and-creative/latex-cookbook
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[sibling distance=10em,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=blue!20}]]
  \node {Formulas}
    child { node {single-line} }
    child { node {multi-line}
      child { node {aligned at}
        child { node {relation sign} }
        child { node {several places} }
        child { node {center} } }
      child { node {first left,\\centered,\\last right} } };
\end{tikzpicture}
\end{document}

New line within a node

Simply specify that the node is a text node, like

...
node[text width=1cm,align=center] {0,1,2\\3,4,5};
...

TikZ matrix for drawing tables

An example taken from here.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes,row sep=-\pgflinewidth,column 2/.style={nodes={rectangle,draw,minimum width=3em}}]
{
0   & 6 \\   1   & 3 \\   };
\end{tikzpicture}
\end{document}

Valid CSS! Valid HTML 4.01 Transitional Back to top
Powered by Jekyll
The content on this site is shared under a CC BY-SA 4.0 license, unless otherwise noted Last Modified: November 20, 2020