PGF/TikZ

From XennisWiki
Jump to: navigation, search

PGF/TikZ is a tandem of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ is a set of higher-level macros that use PGF. (Wikipedia)

Examples

Directed graph

img.tex

\documentclass[tikz,border=10pt]{standalone}
\usepackage[english,ngerman]{babel}
\usepackage[utf8]{inputenc}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm,
                thick,main node/.style={circle,draw,font=\Large\bfseries}]

  \node[main node] (a) {A};
  \node[main node] (b) [below left of=a] {B};
  \node[main node] (c) [below right of=a] {C};

  \path
    (a) edge [loop above] node {200} (a)
        edge node {30} (b)
        edge node {25} (c)
    (b) edge [loop below] node {102} (b)
    	edge [bend left] node {10} (a)
        edge [bend right] node {5} (c)
    (c) edge [loop below] node {165} (c)
        edge node[above] {45} (b);
\end{tikzpicture}
\end{document}

img.tex

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
	every path/.style={>=latex},
	every node/.style={draw}
]
	\node[rectangle, align=left] (cse) { CiteSeerExtractor\\\tiny(API) };
	\node[rectangle, right= of cse, align=left] (pdfmef) { PDFMEF\\\tiny(API) };
	\node[rectangle, right= of pdfmef, align=left] (grobid) { Grobid\\\tiny(API, Extractor)};
  
	\node[rectangle, below= of cse, align=left] (pdfbox) {PDFBox\\\tiny(PDF to text)};  
	\node[rectangle, below= of pdfmef, align=left] (parscit) {ParsCit\\\tiny(Extractor)};

	\node[rectangle, below= of parscit, align=left] (crfpp) {CRF++\\\tiny(CRF library)};
	\node[rectangle, below= of grobid, align=left] (wapit) {Wapit\\\tiny(CRF library)};
  
  	\draw[->] (cse) edge (pdfbox);
	\draw[->] (cse) edge (parscit);

	\draw[->] (pdfmef) edge (parscit);
	\draw[->] (pdfmef) edge (pdfbox);
	\draw[->] (pdfmef) edge (grobid);
	\draw[->] (parscit) edge (crfpp);
	
	\draw[->] (grobid) edge (wapit);
	\draw[dashed,->] (grobid) edge (crfpp);
\end{tikzpicture}
\end{document}

Graph as subfigure

TikZ - Subfigure example.png


\documentclass{article}
\usepackage[tight,footnotesize]{subfigure}
\usepackage{tikz}
\usetikzlibrary{arrows,through} 

\begin{document}

