Difference between revisions of "TikZ"

From ConTeXt wiki
(New page: < Modules | Graphics > [http://pgf.sourceforge.net/ TikZ/pgf] is a portable graphics pack...)
 
m (Edited the Commutative Diagrams section for more clarity)
 
(6 intermediate revisions by 6 users not shown)
Line 4: Line 4:
 
also supporting ConTeXt. It is a possible alternative to [[MetaPost]] & [[MetaFun]].       
 
also supporting ConTeXt. It is a possible alternative to [[MetaPost]] & [[MetaFun]].       
  
[http://modules.contextgarden.net/TikZ TikZ] is included as a (third party) [http://modules.contextgarden.net module]
+
TikZ is included as a (third party) module for ConTeXt.
for ConTeXt.                                                                                                        
 
  
 
== Example ==
 
== Example ==
Line 11: Line 10:
 
From the TikZ/pgf [http://dl.contextgarden.net/modules/t-tikz/doc/generic/pgf/pgfmanual.pdf manual].
 
From the TikZ/pgf [http://dl.contextgarden.net/modules/t-tikz/doc/generic/pgf/pgfmanual.pdf manual].
  
<context source="yes">
+
<texcode>
 
\setupcolors[state=start]
 
\setupcolors[state=start]
 +
\definecolor[darkgreen][g=.5]
 
\usemodule[tikz]         
 
\usemodule[tikz]         
  
Line 46: Line 46:
 
                 node[right,text width=6cm,information text]
 
                 node[right,text width=6cm,information text]
 
                 {
 
                 {
                         The {\green angle $\alpha$} is $30^\circ$ in the
+
                         The {\darkgreen angle $\alpha$} is $30^\circ$ in the
 
                         example ($\pi/6$ in radians). The {\red sine of
 
                         example ($\pi/6$ in radians). The {\red sine of
 
                         $\alpha$}, which is the height of the red line, is
 
                         $\alpha$}, which is the height of the red line, is
Line 57: Line 57:
  
 
\stoptext
 
\stoptext
</context>
+
</texcode>
 +
 
 +
[[Image:TikZ_example.png]]
 +
 
 +
== Placing TikZ graphics ==
 +
 
 +
You should wrap TikZ graphics inside \hbox when placing them, for example:
 +
 
 +
<texcode>
 +
 
 +
\placefigure[here][fig:myfig]{My figure.}{\hbox{\starttikzpicture
 +
\draw (0, 0) circle (3cm);
 +
\stoptikzpicture}}
 +
 
 +
</texcode>
 +
 
 +
Otherwise, positioning will not work (figure will not be centered). See [http://www.ntg.nl/pipermail/ntg-context/2007/023177.html this post] by Aditya Mahajan for explanation.
 +
 
 +
== Example for commutative diagrams ==
 +
 
 +
Hans Åberg wrote to the mailing list on 2016-10-06:
 +
 
 +
There is a tikz-cd package at CTAN: https://www.ctan.org/tex-archive/graphics/pgf/contrib/tikz-cd?lang=en
 +
 
 +
In order to use it, download the the file <code>tikzlibrarycd.code.tex</code> and put it in <code>CONTEXT_HOME/tex/texmf-modules/tex/generic/pgf/libraries/</code>. Then run
 +
<pre>
 +
mtxrun --generate
 +
context --make
 +
</pre>
 +
You should now be able to use tikz-cd.
 +
 
 +
Makes sure to follow the translation rules in the manual for ConTeXt. In addition to that, it seems that you have to replace the apostrophe with "below".
 +
 
 +
The first few examples in the manual, which compiled with latest ConTeXt:
 +
 
 +
<texcode>
 +
\usemodule[tikz]
 +
\usetikzlibrary[cd]
 +
 
 +
\starttext
 +
 
 +
\starttikzcd
 +
A \arrow{rd} \arrow{r}{𝜑} \NC B \NR
 +
                                \NC C
 +
\stoptikzcd
 +
 
 +
\starttikzcd
 +
A \arrow[r, "\phi"] \arrow[d, red]
 +
  \NC B \arrow[d, "\psi" red] \NR
 +
C \arrow[r, red, "\eta" blue]
 +
\NC D
 +
\stoptikzcd
 +
 
 +
\starttikzcd
 +
A \arrow[r, "\phi" near start, "\psi" below, "\eta" near end] \NC B
 +
\stoptikzcd
 +
 
 +
\starttikzcd
 +
T
 +
\arrow[drr, bend left, "x"]
 +
\arrow[ddr, bend right, "y"]
 +
\arrow[dr, dotted, "{(x,y)}" description] \NC \NC \NR
 +
  \NC X \times_Z Y \arrow[r, "p"] \arrow[d, "q"]
 +
  \NC X \arrow[d, "f"] \NR
 +
\NC Y \arrow[r, "g"] \NC Z
 +
\stoptikzcd
 +
 
 +
\starttikzcd[column sep=tiny]
 +
\NC \pi_1(U_1) \ar[dr] \ar[drr, "j_1", bend left=20]
 +
  \NC
 +
    \NC[1.5em] \NR
 +
\pi_1(U_1\cap U_2) \ar[ur, "i_1"] \ar[dr, "i_2" below]
 +
  \NC
 +
    \NC \pi_1(U_1) \ast_{ \pi_1(U_1\cap U_2)} \pi_1(U_2) \ar[r, dashed, "\simeq"]
 +
      \NC \pi_1(X) \NR
 +
\NC \pi_1(U_2) \ar[ur]\ar[urr, "j_2" below, bend right=20]
 +
  \NC
 +
    \NC
 +
\stoptikzcd
 +
 
 +
\stoptext
 +
</texcode>
  
 
== See also ==
 
== See also ==

Latest revision as of 19:58, 5 March 2019

< Modules | Graphics >

TikZ/pgf is a portable graphics package developed for LaTeX, also supporting ConTeXt. It is a possible alternative to MetaPost & MetaFun.

TikZ is included as a (third party) module for ConTeXt.

Example

From the TikZ/pgf manual.

\setupcolors[state=start]
\definecolor[darkgreen][g=.5]
\usemodule[tikz]         

\starttext

\starttikzpicture[      scale=3,line cap=round
                        axes/.style=,         
                        important line/.style={very thick},
                        information text/.style={rounded corners,fill=red!10,inner sep=1ex} ]

        \draw[help lines,step=0.5cm] (-1.4,-1.4) grid (1.4,1.4);
        \draw (0,0) circle (1cm);                               
                \draw[->] (-1.5,0) -- (1.5,0) node[right] {$x$} coordinate(x axis);
                \draw[->] (0,-1.5) -- (0,1.5) node[above] {$y$} coordinate(y axis);
                \foreach \x/\xtext in {-1, -.5/-\frac{1}{2}, 1}                    
                        \draw[xshift=\x cm] (0pt,1pt) -- (0pt,-1pt)                
                                node[below,fill=white] {$\xtext$};
                \foreach \y/\ytext in {-1, -.5/-\frac{1}{2}, .5/\frac{1}{2}, 1}
                        \draw[yshift=\y cm] (1pt,0pt) -- (-1pt,0pt)
                                node[left,fill=white] {$\ytext$};
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0pt) arc(0:30:3mm);
        \draw (15:2mm) node[green!50!black] {$\alpha$};
        \draw[important line,red]
                (30:1cm) -- node[left=1pt,fill=white] {$\sin \alpha$} (30:1cm |- x axis);
        \draw[important line,blue]
                (30:1cm |- x axis) -- node[below=2pt,fill=white] {$\cos \alpha$} (0,0);
        \draw[important line,orange!80!black] (1,0) -- node[right=1pt,fill=white] {
                $\displaystyle \tan \alpha \black =
                \frac{{\red \sin \alpha}}{\blue \cos \alpha}$}
                (intersection of 0,0--30:1cm and 1,0--1,1) coordinate (t);
        \draw (0,0) -- (t);
        \draw[xshift=1.85cm]
                node[right,text width=6cm,information text]
                {
                        The {\darkgreen angle $\alpha$} is $30^\circ$ in the
                        example ($\pi/6$ in radians). The {\red sine of
                        $\alpha$}, which is the height of the red line, is
                        \startformula
                        {\red \sin \alpha} = 1/2.
                        \stopformula
                        By the Theorem of Pythagoras ...
                };
\stoptikzpicture

\stoptext

TikZ example.png

Placing TikZ graphics

You should wrap TikZ graphics inside \hbox when placing them, for example:


\placefigure[here][fig:myfig]{My figure.}{\hbox{\starttikzpicture
	\draw (0, 0) circle (3cm);
\stoptikzpicture}}

Otherwise, positioning will not work (figure will not be centered). See this post by Aditya Mahajan for explanation.

Example for commutative diagrams

Hans Åberg wrote to the mailing list on 2016-10-06:

There is a tikz-cd package at CTAN: https://www.ctan.org/tex-archive/graphics/pgf/contrib/tikz-cd?lang=en

In order to use it, download the the file tikzlibrarycd.code.tex and put it in CONTEXT_HOME/tex/texmf-modules/tex/generic/pgf/libraries/. Then run

mtxrun --generate
context --make

You should now be able to use tikz-cd.

Makes sure to follow the translation rules in the manual for ConTeXt. In addition to that, it seems that you have to replace the apostrophe with "below".

The first few examples in the manual, which compiled with latest ConTeXt:

\usemodule[tikz]
\usetikzlibrary[cd]

\starttext

\starttikzcd
 A \arrow{rd} \arrow{r}{𝜑} \NC B \NR
                                \NC C
\stoptikzcd

\starttikzcd
 A \arrow[r, "\phi"] \arrow[d, red]
   \NC B \arrow[d, "\psi" red] \NR
 C \arrow[r, red, "\eta" blue]
 \NC D
\stoptikzcd

\starttikzcd
 A \arrow[r, "\phi" near start, "\psi" below, "\eta" near end] \NC B
\stoptikzcd

\starttikzcd
 T
 \arrow[drr, bend left, "x"]
 \arrow[ddr, bend right, "y"]
 \arrow[dr, dotted, "{(x,y)}" description] \NC \NC \NR
   \NC X \times_Z Y \arrow[r, "p"] \arrow[d, "q"]
   \NC X \arrow[d, "f"] \NR
 \NC Y \arrow[r, "g"] \NC Z
\stoptikzcd

\starttikzcd[column sep=tiny]
 \NC \pi_1(U_1) \ar[dr] \ar[drr, "j_1", bend left=20]
   \NC
     \NC[1.5em] \NR
 \pi_1(U_1\cap U_2) \ar[ur, "i_1"] \ar[dr, "i_2" below]
   \NC
     \NC \pi_1(U_1) \ast_{ \pi_1(U_1\cap U_2)} \pi_1(U_2) \ar[r, dashed, "\simeq"]
       \NC \pi_1(X) \NR
 \NC \pi_1(U_2) \ar[ur]\ar[urr, "j_2" below, bend right=20]
   \NC
     \NC
\stoptikzcd

\stoptext

See also

Discussion on the ConTeXt user's list.