What Can You Create with TikZ Code Examples?

  • Context: MHB 
  • Thread starter Thread starter MarkFL
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on sharing TikZ code examples for creating various graphical representations, including a 3D Sudoku cube, a belt and pulley system, and geometric shapes like the Penrose triangle. Users are encouraged to contribute templates while being mindful of server load. The examples provided utilize specific TikZ libraries and commands, showcasing the versatility of TikZ in LaTeX for both educational and illustrative purposes.

PREREQUISITES
  • Familiarity with LaTeX typesetting
  • Understanding of TikZ library commands
  • Basic knowledge of geometric shapes and their properties
  • Experience with graphical representation in academic or technical documents
NEXT STEPS
  • Explore TikZ documentation for advanced drawing techniques
  • Learn about TikZ libraries such as 'shapes' and 'positioning'
  • Investigate the use of TikZ for creating complex diagrams in scientific papers
  • Practice creating custom TikZ styles for consistent graphic design
USEFUL FOR

Graphic designers, educators, researchers, and anyone interested in using LaTeX for creating high-quality illustrations and diagrams.

MarkFL
Gold Member
MHB
Messages
13,284
Reaction score
12
I thought it would be nice to begin a thread where we can post examples of TikZ code that the community can use as templates or jumping off points for their own images. I would recommend not posting a huge number of images in the same post, so we don't strain the rendering server (maintained by I like Serena), or MHB's server.

I'll begin with a Sudoku 3D cube:

\begin{tikzpicture}[every node/.style={minimum size=1cm},on grid]
%preamble \usetikzlibrary{positioning}
\begin{scope}[every node/.append style={yslant=-0.5},yslant=-0.5]
\shade[right color=gray!10, left color=black!50] (0,0) rectangle +(3,3);
\node at (0.5,2.5) {9};
\node at (1.5,2.5) {7};
\node at (2.5,2.5) {1};
\node at (0.5,1.5) {2};
\node at (1.5,1.5) {4};
\node at (2.5,1.5) {8};
\node at (0.5,0.5) {5};
\node at (1.5,0.5) {3};
\node at (2.5,0.5) {6};
\draw (0,0) grid (3,3);
\end{scope}
\begin{scope}[every node/.append style={yslant=0.5},yslant=0.5]
\shade[right color=gray!70,left color=gray!10] (3,-3) rectangle +(3,3);
\node at (3.5,-0.5) {3};
\node at (4.5,-0.5) {9};
\node at (5.5,-0.5) {7};
\node at (3.5,-1.5) {6};
\node at (4.5,-1.5) {1};
\node at (5.5,-1.5) {5};
\node at (3.5,-2.5) {8};
\node at (4.5,-2.5) {2};
\node at (5.5,-2.5) {4};
\draw (3,-3) grid (6,0);
\end{scope}
\begin{scope}[every node/.append style={
yslant=0.5,xslant=-1},yslant=0.5,xslant=-1
]
\shade[bottom color=gray!10, top color=black!80] (6,3) rectangle +(-3,-3);
\node at (3.5,2.5) {1};
\node at (3.5,1.5) {4};
\node at (3.5,0.5) {7};
\node at (4.5,2.5) {5};
\node at (4.5,1.5) {6};
\node at (4.5,0.5) {8};
\node at (5.5,2.5) {2};
\node at (5.5,1.5) {3};
\node at (5.5,0.5) {9};
\draw (3,0) grid (6,3);
\end{scope}
\end{tikzpicture}

And a belt and pulley system:

\begin{tikzpicture}

% Definitions
\pgfmathsetmacro{\b}{75}
\pgfmathsetmacro{\a}{15}
\pgfmathsetmacro{\R}{2}
\pgfmathsetmacro{\r}{1}
\pgfmathsetmacro{\P}{\R*tan(\b)}
\pgfmathsetmacro{\Q}{\R/cos(\b)}
\pgfmathsetmacro{\p}{\r/tan(\a)}
\pgfmathsetmacro{\q}{\r/sin(\a)}

% Pulleys

% Big pulley
\draw (0,0) circle (\R) ;
\fill[left color=gray!80, right color=gray!60, middle
color=white] (0,0) circle (\R) ;
\draw[thick, white] (0,0) circle (.8*\R);
\shade[ball color=white] (0,0) circle (.3) node[left,xshift=-5] {$P$};

% small pulley
\draw (\Q+\q-.3, 0) circle (\r);
\fill[left color=gray!80, right color=gray!60, middle
color=white] (\Q+\q-.3, 0) circle (\r) ;
\draw[thick, white] (\Q+\q-.3,0) circle (.8*\r);
\shade[ball color=white] (\Q+\q-.3,0) circle (.15)
node[right, xshift=2] {$Q$};

