Difference between revisions of "Getting started with TIKZ"

From Remeis-Wiki
Jump to navigation Jump to search
(Alex M.: basic TIKZ for positioning text/figures and drawing)
(No difference)

Revision as of 13:41, 13 April 2018

Getting started with TIKZ

TIKZ can give you some measure of control when you want to position text or figures, and is useful for drawing basic shapes (lines, arrows, highlight boxes, polygons).

Most of what I learned from TIKZ comes from 1) Z. Walczak's manual at http://www.tug.org/TUGboat/tb29-1/tb91walczak.pdf and 2) other people's TIKZ slides.

See also http://www.cremeronline.com/LaTeX/minimaltikz.pdf

This page summarizes what I have been able to understand; please see the tex file and pdf result for the code below at /home/markowitz/talks/litclub/tikzprac.pdf and tikzprac.tex

Header and definitions (mostly taken from one of Tommy's talks)

\documentclass{beamer}  
\usetheme{Frankfurt}
\useinnertheme{circles}
\usecolortheme{wolverine}
\usepackage[utf8]{inputenc}     % ggfs. anpassen -- Eingabekodierung
\usepackage[T1]{fontenc}        % Zeichensatzkodierung
\usepackage[english]{babel}     % Sprache einstellen
\usepackage{ragged2e}           % besserer Flattersatz
\usepackage{xspace}             % intelligenter Abstand
\usepackage{graphicx}
\usepackage{multicol}
\usepackage{pdfpages}
\usepackage{tikz}               % Plotting and alignment tools
\usetikzlibrary{shadows}        
\usetikzlibrary{fadings}
\usepackage[outline]{contour}
\contourlength{0.2pt}
% set your graphics path
\graphicspath{{./figsXTE/}}
\newcommand{\Msun}{\hbox{$\rm\thinspace M_{\odot}$}}
\newcommand{\Lsun}{\hbox{$\rm\thinspace L_{\odot}$}}
\setbeamersize{text margin left=5mm}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define your own colors: 
% especially the standard green needs to be dark
\definecolor{tdgreen}{rgb}{0,0.6,0}
\definecolor{tdblue}{rgb}{.114, .157, .706 }
\definecolor{tdred}{rgb}{0.922, .086, .086 }
\definecolor{agmcyan}{rgb}{0.401, .986, .986 }      
% for creating my title page, I define my font sizes
\newcommand\titleFontSize{\fontsize{0.65cm}{0.6cm}\selectfont}
\newcommand\authorFontSize{\fontsize{0.55cm}{0.5cm}\selectfont} 
% simple commands for highlighting text in different colors
\newcommand{\greentext}[1]{{\color{tdgreen}#1}}
\newcommand{\redtext}[1]{{\color{tdred}#1}}
\newcommand{\bluetext}[1]{{\color{tdblue}#1}}
\newcommand{\yellowtext}[1]{{\color{yellow}#1}}
\newcommand{\cyantext}[1]{{\color{agmcyan}\contour{black}{#1}}}
% define my own slides: style chosen here is plain
\newcommand{\tdframe}[1]{\frame[plain]{#1}}
\definecolor{Authors}{rgb}{0.8,0.1,0.01}
\newcommand\outlineCaption[1]{\textbf{\pscharpath[linecolor=orange,linewidth=.1pt, gradangle=90,fillstyle=gradient,gradbegin=Authors,gradend=Authors,gradmidpoint=1]{#1}}}

Code for setting up a TIKZ picture, positioning and drawing

\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Intro.}
\begin{frame}
\frametitle{Anchors, Lines, Arrows}
\centering    
%%% Do "y=\textheight" if there is no "\frametitle"
%%% Do "y=0.88\textheight" if there is a "\frametitle"
\begin{tikzpicture}[x=\columnwidth, y=0.88\textheight]     
% define the coordinate system
\path [use as bounding box] (0,0) rectangle(1,1);  
% % % % % % % % anchoring a picture:
\draw (0.06,0.85) node [anchor=west]
     {\includegraphics[height=2.8\baselineskip]{f3a.jpg}} ;
% % % % % % % % anchoring text:
\draw[color=tdgreen]   (0.50,0.90) node [anchor=west] 
 {\tiny \centering \textbf{0.50 0.90 anchor=west}} ;
\draw[color=red]   (0.50,0.90) node [anchor=east] 
 {\tiny \centering \textbf{0.50 0.90 anchor=east}} ;
\draw[color=blue]   (0.50,0.90) node [anchor=north] 
 {\tiny \centering \textbf{0.50 0.90 anchor=north}} ;
\draw[color=pink]   (0.50,0.90) node [anchor=south] 
 {\tiny \centering \textbf{0.50 0.90 anchor=south}} ;
% % % % % % % % Highlight boxes:
\draw[color=yellow]   (0.87,0.94) node [anchor=center,text width=2.4cm,fill=black!65, rounded corners] 
{\centering \textbf{Highlight box}};
\draw[color=yellow]   (0.87,+0.80) 
node [anchor=center, text width=0.7cm, fill=tdgreen!85, drop shadow] 
{ {\scriptsize \centering \textbf{Box2}} };
\draw[color=black]   (0.87,+0.66) 
node [anchor=center, text width=0.7cm, fill=black!05, drop shadow, rounded corners] 
{ {\scriptsize \centering \textbf{Box3}} };


% % % % % % % % Lines and arrows (curved \& straight):
\draw [-, line width=0.1mm, color=purple] (0.00,0.00) -- (0.00,1.00);
\draw [-, line width=0.1mm, color=yellow] (1.00,0.00) -- (1.00,1.00);
\draw [->,line width=0.8mm, color=orange] (0.06,0.43) -- (0.18,0.43);
\draw [-, line width=0.6mm, color=red]    (0.06,0.40) -- (0.18,0.38);
\draw [<->,line width=0.4mm, color=green] (0.06,0.35) to
     [out=40,in=135] (0.18,0.33);
\draw [->,line width=0.2mm, color=cyan] (0.06,0.32) to
     [out=+40,in=-70] (0.22,0.25);
\draw [->,line width=0.2mm, color=tdblue] (0.06,0.25) to
     [out=-40,in=-70] (0.22,0.15);


% % % % % % % % drawing shapes and associated text:
\draw (0.50,0.25) node
[circle,draw,dashed]{1} -- (0.55,0.33) node [circle,draw,dashed]{2};
\draw 
(0.50,0.11) node [anchor=north east,circle,draw,dashed]{3} -- 
(0.55,0.19) node [anchor=south west,circle,draw,dashed]{4};
% % % % % % % % drawing shapes and associated text: a rectangle with text:
\draw [color=red] (0.85,0.25) node
[rectangle,dashed,draw,color=green,label=above:On Top,label=below:{\textbf{under}}]{\textit{my rectangle}};
   \end{tikzpicture}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{more shapes}
\begin{frame}
%\frametitle{Slide 2}
\centering    
%%% Do "y=\textheight" if there is no "\frametitle"
%%% Do "y=0.88\textheight" if there is a "\frametitle"
\begin{tikzpicture}[x=\columnwidth, y=\textheight]     
% define the coordinate system
\path [use as bounding box] (0,0) rectangle(1,1);  
\draw [-, line width=0.1mm, color=brown] (0.00,0.00) -- (0.00,1.00)
-- (1.00,1.00) -- (1.00,0.00) -- cycle;
% % % % % % % % % % % % Circles:
\draw                               (0.08,0.86) circle (6mm);
\draw [line width=0.8mm,color=green] (0.23,0.86) circle (4ex);
% % % % % % % % % % % % Filled circle:
\draw [line width=1.8mm,color=orange,fill=gray!30!white] (0.38,0.86) circle (4ex);
% % % % % % % % % % % %  Ellipses:
\draw  (0.56, 0.86) [line width=0.2mm,color=blue] ellipse (7pt and 24pt);
\draw  (0.56, 0.86) [line width=0.2mm,color=gray] ellipse (24pt and 7pt);
% % % % % % % % % % % %  A filled triangle:
\draw [color=green,fill=brown,line width=0.2mm] (0.82,0.90) -- (0.89,0.97) --
(0.94,0.93) -- cycle;
% % % % % % % % % % % % Arcs and filled arcs:
% % % % % % % % % % % % (notice that those are semicolons, 
% % % % % % % % % % % % not commas, in the arguments for 'arc')
\draw  (0.23,0.65) [line width=0.6mm,color=brown,dashed] arc (-65:+80:2ex);
\draw  (0.33,0.65) [line width=0.2mm,color=red] arc (-45:+60:2ex);
\draw  (0.43,0.65) [line width=0.4mm,color=blue,fill=yellow] arc (-45:+68:2ex);

% % % % % % % % % % % % A shape with an arc and lines:
\fill [yellow] (0.50,0.50) -- (0.56,0.57) arc(-135:+88:5ex) -- cycle;
% % % % % % % % % % % % Grids:
\draw [step=0.50cm,cyan,thin,dashed]  (0.12,0.03)   grid (0.9,0.45);
\draw [step=0.25cm,gray,thick,dotted]  (0.9,0.03)   grid (0.97,0.45);
% % % % % % % % % % % % Polygons featuring arcs:
% % % % % % % % % % % % Pac-man:
\draw [line width=0.1mm,fill=yellow] (0.39,0.24) --
(0.4766,0.29) arc (+25:+335:1.1cm) -- cycle;
% % % % % % % % % % % % Ghost:
\draw [color=cyan,fill=blue,line width=0.2mm] (0.60,0.17) -- (0.60,0.25) 
arc (+180:0:0.05\textwidth) -- (0.70,0.17) -- 
(0.683,0.19) --
(0.667,0.17) --
(0.650,0.19) --
(0.633,0.17) --
(0.616,0.19) -- cycle; 
\draw (0.6363,0.26) [color=pink,fill] circle (0.31ex);
\draw (0.6637,0.26) [color=pink,fill] circle (0.31ex);
\draw [-,color=pink,line width=0.2mm] (0.617,0.22) to 
     [out=+0,in=+180] (0.628,0.225) to 
     [out=+0,in=+180] (0.639,0.22) to 
     [out=+0,in=+180] (0.650,0.225) to 
     [out=+0,in=+180] (0.661,0.22)  to 
     [out=+0,in=+180] (0.672,0.225) to 
     [out=+0,in=+180] (0.683,0.22)  ;
 \end{tikzpicture}
\end{frame}