\begin{figure}[!t]
	\centering
	\subfigure[Sender-to-Receiver]{
		\label{fig:s2r}
		\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.2cm, thick,node/.style={circle,draw}]
			\node[node]	(A) {A};
			\node[node]	(1) [below left of=A] {1};
			\node[node]	(2) [below right of=A] {2};
			\node (kreis) at (A) [draw, circle through=(A)] {};
	
  			\path
				(A) edge node [below right] {$C_A(t)$} (1)
				(1) edge [bend left] node [above] {M} (A)
				(A) edge node [below left] {} (2)
				(2) edge [bend right] node [above] {M} (A)
    		;
		\end{tikzpicture}
	}
	\subfigure[Receiver-to-Receiver]{
		\label{fig:r2r}
		\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.2cm, thick,node/.style={circle,draw}]
			\node[node]	(A) {A};
			\node[node]	(1) [below left of=A] {1};
			\node[node]	(2) [below right of=A] {2};
	
  			\path
				(A) edge node [above] {M} (1)
				(A) edge node [above] {M} (2)
				(1) edge node [above] {$C_1(t$)} (2)
				(2.south west) edge node [below] {$C_2(t')$} (1.south east)
    		;
		\end{tikzpicture}
	}
	\caption{Comparison of a sender-to-receiver~(a) and a receiver-to-receiver synchronization (b) in a simple network with three nodes. }
	\label{fig:s2tAndt2t}
\end{figure}

\end{document}

TikZ and Beamer

see Beamer#TikZ and Beamer and Beamer#Animated TikZ mindmap

Entity–relationship model

Entity–relationship model realized with the Tikz-er2 package.

img.tex

\documentclass[border=10pt]{standalone}
\usepackage[english,ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz-er2}
\usetikzlibrary{positioning}
\usetikzlibrary{shadows}

\tikzstyle{every entity} = [top color=white, bottom color=blue!30, 
                            draw=blue!50!black!100, drop shadow]
\tikzstyle{every weak entity} = [drop shadow={shadow xshift=.7ex, 
                                 shadow yshift=-.7ex}]
\tikzstyle{every attribute} = [top color=white, bottom color=yellow!20, 
                               draw=yellow, node distance=1cm, drop shadow]
\tikzstyle{every relationship} = [top color=white, bottom color=red!20, 
                                  draw=red!50!black!100, drop shadow]
\tikzstyle{every isa} = [top color=white, bottom color=green!20, 
                         draw=green!50!black!100, drop shadow]

\begin{document}
\begin{tikzpicture}[node distance=1.5cm, every edge/.style={link}]

	% Publication
	\node[entity] (pub) {Publication};
	
	% Author affiliation relation
	\node[relationship] (paa) [left=of pub] {has} edge node [above] {* ~~~ 1} (pub);
	\node[entity] (author) [above=of paa] {Author} edge node [right, align=left] {1\\~\\*} (paa);
	\node[entity] (affilation) [below=of paa] {Affiliation} edge node [right, align=left] {*\\~\\0..1} (paa);
	
	% References, keyword, journal and URL 
	\node[entity] (reference) [above=of pub] {Reference} edge node [left, align=left] {*\\~\\1} (pub);
	\node[entity] (keyword) [right=of pub] {Keyword} edge node [above, align=left] {1 ~~~ *}  (pub);
	\node[entity] (journal) [below right=of pub] {Journal} edge node [right, align=left] {*\\~~~~~~0..1} (pub);
	\node[entity] (url) [below=of pub] {URL} edge node [left, align=left] {1\\~\\*} (pub);

\end{tikzpicture}
\end{document}

Gnuplot

Further information: tutorials :: Funktionen plotten

Installation

  • Download and install gnuplot
  • Let Tex execute external programs: --enable-write18 (for MiKTeX) or --shell-escape, example for PdfLaTeX
pdflatex -synctex=1 -interaction=nonstopmode --enable-write18 %.tex

Examples

More examples: GNUPLOT examples, concret: Example: GNUPLOT basics

The command to compile and create the following examples is (% is the placeholder for the file name without extension)

pdflatex --jobname=%-fig --enable-write18 %.tex
Example 1


img.txt (File name has to be inserted into \pgfrealjobname)

% Based on http://www.texample.net/tikz/examples/gnuplot-basics/
\documentclass{article}
 
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\pgfrealjobname{img}
 
\begin{document}
\pagestyle{empty}
 
\beginpgfgraphicnamed{img-fig}
	\begin{tikzpicture}[domain=0:4]
	    \draw[very thin,color=gray] (-0.1,-0.1) grid (3.9,3.9);
	    \draw[->] (-0.2,0) -- (4.2,0) node[below] {UTC $t$};
	    \draw[->] (0,-0.2) -- (0,4.2) node[rotate=90, yshift=0.5cm, left] {Clock time $C(t)$};
 
	    \draw[domain=0:3.8, color=blue] plot[id=x] function{x} 
	        node[right] {$f(t)=1$};
	    \draw[color=black] plot[id=x] function{0.5*x} 
	        node[right] {$f(t) < 1$};
	    \draw[domain=0:2, color=black] plot[id=x] function{2*x} 
	        node[above] {$f(t) > 1$};
	\end{tikzpicture}
\endpgfgraphicnamed
 
\end{document}
Example 2

img.tex

\documentclass{article}
 
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\pgfrealjobname{img}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
 
\begin{document}
\pagestyle{empty}
 
\beginpgfgraphicnamed{img-fig}
	\begin{tikzpicture}[domain=0:4]
	    \draw[very thin,color=gray] (-0.1,-0.1) grid (4,4);
	    \draw[->] (-0.2,0) -- (4.2,0) node[right] {$x_1$};
	    \draw[->] (0,-0.2) -- (0,4.2) node[above] {$x_2$};

	    \draw[fill=blue!20!white,opacity=0.6] (0,0) -- (0,2) -- (4/3,4/3) -- (2,0);
	    
	    \draw[domain=0:2, color=green!40!black] plot[id=x] function{-2*x+4} 
	        node[below] {$2x_1+x_2\leq 4$};
	    \draw[color=black] plot[id=x] function{-0.5*x+2} 
	        node[above right] {$\frac{1}{2}x_1+x_2\leq 2$};
	     
	    \draw (0,0) circle [radius=.1cm] node[below left] {$x_a$};
	    \draw (0,2) circle [radius=.1cm] node[above left] {$x_b$};
	    \draw (4/3,4/3) circle [radius=.1cm] node[above right] {$x_c$};
	    \draw (2,0) circle [radius=.1cm] node[above right] {$x_d$};	    
	\end{tikzpicture}
\endpgfgraphicnamed
 
\end{document}

See also

External links