TikZ Some Examples
A lot of examples about Tikz [A PGF and TikZ examples gallery] can be found on the beautiful site of Kjell Magne Fauske :A PGF and TikZ examples gallery
Exemple 1 : RotateTriangle
RotateTriangle : [PDF] [TEX]This is an updated version more compact.
\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path (0,0) coordinate (A)
(-60:12cm) coordinate (B)
(240:12cm) coordinate (C);
\foreach \density in {20,30,...,160}
\draw[fill=MidnightBlue!\density]
(A) coordinate (X) --
(B) coordinate[pos=.15] (A) --
(C) coordinate[pos=.15] (B) --
(X) coordinate[pos=.15] (C);
\end{tikzpicture}
\end{document}
Exemple 2 : Annotated 3D box
Annotated 3D box : [PDF] [TEX]% Author : Alain Matthes
% Encoding : UTF8
% Engine : PDFLaTeX
\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage{fullpage}
\usepackage{fourier}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
\begin{comment}
Annotated 3D box
In the current version of PGF there is a small, undocumented library for 3D drawings. This example shows how you can use it to draw simple, but effective 3D-illustrations.
\end{comment}
\usetikzlibrary{calc,3d}
\begin{document}
\thispagestyle{empty}
\begin{center}
\begin{tikzpicture}[x = {(-0.5cm,-0.5cm)},
y = {(0.9659cm,-0.25882cm)},
z = {(0cm,1cm)},
scale = 2,
color = {lightgray}]
% style of faces
\tikzset{facestyle/.style={fill=lightgray,draw=red,very thin,line join=round}}
% face "back"
\begin{scope}[canvas is zy plane at x=0]
\path[facestyle,shade] (0,0) rectangle (2,4);
\end{scope}
% face "left"
\begin{scope}[canvas is zx plane at y=0]
\path[facestyle,shade] (0,0) rectangle (2,3);
\end{scope}
% face "front"
\begin{scope}[canvas is zy plane at x=3]
\path[facestyle] (0,0) rectangle (2,4);
\end{scope}
% face "right"
\begin{scope}[canvas is zx plane at y=4]
\path[facestyle] (0,0) rectangle (2,3);
\end{scope}
% face "up"
\draw[fill=brown,draw=red,opacity=.8,very thin,line join=round]
(0,0,2) --
(3,0,2) --
(3,{4*cos(15)},{4*sin(15)+2}) --
(0,{4*cos(15)},{4*sin(15)+2}) --cycle ;
% labels
\draw[very thin,black,line join=round]
(3,0,0) -- node [sloped,below] {$a$}
(3,4,0) -- node [right] {$b $}
(3,4,2) -- node [sloped,above] {$c=\sqrt{a^2+b^2}$}
(3,0,0);
\end{tikzpicture}
\end{center}
\end{document}
Exemple 3 : Product of matrices
Product of matrices : [PDF] [TEX]\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage[upright]{fourier}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,decorations.pathmorphing}
\usepackage{fullpage}
\begin{document}
To multiply two matrices, the first matrix must have the same number of rows (p) as the second matrix has columns (q). In other words, p of the first matrix must equal q of the second matrix. In general terms, a matrix C which is a product of two matrices, A and B, will have elements given by the following
\[c_{ij}=\sum_{k=1}^p a_{ik}b_{kj}\]
where $i$ = ith row and $j$ = jth column
\begin{tikzpicture}[>=latex]
% unit
\newcommand{\myunit}{1.2 cm}
% the styles
\tikzset{node style sp/.style={draw,circle,minimum size=\myunit}}
\tikzset{node style ge/.style={circle,minimum size=\myunit}}
\tikzset{arrow style mul/.style={draw,sloped,midway,fill=white}}
\tikzset{arrow style plus/.style={midway,sloped,fill=white}}
% defintion of matrices
\matrix (A) [matrix of math nodes,%
nodes = {node style ge},%
left delimiter = (,%
right delimiter = )] at (0,0)
{%
a_{11} &\ldots & a_{1k} & \ldots & a_{1p} \\
\vdots & \ddots & \vdots & \vdots & \vdots \\
\node[node style sp] {a_{i1}};& \ldots%
& \node[node style sp] {a_{ik}};%
& \ldots%
& \node[node style sp] {a_{ip}}; \\
\vdots & \vdots& \vdots & \ddots & \vdots \\
a_{n1}& \ldots & a_{nk} & \ldots & a_{np} \\
};
\node [draw,below] at (A.south) { $A$ : \textcolor{red}{$n$ rows} $p$ columns};
\matrix (B) [matrix of math nodes,%
nodes = {node style ge},%
left delimiter = (,%
right delimiter =)] at (7*\myunit,7*\myunit)
{%
b_{11} & \ldots& \node[node style sp] {b_{1j}};%
& \ldots & b_{1q} \\
\vdots& \ddots & \vdots & \vdots & \vdots \\
b_{k1} & \ldots& \node[node style sp] {b_{kj}};%
& \ldots & b_{kq} \\
\vdots& \vdots & \vdots & \ddots & \vdots \\
b_{p1} & \ldots& \node[node style sp] {b_{pj}};%
& \ldots & b_{pq} \\
};
\node [draw,above] at (B.north) { $B$ : $p$ rows \textcolor{red}{$q$ columns}};
% matrice résultat
\matrix (C) [matrix of math nodes,%
nodes = {node style ge},%
left delimiter = (,%
right delimiter = )] at (7*\myunit,0)
{%
c_{11} & \ldots& c_{1j} & \ldots & c_{1q} \\
\vdots& \ddots & \vdots & \vdots & \vdots \\
c_{i1}& \ldots & \node[node style sp,red] {c_{ij}};%
& \ldots & c_{iq} \\
\vdots& \vdots & \vdots & \ddots & \vdots \\
c_{n1}& \ldots & c_{nk} & \ldots & c_{nq} \\
};
\node [draw,below] at (C.south) {$ C=A\times B$ : \textcolor{red}{$n$ rows} \textcolor{red}{$q$ columns}};
% arrows
\draw[blue] (A-3-1.north) -- (C-3-3.north);
\draw[blue] (A-3-1.south) -- (C-3-3.south);
\draw[blue] (B-1-3.west) -- (C-3-3.west);
\draw[blue] (B-1-3.east) -- (C-3-3.east);
\draw[<->,red](A-3-1) to[in=180,out=90] (B-1-3)%
node[arrow style mul] (x) {$a_{i1}\times b_{1j}$};
\draw[<->,red](A-3-3) to[in=180,out=90](B-3-3)%
node[arrow style mul] (y) {$a_{ik}\times b_{kj}$};
\draw[<->,red](A-3-5) to[in=180,out=90](B-5-3)%
node[arrow style mul] (z) {$a_{ip}\times b_{pj}$};
\draw[red,->] (x) to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (y)%
to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (z);
%
% to (C-3-3.north west);
\draw[->,red,decorate,decoration=zigzag] (z) -- (C-3-3.north west);
\end{tikzpicture}
\end{document}
% encoding : utf8
% format : pdfLaTeX
% author : Alain Matthes
Exemple 3 : Lunula
Lunula : [PDF] [TEX]% 25/11/2008
% Alain Matthes
\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{through,calc}
\begin{document}
\begin{tikzpicture}[thick]
\path[draw] (-4,0) coordinate [label= left:$A$] (A)
-- ( 0,4) coordinate [label=above:$C$] (C)
-- ( 4,0) coordinate [label=right:$B$] (B)
-- cycle;
\draw [color=red] circle(4cm);
\draw[color=red,fill=black!20]
let \p1 = ($ (B) - (C) $) in
(A) arc (180:360:4cm) arc (-45:-135:{veclen(\x1,\y1)});
\foreach \point in {A,B,C} \fill [black] (\point) circle (2pt);
\end{tikzpicture}
\end{document}
Exemple 4 Schedule
Schedule : [PDF] [TEX]%!TEX encoding = UTF-8 Unicode
% Author: Alain Matthes
\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier} % Utilisation des polices texte
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{0pt}%
\usetikzlibrary[positioning]
\usetikzlibrary{patterns}
\usepackage[french]{babel} % styles français
\newcommand{\daywidth}{8 em}
% Style for Days
\tikzset{day/.style = {draw,
minimum height = 1cm,
minimum width = \daywidth,
fill = yellow!20,
anchor = south west}}
% style for title
\tikzset{title/.style = {draw,
minimum height = 1 cm,
minimum width = 1.5 cm,
fill = black,
text = white}}
% Style for hour
\tikzset{hour/.style = {draw,
minimum height = 1 cm,
minimum width = 1.5 cm,
fill = yellow!30,
anchor = north east}}
% Styles for events
\tikzset{hours/.style = {draw, text centered,
minimum width = \daywidth,
anchor = north west,
text width = 7 em}}
\tikzset{1hour/.style ={hours, minimum height = 1cm}}
\tikzset{2hours/.style ={hours, minimum height = 2cm}}
\tikzset{3hours/.style ={hours, minimum height = 3cm}}
%Style for type of sequence
\tikzset{6B/.style ={1hour, fill = green!20}}
\tikzset{2A/.style ={1hour, fill = red!20}}
\tikzset{TESSP/.style ={1hour, fill = blue!20}}
\tikzset{TES/.style ={1hour, fill = blue!10}}
\tikzset{PESSP/.style ={1hour, fill = magenta!50}}
\tikzset{Empty/.style ={1hour, fill = lightgray!30}}
\begin{document}
\begin{tikzpicture}[x=\daywidth, y=-1cm, node distance=0 cm,outer sep = 0pt]
% Positioning labels for days and hours
\node[day] (lundi) at (1,8) {\textbf{Lundi}};
\node[day] (mardi) [right = of lundi] {\textbf{Mardi}};
\node[day] (mercredi) [right = of mardi] {\textbf{Mercredi}};
\node[day] (jeudi) [right = of mercredi] {\textbf{Jeudi}};
\node[day] (vendredi) [right = of jeudi] {\textbf{Vendredi}};
\node[hour] (8-9) at (1,8) {\textbf{8-9}};
\node[hour] (9-10) [below = of 8-9 ] {\textbf{9-10}};
\node[hour] (10-11) [below = of 9-10] {\textbf{10-11}};
\node[hour] (11-12) [below = of 10-11] {\textbf{11-12}};
\node[hour] (12-13) [below = of 11-12] {\textbf{12-13}};
\node[hour] (13-14) [below = of 12-13] {\textbf{13-14}};
\node[title,above = of 8-9] {2009};
% lundi 1
\node[6B,2hours] at (1,8) {6B \textcolor{red}{(S 01)}};
\node[Empty] at (1,10) {};
\node[Empty] at (1,11) {};
\node[Empty] at (1,12) {};
\node[Empty] at (1,13) {};
% mardi 2
\node[2A] at (2,8) {2A \textcolor{red}{(B 21)}};
\node[Empty] at (2,9) {};
\node[Empty] at (2,10) {};
\node[TES] at (2,11) {TES \textcolor{red}{(B 12)}};
\node[Empty] at (2,12) {};
\node[PESSP] at (2,13) {PESSP \textcolor{red}{(B 21)}};
% mercredi 3
\node[TES,2hours]at (3,8) {TES \textcolor{red}{(B 13)}};
\node[2A] at (3,10) {2A gr B \textcolor{red}{(CB)}};
\node[2A] at (3,11) {2A gr A \textcolor{red}{(CB)}};
\node[Empty] at (3,12) {};
\node[Empty] at (3,13) {};
% jeudi 4
\node[2A] at (4,8) {2A \textcolor{red}{(B 21)}};
\node[TESSP] at (4,9) {TESSP \textcolor{red}{(B 12)}};
\node[6B] at (4,10) { 6B \textcolor{red}{(S 21)}};
\node[PESSP] at (4,11) { PESSP \textcolor{red}{(B 12)}};
\node[Empty] at (4,12) {};
\node[Empty] at (4,13) {};
% vendredi 5
\node[6B] at (5,8) {6B \textcolor{red}{(S 21)}};
\node[Empty] at (5,9) {};
\node[TESSP] at (5,10) {TESSP \textcolor{red}{(S 01)}};
\node[TES] at (5,11) {TES \textcolor{red}{(S 01)}};
\node[2A] at (5,12) {2A \textcolor{red}{(S 38)}};
\node[Empty] at (5,13) {};
\end{tikzpicture}
\end{document}
Exemple 5 : RotateSquare
RotateSquare : [PDF] [TEX]\documentclass{article}
\usepackage[usenames,dvipsnames,pdftex]{xcolor}
\usepackage{tikz,ifthen,fullpage}
\begin{document}
\thispagestyle{empty}
\newcounter{density}
\setcounter{density}{60}
\begin{tikzpicture}
\def\a{12}
\def\couleur{MidnightBlue}
\path (0 cm,0 cm) coordinate(A)
(\a cm,0 cm) coordinate(B)
(\a cm,\a cm) coordinate(C)
(0 cm,\a cm) coordinate(D);
\draw[fill=\couleur!\thedensity] (A)--(B)--(C)--(D)--cycle;
\foreach \i in {2,...,40}
{\ifthenelse{\isodd{\i}}{%
\def\couleur{MidnightBlue}}{%
\def\couleur{BurntOrange}}
\pgfmathsetcounter{density}{\thedensity+5}
\setcounter{density}{\thedensity}
\path coordinate(T) at (A);
\path (A)--(B) coordinate[pos=.9] (A)
--(C) coordinate[pos=.9] (B)
--(D) coordinate[pos=.9] (C)
--(T) coordinate[pos=.9] (D);
\draw[fill=\couleur!\thedensity] (A)--(B)--(C)--(D)--cycle;}
\end{tikzpicture}
\end{document}
Exemple 6 : Blackboard
Blackboard : [PDF] [TEX]\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,fullpage}
\usetikzlibrary{backgrounds}
\begin{document}
\thispagestyle{empty}
\tikzstyle{background rectangle}=[fill=black]
\begin{tikzpicture}[show background rectangle,line width = 1pt,color=white]
\node at (0,0) (O) {$\bullet$};
\draw (0,0) circle(5cm);
\draw (0,0) -- (-140:5) node(A) {$\bullet$};
\path[anchor=base] (O) to node[midway,sloped,above]{Radius} (A);
\draw[anchor=base] (A.center) -- +(130:5);
\draw[anchor=base] (A.center) -- +(-50:5) node[pos=.6,sloped,above] {Tangent};
\end{tikzpicture}
\end{document}
% Name : blackboard
% Encoding : utf8
% Engine : pdflatex
% Author: Alain Matthes
Exemple 7 : Suite
Suite récurrente : [PDF] [TEX]\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,fullpage}
\usetikzlibrary{arrows}
\begin{document}
\thispagestyle{empty}
\begin{figure}[htbp]
\centering
\newcounter{j}
\begin{tikzpicture}[scale=10,>=latex']
\draw[color=blue,samples at={0,0.01,...,1.07}] plot (\x,{cos(\x r)});
\draw[color=green](0,0)--(1,1);
\draw[->](0,0)--(0,1) node[above]{$y$};
\draw[->](0,0)--(1,0) node[right]{$x$};
\newcounter{cnt}
\newcommand{\x}{.2}
\foreach \i in {1,...,7}{%
\pgfmathcos{\x r}
\let\y\pgfmathresult
\draw[color=magenta](\x,\x)--(\x,\y)--(\y,\y);
\draw[color=orange,dotted,line width=0.8pt]%
(\x,\x)--(\x,0) node[below=8pt]{$u_\i$};
\pgfmathsetcounter{j}{\i+1}
\draw[color=blue,dotted,line width=0.8pt]%
(\x,\y)--(0,\y) node[left=8pt] {$u_\thej$};
\global\let\x\y}
\end{tikzpicture}
\caption{$f(x)=\cos (x)$}
\end{figure}
\end{document}
% Name : suite
% Encoding : utf8
% Engine : pdflatex
% Author: Alain Matthes
Exemple 8 : Ulam's Spiral
RotateSquare : [PDF] [TEX]\documentclass{article}
\RequirePackage{ifthen,fullpage}
\RequirePackage{tikz}
\newcounter{cmpt}
\newcounter{next}
\newcounter{end}
\makeatletter
\newcounter{prim@r}
\newcounter{prim@a}
\newcounter{prim@n}
\newcounter{prim@d}
\newcounter{prim@b}
\newboolean{tkzaIsAPrim}
%<–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––>
% test if #1 is a prim number result set boolean :tkzaIsAPrim
%<–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––>
\newcommand*\primality[1]{%
\setboolean{tkzaIsAPrim}{true}%
\setcounter{prim@a}{#1}%
\ifthenelse{#1 = 0}{%
\setboolean{tkzaIsAPrim}{false}}{%
\ifthenelse{#1 = 1}{%
\setboolean{tkzaIsAPrim}{false}}{%
\ifthenelse{#1 = 2}{}{%
\ifthenelse{#1 = 3}{}{%
\ifthenelse{\isodd{#1}}{%
\setcounter{prim@d}{3}%
\whiledo{\value{prim@a} > \value{prim@d}}{%
\pgfmathsetcounter{prim@r}{mod(\theprim@a,\theprim@d)}%
\ifthenelse{\value{prim@r} = 0}{%
\setboolean{tkzaIsAPrim}{false}%
\setcounter{prim@d}{\theprim@a}}{%
\pgfmathaddtocounter{prim@d}{2}
}%
}% fin du whiledo
}{%
\setboolean{tkzaIsAPrim}{false}%
}%
}%
}%
}%
}%
}%
\makeatother
\newcommand*\ballcolor{%
\stepcounter{cmpt}
\primality{\value{cmpt}}
\ifthenelse{\boolean{tkzaIsAPrim}}{%
\colorlet{colornb}{blue!50}}{%
\colorlet{colornb}{orange!60}}%
}
\newcommand*{\ulam}[2]{%
\setcounter{cmpt}{#1}
\addtocounter{cmpt}{-1}
\setcounter{next}{0}
\setcounter{end}{#2}
\ballcolor
\node(place) at (0,0) {$\mathbf\thecmpt$};
\whiledo{\value{cmpt}<\value{end}}%
{
\foreach \i in {0,...,\thenext}
{\ballcolor
\node[above of=place] (place) {$\mathbf\thecmpt$};
}
\foreach \i in {0,...,\thenext}
{\ballcolor
\node[right of=place] (place) {$\mathbf\thecmpt$};
}
\stepcounter{next}
\foreach \i in {0,...,\thenext}
{\ballcolor
\node[below of=place] (place) {$\mathbf\thecmpt$};
}
\foreach \i in {0,...,\thenext}
{\ballcolor
\node[left of=place] (place) {$\mathbf\thecmpt$};
}
\stepcounter{next}
}
\addtocounter{next}{-1}
\foreach \i in {0,...,\thenext}
{\ballcolor
\node[above of=place] (place) {$\mathbf\thecmpt$};}
}
\begin{document}
\begin{center}
\begin{tikzpicture}
\tikzstyle{every node}=[draw,%
rectangle,%
shade,%
minimum size = 1.6cm,%
ball color = colornb,%
node distance = 1.6cm]
\ulam{41}{113}
\end{tikzpicture}
\end{center}
\end{document}








