TikZ: A better way to draw scientific and technical diagrams

In preparation of my last paper I needed to draw a detailed complex diagram. Since I use LaTeX and source control (git) I did not want to use Adobe Illustrator (which I don't have a license), Inkscape (great package, though still very limited for complex diagrams), and other common ones such as Corel. Needless to say that all their formats are binary and not source-control friendly.

After a quick search I found TikZ. It had a wow effect on me right away. In TikZ, you can use the full power of LaTeX (as in typesetting), and in addition enhance your diagrams with the power of programming. You can create this kind of diagrams in a minute:

Tikz fun diagram

What you get is a vector-based image that can be used in your publications (no denting when you scale it), and also track the changes in a version-control system (like when you added a part and then removed it, and you want it back). Also, you can maximize it to your needs and get such diagrams as well:

Tikz diagram

Also, the synthax is pretty easy to learn too (see below). So, give it a try!

\documentclass[]{article} 
\usepackage[utf8]{inputenc} 
\usepackage{fullpage} 
\usepackage{tikz} 
%define some cool colors
\xdefinecolor{c1}{HTML}{D4ACEE} 
\xdefinecolor{c2}{HTML}{EEc6AC} 
\xdefinecolor{c3}{HTML}{EDEEAC} 
\xdefinecolor{c4}{HTML}{B3EEAC}
\begin{document} 
\thispagestyle{empty} 
\begin{center}
%here starts the tikz image
\begin{tikzpicture}
% we define a scope to act as a group property. The subgroups will inherit what you define here
\begin{scope}[ultra thin]
% we create a for..next loop in Tikz, define xss to be the counter
\foreach \xss in {0,.1,...,8.1} {
% then start drawing lines.
\draw[color=c1] (\xss,0) -- (0,8-\xss); 
\draw[color=c2] (-\xss,0) -- (0,8-\xss); 
\draw[color=c3] (\xss,0) -- (0,\xss-8); 
\draw[color=c4] (-\xss,0) -- (0,\xss-8);
}
\end{scope}
% let's have some axis as well
\begin{scope}
[color=black!50!white] \draw[<->] (-8.5,0) -- (8.5,0) 
node[right] {$x$}; 
\draw[<->] (0,-8.5) -- (0,8.5) node[above] {$y$}; 
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}

The code to create the diagram of flag in the flow is as follow:

