User Tools

Site Tools


latex:tikz

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
latex:tikz [2018/06/23 16:00] โ€“ [Flowchart] iwnlatex:tikz [2021/08/09 23:46] โ€“ [How to draw diagrams in LaTeX with TikZ] iwn
Line 4: Line 4:
  
 ''TikZ'' should work out-of-the-box with ''\usepackage{tikz}'', but the syntax is not so straightforward at first. To get started, take a look at these materials: ''TikZ'' should work out-of-the-box with ''\usepackage{tikz}'', but the syntax is not so straightforward at first. To get started, take a look at these materials:
-  * [[https://www.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html|some basic shapes]]ย +  * [[https://www.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html|Some basic shapes]].ย 
-  * [[https://www.tug.org/TUGboat/tb29-1/tb91walczak.pdf|some more basic shapes]]ย +  * [[https://www.tug.org/TUGboat/tb29-1/tb91walczak.pdf|Some more basic shapes]].ย 
-  * [[http://www.texample.net/tikz/examples/|large database of simple and advanced examples]] with a [[http://www.texample.net/tikz/examples/area/physics/|physics section]], including a Feynman diagram and a Standard Model diagram.ย +  * [[http://www.texample.net/tikz/examples/|TeXample]]: A large database of simple and advanced examples with a [[http://www.texample.net/tikz/examples/area/physics/|physics section]], including a Feynman diagram and a Standard Model diagram. This website will be superseded by [[https://tikz.net|TikZ.net]].ย 
-  * [[http://pgf.sourceforge.net/pgf_CVS.pdf|official TikZ & PGF manual]]ย +  * The examples below and many more are also available on [[https://github.com/IzaakWN/CodeSnippets/tree/master/LaTeX/TikZ|GitHub]] and [[https://ineuteli.web.cern.ch/ineuteli/diagrams/|this gallery page]].ย 
-ย +  * [[http://pgf.sourceforge.net/pgf_CVS.pdf|Official TikZ & PGF manual]].ย 
 +  * For Feynman diagrams, please see [[latex:feynman|this page]].
  
  ===== Control regions =====  ===== Control regions =====
Line 110: Line 110:
  ===== Sets =====  ===== Sets =====
  
-Example of a flow chart by using a predefined box with ''\def'' and arguments.+Another simple example of a rectangle with rounded corners:
  
 <WRAP group><WRAP half column 55% lo> <WRAP group><WRAP half column 55% lo>
Line 146: Line 146:
  ===== Flowchart =====  ===== Flowchart =====
  
-Another simple example of a rectangle with rounded corners ([[latex:tikz:guitar_tunings|full code]]):+Example of a flow chart by using predefined drawing styles and a predefined box with ''\def'' and arguments ([[latex:tikz:guitar_tunings|full code]]):
  
 <WRAP group><WRAP half column 55% lo> <WRAP group><WRAP half column 55% lo>
Line 167: Line 167:
   \tuning(C,  0, 3, DADGBE,      drop D)   \tuning(C,  0, 3, DADGBE,      drop D)
   \tuning(Cl,-2, 3, EAEAC\sh\,E, open A)   \tuning(Cl,-2, 3, EAEAC\sh\,E, open A)
-  \tuning(Cr, 2.3, 3, E\b A\b D\b G\b B\b E\b, standard E\b/half step down) 
   \tuning(Bl,-1, 2, DGDGBD,      open G/spanish)   \tuning(Bl,-1, 2, DGDGBD,      open G/spanish)
   \tuning(Br, 1, 2, DADFAD,      open minor/cross-note D)   \tuning(Br, 1, 2, DADFAD,      open minor/cross-note D)
Line 178: Line 177:
   \draw[arrow]     (D)  -- (C);   \draw[arrow]     (D)  -- (C);
   \draw[sarrow]    (D)  -- (Cl);   \draw[sarrow]    (D)  -- (Cl);
-  \draw[sarrow]    (D)  -- ([xshift=7]Cr.north west) node[fscale=-2,midway,above=2,right=-2] {capo}; 
   \draw[sarrow,<-] (Cl) -- (Bl) node[fscale=-2,midway,above=1,right=-2] {capo};   \draw[sarrow,<-] (Cl) -- (Bl) node[fscale=-2,midway,above=1,right=-2] {capo};
   \draw[sarrow]    (C)  -- (Bl);   \draw[sarrow]    (C)  -- (Bl);
Line 192: Line 190:
 \\ \\ \\ \\ \\ \\ \\ \\
 </WRAP></WRAP> </WRAP></WRAP>
 +
 +
 +
 +Example of a node with arrows to text below it ([[latex:tikz:arrows_to_nodes|full code]]). Note the use of ''|-'' between coordinates to draw a line that first goes down vertically and then horizontally. Vice versa can be done with ''-|''. The ''\strut'' command is used to ensure that the text line has a fixed height.
 +
 +<WRAP group><WRAP half column 55% lo>
 +<code latex>
 +\begin{tikzpicture}[yscale=0.8,anchor=west]
 +  
 +  % FIRST COLUMN
 +  \node[anchor=west,draw=myblue,fill=mylightblue,thick,rounded corners=4,inner sep=1.5pt] (L) at (0,4) {\;\strut$qq\nu\nu$\;};  
 +  \node (L1) at (0.5,3) {\strut$jj\nu\nu$};
 +  \node (L2) at (0.5,2) {\strut bb$\nu\nu$};
 +  \node (L3) at (0.5,1) {\strut tt$\nu\nu$};
 +  \draw[->,myblue,thick] (L.south west)++(0.18,0) |- (L1.west);
 +  \draw[->,myblue,thick] (L.south west)++(0.18,0) |- (L2.west);
 +  \draw[->,myblue,thick] (L.south west)++(0.18,0) |- (L3.west);
 +  
 +  % SECOND COLUMN
 +  \begin{scope}[shift={(2.5,0)}]
 +    \node[draw=myblue,fill=mylightblue,thick,rounded corners=4,inner sep=1.5pt] (M) at (0,4) {\;\strut$qq\ell\ell$\;};
 +    \node (M1) at (0.5,3) {\strut$jj\mu\mu$};
 +    \node (M2) at (0.5,2) {\strut bb$\tau\tau$, b$\tau\tau$};
 +    \node (M3) at (0.5,1) {\strut tt$\tau\tau$};
 +    \draw[->,myblue,thick] (M.south west)++(0.18,0) |- (M1.west);
 +    \draw[->,myblue,thick] (M.south west)++(0.18,0) |- (M2.west);
 +    \draw[->,myblue,thick] (M.south west)++(0.18,0) |- (M3.west);
 +  \end{scope}
 +  
 +  % THIRD COLUMN
 +  \begin{scope}[shift={(5.0,0)}]
 +    \node[draw=myblue,fill=mylightblue,thick,rounded corners=4,inner sep=1.5pt] (R) at (0,4) {\;\strut$qq\ell\nu$\;};
 +    \node (R1) at (0.5,3) {\strut$jj\mu\nu$};
 +    \draw[->,myblue,thick] (R.south west)++(0.18,0) |- (R1.west);
 +  \end{scope}
 +  
 +\end{tikzpicture}
 +</code>
 +</WRAP><WRAP half column 40%>
 +\\ \\ \\ \\
 +{{ latex:tikz:arrows_to_nodes.png ? 1000 }}
 +\\ \\ \\ \\
 +</WRAP></WRAP>
 +
 +
  ===== Atom models =====  ===== Atom models =====
  
Line 931: Line 974:
 \\ \\
 </WRAP></WRAP> </WRAP></WRAP>
 +
 +
 +
  ===== Higgs decay planes =====  ===== Higgs decay planes =====
  
Line 948: Line 994:
  
  
 +
 + ===== Impact parameters =====
 +
 +Examples of defining impact parameters in proton collisions ([[latex:tikz:impact_parameters|full code]]).
 +
 +<WRAP group><WRAP column 28%>
 +{{latex:tikz:impact_parameters1.png ? 0x1000 }}
 +</WRAP><WRAP column 26%>
 +{{latex:tikz:impact_parameters2.png ? 0x1000 }}
 +</WRAP><WRAP column 38%>
 +{{latex:tikz:impact_parameters3.png ? 0x1000 }}
 +</WRAP></WRAP>
  
  ===== Timelines and scales =====  ===== Timelines and scales =====
Line 968: Line 1026:
 \\ \\
 </WRAP></WRAP> </WRAP></WRAP>
- 
  
  
Line 1078: Line 1135:
 \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\  \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 
 </WRAP></WRAP> </WRAP></WRAP>
 +
 +
 +
 +
 +
 + ===== Top jets ===
 +
 +The full code can be found [[latex:tikz:jet_top|here]].
 +
 +<WRAP group><WRAP column 30%>
 +{{ latex:tikz:jet_top_resolved.png ? 0x1000 }}
 +</WRAP><WRAP column 30%>
 +{{latex:tikz:jet_top_merged_partial.png ? 0x1000 }}
 +</WRAP><WRAP column 30%>
 +\\ \\ \\ \\ \\
 +{{latex:tikz:jet_top_merged_full.png ? 0x1000 }}
 +</WRAP></WRAP>
 +
 +<code latex>
 +\newcommand\jetcone[5][blue]{{
 +  \pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}{\pgfpointanchor{#3}{center}}
 +  \edef\ang{#4/2}
 +  \edef\e{#5}
 +  \edef\vang{\pgfmathresult} % angle of vector OV
 +  \tikzmath{
 +    coordinate \C;
 +    \C = (#2)-(#3);
 +    \x = veclen(\Cx,\Cy)*\e*sin(\ang)^2; % x coordinate P
 +    \y = tan(\ang)*(veclen(\Cx,\Cy)-\x); % y coordinate P
 +    \a = veclen(\Cx,\Cy)*sqrt(\e)*sin(\ang); % vertical radius
 +    \b = veclen(\Cx,\Cy)*tan(\ang)*sqrt(1-\e*sin(\ang)^2); % horizontal radius
 +    \angb = acos(sqrt(\e)*sin(\ang)); % angle of P in ellipse
 +  }
 +  \coordinate (tmpL) at ($(#3)-(\vang:\x pt)+(\vang+90:\y pt)$); % tangency
 +  \draw[thin,#1!40!black,fill=#1!50!black!80,rotate=\vang]
 +    (#3) ellipse({\a pt} and {\b pt});
 +  \draw[thin,#1!40!black,fill=#1!80!black!40,rotate=\vang]
 +    (tmpL) arc(180-\angb:180+\angb:{\a pt} and {\b pt})
 +    -- ($(#2)+(\vang:0.018)$) -- cycle;
 +}}
 +
 +\begin{tikzpicture}
 +  \def\R{2.5}
 +  \coordinate (O) at (0,0);
 +  \coordinate (BJ) at ( 65:1.1*\R); % b jet 1
 +  \coordinate (J1) at ( 15:1.0*\R); % q jet 1
 +  \coordinate (J2) at (-20:1.0*\R); % q jet 2
 +  \jetcone[green!80!black]{O}{BJ}{14}{0.10}
 +  \jetcone{O}{J1}{16}{0.08}
 +  \jetcone{O}{J2}{16}{0.10}
 +  \node[green!50!black] at (65:1.24*\R) {b};
 +  \node[blue!80!black,right] at (-5:1.00*\R) {$\mathrm{W} \to qq$};
 +\end{tikzpicture}
 +</code>
 +
 +
 + ===== Jets vectors =====
 +
 +One can do some projections of jet and MET vectors to construct variables like MT2 in SUSY searches. The full code can be found [[latex:tikz:jet_vectors|here]].
 +
 +<WRAP group><WRAP half column 54%>
 +{{ latex:tikz:jet_vectors.png ? 0x1000 }}
 +</WRAP><WRAP half column 42%>
 +{{latex:tikz:jet_vectors_3D.png ? 0x1000 }}
 +</WRAP></WRAP>
 +
 +<code latex>
 +\newcommand\jetcone[4]{
 +  \pgfmathanglebetweenpoints{\pgfpointanchor{#1}{center}}{\pgfpointanchor{#2}{center}}
 +  \edef\tmpang{\pgfmathresult}
 +  \coordinate (tmpC) at ($(#2)+(\tmpang-180:{abs(#4)+0.4})$); % center
 +  \coordinate (tmpL) at ($(tmpC)+(\tmpang+90:#3)$); % left corner
 +  \coordinate (tmpR) at ($(tmpC)+(\tmpang-90:#3)$); % right corner
 +  \draw[cone,rotate=\tmpang]
 +    %(tmpR) arc(90:-90:{#4} and {#3});
 +    (tmpC) ellipse({#4} and {#3});
 +  \begin{scope}
 +    \clip[rotate=\tmpang] (tmpR) -- (#1) -- (tmpL) arc(90:-90:{#4+0.6} and {#3});
 +    \draw[vector] (#1) -- (#2);
 +  \end{scope}
 +  \draw[cone,rotate=\tmpang]
 +    (tmpL) arc(90:270:{#4} and {#3}) -- (#1) -- cycle;
 +}
 +
 +% MT2
 +\begin{tikzpicture}
 +  \def\R{2.8}
 +  \coordinate (O) at (0,0);
 +  \coordinate (J1) at (170:\R); % jet 1 pT
 +  \coordinate (J2) at (-35:\R); % jet 2 pT
 +  \coordinate (M1) at (130:0.9*\R); % pTmiss component 1
 +  \coordinate (M2) at (-10:0.8*\R); % pTmiss component 2
 +  \coordinate (M) at ($(M1)+(M2)$); % total missing momentum
 +  
 +  % PTMISS
 +  \draw[ptmiss,-,very thin] (M1) -- (M) -- (M2);
 +  \draw[ptmiss] (O) -- (M1) node[left=2,above=-3] {$\ptmissX{1}$};
 +  \draw[ptmiss] (O) -- (M2) node[right=0] {$\ptmissX{2}$};
 +  \draw[ptmiss] (O) -- (M) node[above right=-1] {$\ptmiss$};
 +  
 +  % JET CONES
 +  %\draw[vector] (O) -- (J1);
 +  %\draw[vector] (O) -- (J2);
 +  %\cone{O}{J1}{0.3}{0.15}
 +  \jetcone{O}{J1}{0.4}{0.08}
 +  \jetcone{O}{J2}{0.4}{0.10}
 +  \node[vector,left] at (J1) {$\vv{p}_{j_1}$};
 +  \node[vector,right] at (J2) {$\vv{p}_{j_2}$};
 +  
 +  % CURLY BRACE
 +  \draw[line width=0.5,mygreen,decorate,decoration={brace,amplitude=6}]
 +    ($(J1)+(195:0.35*\R)$) -- ($(M1)+(120:0.30*\R)$) node[midway,above left=2] {$\MTX{1}$};
 +  \draw[line width=0.5,mygreen,decorate,decoration={brace,amplitude=6}]
 +    ($(M2)+(10:0.48*\R)$) -- ($(J2)+(-45:0.26*\R)$) node[midway,below=4,right=4] {$\MTX{2}$};
 +  
 +\end{tikzpicture}
 +</code>
 +
 +
 + ===== Tangent to a circle or ellipse =====
 +
 +Some different methods of finding the tangent to a circle or ellipse in TikZ.
 +Using these methods a nice cone can be made.
 +The full code can be found [[latex:tikz:circle_tangent|here]].
 +
 +
 +<WRAP group><WRAP half column 45%>
 +{{ latex:tikz:circle_tangent.png ? 0x250 }}
 +</WRAP><WRAP half column 45%>
 +{{latex:tikz:circle_tangent_ellipse.png ? 0x200 }}
 +</WRAP></WRAP>
 +
 +<WRAP group><WRAP half column 45%> \\ \\
 +{{ latex:tikz:circle_tangent_cone1.png ? 0x200 }}
 +</WRAP><WRAP half column 45%>
 +{{latex:tikz:circle_tangent_cone2.png ? 0x250 }}
 +</WRAP></WRAP> \\
 +
 +<code latex>
 +% TANGENT to CIRCLE - known: r, q
 +\begin{tikzpicture}
 +  \def\r{1.5} % radius
 +  \def\q{4} % distance center-external point q = |OQ|
 +  \def\x{{\r^2/\q}} % Q x coordinate
 +  \def\y{{\r*sqrt(1-(\r/\q)^2}} % Q y coordinate
 +  \coordinate (O) at (0,0); % circle center O
 +  \coordinate (Q) at (\q,0); % external point Q
 +  \coordinate (P) at (\x,\y); % point of tangency, P
 +  \draw[->] (0,-1.3*\r) -- (0,1.5*\r);
 +  \draw[->] (-1.3*\r,0) -- (\q+0.4*\r,0);
 +  \draw[dashed] (\x,0) |- (0,\y);
 +  \draw[myblue,thick] (O) circle(\r);
 +  \draw[mygreen,thick] ($(Q)!-0.2!(P)$) -- ($(Q)!1.3!(P)$);
 +  \draw[mygreen,thick] ($(O)!-0.3!(P)$) -- ($(O)!1.4!(P)$);
 +  \rightAngle{Q}{P}{O}{0.40}
 +  \fill[myred] (O) circle(0.05) node[below right] {O};
 +  \fill[myred] (Q) circle(0.05) node[below left] {Q};
 +  \fill[myred] (P) circle(0.05) node[above=3,right=4] {P};
 +\end{tikzpicture}
 +
 +% TANGENT to ELLIPSE - known: a, b, q
 +\begin{tikzpicture}
 +  \def\a{1.5} % horizontal radius
 +  \def\b{1.0} % vertical radius
 +  \def\q{4} % distance center-external point q = |OQ|
 +  \def\x{{\a^2/\q}} % x coordinate P
 +  \def\y{{\b*sqrt(1-(\a/\q)^2}} % y coordinate P
 +  \coordinate (O) at (0,0); % circle center O
 +  \coordinate (Q) at (\q,0); % external point Q
 +  \coordinate (P) at (\x,\y); % point of tangency, P
 +  \draw[->] (0,-\b-0.3*\a) -- (0,\b+0.4*\a);
 +  \draw[->] (-1.3*\a,0) -- (\q+0.4*\a,0);
 +  \draw[dashed] (\x,0) |- (0,\y);
 +  \draw[myblue,thick] (O) ellipse({\a} and {\b});
 +  \draw[mygreen,thick] ($(Q)!-0.2!(P)$) -- ($(Q)!1.3!(P)$);
 +  \draw[mygreen,thick] ($(O)!-0.3!(P)$) -- ($(O)!1.4!(P)$);
 +  \fill[myred] (O) circle(0.05) node[below right] {O};
 +  \fill[myred] (Q) circle(0.05) node[below left] {Q};
 +  \fill[myred] (P) circle(0.05) node[above=3,right=4] {P};
 +\end{tikzpicture}
 +
 +</code>
latex/tikz.txt ยท Last modified: 2022/12/02 15:42 by iwn