% belt and point labels
\begin{scope}[ultra thick]
\draw (\b:\R) arc (\b:360-\b:\R) ;
\draw (\b:\R) -- ( \P, 0 );
\draw (-\b:\R) -- ( \P, 0 );
\draw (\Q-.3,0) -- + (\a:\p) arc (105:-105:\r) ;
\draw (\Q-.3,0) -- + (-\a:\p);
%\draw (\b:\R) arc (\b:360-\b:\r) ;
\end{scope}

\draw (0,0) -- (\b:\R) node[midway, above,sloped] {$R$} node[above] {$A$};
\draw (-\b:\R)--(0,0) ;
\draw (\Q+\q-.3,0) -- +(105:\r) node[midway,above, sloped] {$r$}
node[above] {$E$};
\draw (\Q+\q-.3,0) -- +(-105:\r) node[below] {$D$};
\node[below] at (-\b:\R) {$B$};
\node[below] at (\Q-.3,0) {$C$};

% center line
\draw[dash pattern=on5pt off3pt] (0,0) -- (\Q+\q-.3,0);

% angle label
\node[fill=white] at (0.73*\Q, 0) {$\theta$} ;
\draw (\Q-1.8,0) arc (180:195:1.5);
\draw (\Q-1.8,0) arc (180:165:1.5);
\end{tikzpicture}
 
Last edited by a moderator:
Physics news on Phys.org
Escher Brick:

\begin{tikzpicture}[scale=4.5, line join=bevel]
% \a and \b are two macros defining characteristic
% dimensions of the impossible brick.
\pgfmathsetmacro{\a}{0.18}
\pgfmathsetmacro{\b}{1.37}