\begin{tikzpicture}     [scale=.25]  
\usetikzlibrary{shadows,decorations.pathreplacing,shapes,calc,decorations.markings,patterns,arrows}  
\tikzstyle{ann} = [ rounded corners=1pt,font=\tiny,inner sep=1pt]   
\tikzstyle{ann2} = [ rounded corners=1pt,fill=white, font=\tiny,inner sep=1pt]      
\tikzstyle{ann3} = [fill=white, font=\tiny,inner sep=1pt] 
% %grids
%   \begin{scope}[opacity=0.3]
% \draw[black] (-10,-2) grid (5+28,20);
% \draw[<->,red] (0,20) -- (0,0);
% \draw[<->,red] (-7,0) -- (28,0);
%   \foreach \i in {-1,0,...,16}{
%   \node[right,red] at (5+25,\i) {\tiny \i};
%   }
%   \foreach \i in {-10,-9,...,30}{
%   \node[red, rotate=90] at (\i,2) {\tiny \i};
%   }
% \end{scope}
\begin{scope}       []  
\draw[stealth'-stealth',ultra thin] (26.5,16) -- (26.5,6);          
\draw[ultra thin] (26.2,6) -- (26.8,6);     
\draw[ultra thin] (26.2,16) -- (26.8,16);       
\node[ann2] at (26.5, 11) {$L_{y}$} ;   
\draw[stealth'-stealth',ultra thin] (-5,17) -- (25,17);
\draw[ultra thin] (-5,17.3) -- (-5,16.7);       
\draw[ultra thin] (25,17.3) -- (25,16.7);       
\node[ann2] at (12.5, 17) {$L_{x}$} ;
\draw[ thick] (-5,6) -- (25,6);         
\draw[ thick] (-5,16) -- (25,16);       
\draw[dashed, gray] (-5,6) -- (-5,16);      
\draw[dashed, gray] (25,6) -- (25,16);      
\filldraw[fill=black!10, draw=none] (0,11) circle (1.5);    
\begin{scope}[yshift=11cm]
%\filldraw[color=black!20](8,0)circle(.02cm);   
\draw[draw=black] (30:1.5) -- (8,.35) arc (90:-90:.35) -- (-30:1.5) ;
\end{scope}
%\draw[fill=gray!20, densely dashed] (-1.5,11) arc (180:30:1.5); 
\draw[fill=gray!20, densely dashed,draw=red!50!black,semithick] (1.25,11.75) arc (30:360-30:1.5); 
\draw[-o, ultra thin, gray](5,8.5) node[below, ann, black] {$\Gamma_{FSI}$} to (6,10.75);
\draw[-o, ultra thin, gray](1,8.5) node[below, ann, black] {$\Gamma_{H}$} to (0,9.75);
\draw[ultra thin, black!70] (.6,7.5) -- (.6,7.3) -- (5,7.3) -- (5,7.5);
\draw[ultra thin, black!70] (5.6/2,7.3) -- (5.6/2,7) -- (9,7) -- (9,7.5) node [above, yshift=-2.7, xshift=-1.3,color=black] {{\tiny $\Gamma_{CD}$}};        
\node[ann] at (0.15,11) {$\Omega_{H}$};
\node[ann2] at (20,16) {...};       
\node[ann2] at (20,6) {...};        
\node[ann] at (18,11) {$\Omega_{F}$};       
\node[ann] at (3,11) {$\Omega_{T}$};
\foreach \yss in {6,7,...,16} {             
\draw [thin,-latex', black!80] (-6,\yss) -- (-5.1,\yss); } 
\node [ann] at (-7.3,11.5) {$c_{0}$};
\node [ann] at (-7.3,10.5) {$u_{0}$};
\begin{scope}[xshift=23cm,yshift=6.6cm,scale=.7, black!70]
\draw(-0.75,-0.35)node[ ann]{$z$};
\draw[<->, ultra thin] (1.5,0) node[right, ann]{$x$}-|(0,1.5) node[above, ann]{$y$};
\filldraw[fill=white](0,0)circle(.3cm);
\filldraw[fill=black](0,0)circle(.05cm);
\end{scope}
\begin{scope}[yshift=11cm]
\fill(8.35,0) circle (3pt);
\draw[ultra thin, color=black!70] (-1.5,3.3) -- (-1.5,3.5) -- (1.4,3.5) -- (1.4,3.3) node [above, xshift=-10pt, color=black!70] {{\tiny Head}};     
\draw[ultra thin, color=black!70] (1.6,3.3) -- (1.6,3.5) -- (8.5,3.5) -- (8.5,3.3) node [above, xshift=-25pt, color=black!70] {{\tiny Tail}};       
\draw[ultra thin, color=black!70](8.8,0) -- (9,0) -- (11,2) node[above , color=black!70] {{\tiny Tip}};
\end{scope}
\begin{scope}[xshift=5cm, yshift=2cm]
\filldraw[fill=black!10, draw=none] (-4,3.1) rectangle (12,-2.1);   
\node[ann3] at (-5, 2.5) {\large b} ;
\node[ann3] at (-9, 12.8) {\large a} ;
\draw[ultra thin,] (-1.5,0) -- (8.3,0);     
\draw[ultra thin,] (0,1.5) -- (0,-1.5);
\draw[] (-0.1,.75) -- (0.1,.75);
\draw[] (-0.1,-.75) -- (0.1,-.75);
\draw[ultra thin,] (0,1.5) -- (0,-1.5);
\draw[ultra thin,] (8,.3) -- (8,-.3);       
\draw[](0,0)circle(1.5cm);
\draw[](8,0)circle(.3cm);
\draw[ultra thin,] (0,.75) -- (8,.3);
\draw[ultra thin,] (0,-.75) -- (8,-.3);
\fill(8,0) circle (.02cm);
\fill(0,0) circle (.02cm);
\draw[stealth'-stealth',ultra thin] (-3,1.5) -- (-3,-1.5);          
\draw[ultra thin] (-2.7,1.5) -- (-3.3,1.5);     
\draw[ultra thin] (-2.7,-1.5) -- (-3.3,-1.5);           
\node[ann3] at (-3, 0) {$d$} ;
% d/4
\draw[stealth' reversed-stealth' reversed,ultra thin] (-2,1.1) -- (-2,-.30);        
\draw[ultra thin] (-1.7,.75) -- (-2.3,.75);     
\draw[ultra thin] (-1.7,0) -- (-2.3,0);         
\draw[densely dotted, gray](-2,1.1) -- (-2,1.2) -- (-2.8, 2.3);
\node[ann3] at (-2, 2.5) {$d/4$} ;
\draw[stealth' reversed-stealth' reversed,ultra thin] (9,.7) -- (9,-.7);        
\draw[ultra thin] (8.9,.3) -- (9.1,.3);     
\draw[ultra thin] (8.9,-.3) -- (9.1,-.3);           
\node[ann3] at (10.5, 0) {$d/5$};
%zicks
% \draw[ultra thin] (-.10, 3.6/8) -- (.1,2.4/8);    
% \draw[ultra thin] (-.10, 9.6/8) -- (.1,8.4/8);        
% for L
\draw[stealth'-stealth', ultra thin] (0,2) -- (8,2);        
\draw[ultra thin] (0,2.3) -- (0,1.7);       
\draw[ultra thin] (8,2.3) -- (8,1.7);           
\node[ann2] at (4,2) {$L$} ;    
\end{scope}
\end{scope}
\end{tikzpicture}

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>