\tikzset{%
apply style/.code={\tikzset{#1}},
brick_edges/.style={thick,draw=black},
face_coloura/.style={fill=gray!50},
face_colourb/.style={fill=gray!25},
face_colourc/.style={fill=gray!90},
}

\foreach \theta/\v/\facestyleone/\facestyletwo in {%
0/0/{brick_edges,face_coloura}/{brick_edges,face_colourc},
180/-\a/{brick_edges,face_colourb}/{brick_edges,face_colourc}
}{
\begin{scope}[rotate=\theta,shift={(\v,0)}]
\draw[apply style/.expand once=\facestyleone]
({-.5*\b},{1.5*\a}) --
++(\b,0) --
++(-\a,-\a) --
++({-\b+2*\a},0) --
++(0,-{2*\a}) --
++(\b,0) --
++(-\a,-\a) --
++(-\b,0) --
cycle;
\draw[apply style/.expand once=\facestyletwo]
({.5*\b},{1.5*\a}) --
++(0,{-2*\a}) --
++(-\a,0) --
++(0,\a) --
cycle;
\end{scope}
}
\end{tikzpicture}

Penrose Triangle:

\begin{tikzpicture}[scale=1, line join=bevel]

% \a and \b are two macros defining characteristic
% dimensions of the Penrose triangle.
\pgfmathsetmacro{\a}{2.5}
\pgfmathsetmacro{\b}{0.9}

\tikzset{%
apply style/.code = {\tikzset{#1}},
triangle_edges/.style = {thick,draw=black}
}

\foreach \theta/\facestyle in {%
0/{triangle_edges, fill = gray!50},
120/{triangle_edges, fill = gray!25},
240/{triangle_edges, fill = gray!90}%
}{
\begin{scope}[rotate=\theta]
\draw[apply style/.expand once=\facestyle]
({-sqrt(3)/2*\a},{-0.5*\a}) --
++(-\b,0) --
({0.5*\b},{\a+3*sqrt(3)/2*\b}) -- % higher point
({sqrt(3)/2*\a+2.5*\b},{-.5*\a-sqrt(3)/2*\b}) -- % rightmost point
++({-.5*\b},-{sqrt(3)/2*\b}) -- % lower point
({0.5*\b},{\a+sqrt(3)/2*\b}) --
cycle;
\end{scope}
}
\end{tikzpicture}
 
The electric dipole moment (p) in the water molecule

\begin{tikzpicture}[>=latex,scale=1.3]
\shade[ball color=gray!10!] (0,0) coordinate(Hp) circle (.9) ;
\shade[ball color=gray!10!] (2,-1.53) coordinate(O) circle (1.62) ;
\shade[ball color=gray!10!] (4,0) coordinate(Hm) circle (.9) ;
\draw[thick,dashed] (0,0) -- (2,-1.53) -- (4,0) ;
\draw[thick] (2,.2) -- (2,1.5) node
{$\mathbf{p}$} ;
\draw (2.48,-1.2) arc (33:142:.6) ;
\draw (2,-.95) node[above]{$105^{\circ}$} ;
\draw (0,.2) node
{H$^+$} ;
\draw (4,.2) node
{H$^-$} ;
\draw (2,-1.63) node[below]{O$^{2-}$} ;
\foreach \point in {O,Hp,Hm}
\fill [black] (\point) circle (2pt) ;
\end{tikzpicture}​
 
Parallel lines and related angles

\begin{tikzpicture}
\draw[fill=yellow] (0,0) -- (60:.75cm) arc (60:180:.75cm);
\draw(120:0.4cm) node {$\alpha$};

\draw[fill=green!30] (0,0) -- (right:.75cm) arc (0:60:.75cm);
\draw(30:0.5cm) node {$\beta$};

\begin{scope}[shift={(60:2cm)}]
\draw[fill=green!30] (0,0) -- (180:.75cm) arc (180:240:.75cm);
\draw (30:-0.5cm) node {$\gamma$};

\draw[fill=yellow] (0,0) -- (240:.75cm) arc (240:360:.75cm);
\draw (-60:0.4cm) node {$\delta$};
\end{scope}

\begin{scope}[thick]
\draw (60:-1cm) node[fill=white] {$E$} -- (60:3cm) node[fill=white] {$F$};
\draw[red] (-2,0) node
{$A$} -- (3,0)
node
{$B$};
\draw[blue,shift={(60:2cm)}] (-3,0) node
{$C$} -- (2,0)
node
{$D$};
\draw[shift={(60:1cm)},xshift=4cm]
node [right,text width=6cm,rounded corners,fill=red!20,inner sep=1ex]
{
When we assume that $\color{red}AB$ and $\color{blue}CD$ are
parallel, I.\,e., ${\color{red}AB} \mathbin{\|} \color{blue}CD$,
then $\alpha = \delta$ and $\beta = \gamma$.
};
\end{scope}
\end{tikzpicture}

Intersection of

\begin{tikzpicture}
%preamble \usetikzlibrary{arrows}
[
scale=5,
axis/.style={very thick, ->, >=stealth'},
important line/.style={thick},
dashed line/.style={dashed, thin},
pile/.style={thick, ->, >=stealth', shorten <=2pt, shorten
>=2pt},
every node/.style={color=black}
]
% axis
\draw[axis] (-0.1,0) -- (1.1,0) node(xline)

{$G\uparrow/T\downarrow$};
\draw[axis] (0,-0.1) -- (0,1.1) node(yline)[above] {$E$};
% Lines
\draw[important line] (.15,.15) coordinate (A) -- (.85,.85)
coordinate (B) node[right, text width=5em] {$Y^O$};
\draw[important line] (.15,.85) coordinate (C) -- (.85,.15)
coordinate (D) node[right, text width=5em] {$\mathit{NX}=x$};
% Intersection of lines
\fill[red] (intersection cs:
first line={(A) -- (B)},
second line={(C) -- (D)}) coordinate (E) circle (.4pt)
node[above,] {$A$};
% The E point is placed more or less randomly
\fill[red] (E) +(-.075cm,-.2cm) coordinate (out) circle (.4pt)
node[below left] {$B$};
% Line connecting out and ext balances
\draw [pile] (out) -- (intersection of A--B and out--[shift={(0:1pt)}]out)
coordinate (extbal);
\fill[red] (extbal) circle (.4pt) node[above] {$C$};
% line connecting out and int balances
\draw [pile] (out) -- (intersection of C--D and out--[shift={(0:1pt)}]out)
coordinate (intbal);
\fill[red] (intbal) circle (.4pt) node[above] {$D$};
% line between out og all balanced out :)
\draw[pile] (out) -- (E);
\end{tikzpicture}

Intersecting lines

\begin{tikzpicture}[scale=1.5]
% Draw axes
\draw [<->,thick] (0,2) node (yaxis) [above] {$y$}
|- (3,0) node (xaxis)
{$x$};
% Draw two intersecting lines
\draw (0,0) coordinate (a_1) -- (2,1.8) coordinate (a_2);
\draw (0,1.5) coordinate (b_1) -- (2.5,0) coordinate (b_2);
% Calculate the intersection of the lines a_1 -- a_2 and b_1 -- b_2
% and store the coordinate in c.
\coordinate (c) at (intersection of a_1--a_2 and b_1--b_2);
% Draw lines indicating intersection with y and x axis. Here we use
% the perpendicular coordinate system
\draw[dashed] (yaxis |- c) node
{$y'$}
-| (xaxis -| c) node[below] {$x'$};
% Draw a dot to indicate intersection point
\fill[red] (c) circle (2pt);
\end{tikzpicture}​
 
Last edited by a moderator:

Similar threads

Replies
2
Views
2K
  • · Replies 0 ·
Replies
0
Views
6K
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 27 ·
Replies
27
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • Poll Poll
  • · Replies 1 ·
Replies
1
Views
2K