TeXblog

 Typography with TeX and LaTeX

Archive for the 'pgf/TikZ' Category

More on TikZ tables

30 April 2010 by Stefan Kottwitz

At present I don’t have much time for the blog because of my work, but I keep on reading and answering questions and comments.

For instance since I’ve noticed a link by Uwe to a table related topic and because of Oliviers comment there I’ve added another comment to “Fancy tables with TikZ“. It shows a tikz-matrix based table, seperating the actual implementation from the table contents. A simple code like

\begin{mytable}{1.2cm}{2.4cm}{1.2cm}{0.6cm}{
   \head{Rank} & \head{Distribution} & \head{Hits} & \\
   1 & Ubuntu & 2114 & \down \\
   2 & Fedora & 1451 & \up   \\
             ...
  10 & Arch   &  625 & \down \\
}
\end{mytable}

produces the following output.

table with TikZ

Changing the style of tables in the whole document is easier when they are defined as new environments in the preamble. For instance the mytable environment could be implemented without TikZ using just the standard tabular environment without changing the usage of those tables. Likewise with \head - I avoid using \textbf directly. It allows to change the look of the headings in a consistent way by formatting logically.

Comments and suggestions are always welcome, perhaps following the original post “Fancy tables with TikZ“.

Category: pgf/TikZ, Graphics | 1 Comment »

Fancy tables with TikZ

26 August 2009 by Stefan Kottwitz

Today I wanted to create a table for a presentation and I remembered a suggestion made by Kjell Magne Fauske in comp.text.tex in August 2008, to use TikZ to create tables with rounded corners. I’ve investigated this idea further to achieve a colorful table layout in a shape that’s unusual for LaTeX. Here’s the result, displaying a ranking of Linux distributions produced by DistroWatch.com:

fancy tables with TikZ

Click on the image to see it bigger, for a complete view open the pdf presentation.

The table has been put into a TikZ node, both at the top and at the bottom of the table I’ve used rectangles with rounded corners and some shading. A third rectangle is used to overlay parts of the other two rectangles.

The complete source code:

\documentclass{beamer}
\usetheme{default}
\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\setbeamertemplate{background canvas}[vertical shading]%
  [top=blue!1,bottom=blue!30]
\setbeamertemplate{navigation symbols}{}
\newcommand*\up{\textcolor{green}{%
  \ensuremath{\blacktriangle}}}
\newcommand*\down{\textcolor{red}{%
  \ensuremath{\blacktriangledown}}}
\newcommand*\const{\textcolor{darkgray}%
  {\textbf{--}}}
\begin{document}
\begin{frame}[bg=lightgray]
\textbf{\Large Linux distribution ranking,
        26th August 2009}
\begin{center}
\begin{tikzpicture}
\node (tbl) {
\begin{tabularx}{.6\textwidth}{cXrcc}
\arrayrulecolor{purple}
\textbf{Rank} & \textbf{Distribution} &
  \textbf{Hits} & \\
1 & Ubuntu\rule{0pt}{2.5ex}  &  2114 & \down \\
\midrule
2 & Fedora & 1451 & \up \\
\midrule
3 & Mint & 1297 & \const \\
\midrule
4 & OpenSUSE & 1228 & \up \\
\midrule
5 & Debian & 910 & \down \\
\midrule
6 & Mandriva & 907 & \up \\
\midrule
7 & PCLinuxOS & 764 & \up \\
\midrule
8 & Puppy & 738 & \up \\
\midrule
9 & Sabayon & 671 & \up \\
\midrule
10 & Arch & 625 & \down \\[0.5ex]
\end{tabularx}};
\begin{pgfonlayer}{background}
\draw[rounded corners,top color=red,bottom color=black,
    draw=white] ($(tbl.north west)+(0.14,0)$)
    rectangle ($(tbl.north east)-(0.13,0.9)$);
\draw[rounded corners,top color=white,bottom color=black,
    middle color=red,draw=blue!20] ($(tbl.south west)
    +(0.12,0.5)$) rectangle ($(tbl.south east)-(0.12,0)$);
\draw[top color=blue!1,bottom color=blue!20,draw=white]
    ($(tbl.north east)-(0.13,0.6)$)
    rectangle ($(tbl.south west)+(0.13,0.2)$);
\end{pgfonlayer}
\end{tikzpicture}
\end{center}
\small
Data by DistroWatch.com, spanning over the last 6 months,
hits per day.
\end{frame}
\end{document}

It’s just a draft, some lengths have been adjusted to fit in order to demonstrate using TikZ with tables in a quick way.

Category: pgf/TikZ, Figures and Tables, Graphics | 13 Comments »

Tutorial: Commutative Diagrams using TikZ

4 February 2009 by Stefan Kottwitz

In previous posts I’ve shown some examples how the TikZ graphics package could be used to draw mathematical diagrams like exact sequences and commutative diagrams. Felix Lenders has written a tutorial called Commutative Diagrams using TikZ dealing with this subject and has published it today. In that paper he’s describing basic syntax, styles, arrows, curves, positioning issues and more. The tutorial contains several examples with the corresponding source code.

To read related blog posts have a look at the TikZ category.

Category: News, pgf/TikZ, Mathematics | 5 Comments »

Math formulas and arrows with TikZ

19 January 2009 by Stefan Kottwitz

In the Art of Problem Solving Forum somebody asked for help in creating a construction for proofs and demonstrations in Logic by LaTeX commands. Math expressions should be aligned, some connected by arrows going vertically and horizontally.
Regarding arrows I would use tikz and also its matrix library for math nodes. The nodes could be connected by -| or |- path construction operations, like

\draw (node1) -| (node2) |- (node3);

For creation of the example given in the thread on AoPS I’ve used a matrix of math nodes, some empty nodes for alignment and |- -| arrows, here’s the compilable example code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[every node/.style={anchor=west}]
  \matrix (m) [matrix of math nodes,
    nodes in empty cells]{
    \quad & 1.\quad (x)(Q\supset Fx) & \\
    & 2.\quad Q\supset Fx & \textrm{I,\textbf{UI}} & \\
    & 3.\quad Q \\
    & 4.\quad Fx & 2, 3, \textrm{ M.P.}\\
    & 5.\quad (x)Fx & 4, \textrm{ \textbf{UG}} \\
    & 6.\quad Q\supset(x)Fx & 3-5, \textrm{ C.P.} \\
    & 7.\quad \parbox[t]{2.9cm}{%
             $(x)(Q\supset(x)Fx)\supset$\\
             $[Q\supset(x)Fx]$} & 1-6, \textrm{ C.P.}\\};
  \draw[-stealth] (m-7-2.north east)
               -| (m-1-1.west) |- (m-1-2);
  \draw[-stealth] (m-6-2.north east)
               -| (m-3-1.east) |- (m-3-2);
\end{tikzpicture}
\end{document}

Output:

logic diagram with TikZ

Category: pgf/TikZ, Mathematics | No Comments »

TikZ: Commutative diagrams with crossing edges

7 January 2009 by Stefan Kottwitz

When one edge of a graph passes over or under another edge there may be a small gap needed in the drawing of one of those edges. An easy trick is to draw the upper edge two times: first with the background color and more thickness, afterwards normally, like
<node1> edge [-,line width=6pt,draw=white] <node2> edge <node2>
Here’s an example diagram used in the definition of the pull-back of a Banach bundle:

Tikz commutative diagram

The source code:

\begin{tikzpicture}
  \matrix (m) [matrix of math nodes, row sep=3em,
    column sep=3em]{
    & f^\ast E_V& & \vphantom{f^\ast}E_V \\
    f^\ast E & & \vphantom{f^\ast}E & \\
    & U & & V \\
    M & & N & \\};
  \path[-stealth]
    (m-1-2) edge (m-1-4) edge (m-2-1)
            edge [densely dotted] (m-3-2)
    (m-1-4) edge (m-3-4) edge (m-2-3)
    (m-2-1) edge [-,line width=6pt,draw=white] (m-2-3)
            edge (m-2-3) edge (m-4-1)
    (m-3-2) edge [densely dotted] (m-3-4)
            edge [densely dotted] (m-4-1)
    (m-4-1) edge (m-4-3)
    (m-3-4) edge (m-4-3)
    (m-2-3) edge [-,line width=6pt,draw=white] (m-4-3)
            edge (m-4-3);
\end{tikzpicture}

This topic was discussed on Matheplanet.com.

If you want to read more about commutative diagrams with TikZ have a look here: Chains with labeled edges.

Category: pgf/TikZ, Mathematics | 3 Comments »

TikZ: chains with labeled edges

19 October 2008 by Stefan Kottwitz

These days I wanted to include some commutative diagrams in a math text. There are already packages designed for this purpose, like amscd and xy-pic. I’ve used xy-pic before and didn’t like its usability and output much. The most recent documents I found on CTAN were dated 1999, many links on its homepage were dead, though xy-pic still works fine today, also with pdflatex. But I decided to use pgf/TikZ now because it can be used to create graphics in many different ways. For instance the beamer class is using pgf already, so why not use it also for math diagrams.

For writing exact sequences the chains library seemed very useful, but I missed the feature to label the edges of a chain. Just arrows weren’t enough, I needed to write maps over, under or just next to it.

I decided to make a workaround by modifying the join method of the chain library. Its syntax is join=with<node> by <options>, I’m changing the syntax to join={node[options] {label}}, this is the code to achieve the effect wanted:

\tikzset{join/.code=\tikzset{after node path={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[every join]#1(\tikzchaincurrent)\fi}}}

Some general settings before starting the diagrams:

\tikzset{>=stealth',every on chain/.append style={join},
         every join/.style={->}}

Now a short exact sequence can be written for example:

\begin{tikzpicture}[start chain] {
    \node[on chain] {$0$};
    \node[on chain] {$A$} ;
    \node[on chain, join={node[above]
          {$\scriptstyle\varphi$}}] {$B$};
    \node[on chain, join={node[above]
          {$\scriptstyle\psi$}}] {$C$};
    \node[on chain] {$0$}; }
\end{tikzpicture}

Output:

long exact sequence

For more complex diagrams you could use the matrix library to create matrices of math nodes, connecting the nodes by chains. Here’s the code for the Short 5-Lemma as example:

\begin{tikzpicture}
  \matrix (m) [matrix of math nodes, row sep=3em,
    column sep=3em]
    { 0 & A  & B  & C  & 0 \\
      0 & A' & B' & C' & 0 \\ };
  { [start chain] \chainin (m-1-1);
    \chainin (m-1-2);
    { [start branch=A] \chainin (m-2-2)
        [join={node[right] {$\scriptstyle\eta_1$}}];}
    \chainin (m-1-3) [join={node[above]
                      {$\scriptstyle\varphi$}}];
    { [start branch=B] \chainin (m-2-3)
        [join={node[right] {$\scriptstyle\eta_2$}}];}
    \chainin (m-1-4) [join={node[above]
                      {$\scriptstyle\psi$}}];
    { [start branch=C] \chainin (m-2-4)
        [join={node[right] {$\scriptstyle\eta_3$}}];}
    \chainin (m-1-5); }
  { [start chain] \chainin (m-2-1);
    \chainin (m-2-2);
    \chainin (m-2-3) [join={node[above]
                      {$\scriptstyle\varphi'$}}];
    \chainin (m-2-4) [join={node[above]
                      {$\scriptstyle\psi'$}}];
    \chainin (m-2-5); }
\end{tikzpicture}

Output:

long exact sequence

See full LaTeX source code.
During writing of this entry I’ve applied that modification also on CQF.info.

Category: pgf/TikZ, Mathematics | 11 Comments »

Fancy chapter headings with TikZ

11 October 2008 by Stefan Kottwitz

A question on LaTeX-Community.org inspired me to try pgf/TikZ together with titlesec to produce fancy chapter headings. The result is this small compilable demonstration example:

\documentclass[svgnames]{report}
\usepackage{tikz}
\usepackage{kpfonts}
\usepackage[explicit]{titlesec}
\newcommand*\chapterlabel{}
\titleformat{\chapter}
  {\gdef\chapterlabel{}
   \normalfont\sffamily\Huge\bfseries\scshape}
  {\gdef\chapterlabel{\thechapter\ }}{0pt}
  {\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=LightSkyBlue] (0,0) rectangle
          (\paperwidth,3cm);
        \node[anchor=east,xshift=.9\paperwidth,rectangle,
              rounded corners=20pt,inner sep=11pt,
              fill=MidnightBlue]
              {\color{white}\chapterlabel#1};
       \end{tikzpicture}
      };
   \end{tikzpicture}
  }
\titlespacing*{\chapter}{0pt}{50pt}{-60pt}
 
\begin{document}
\tableofcontents
\chapter{Introduction}
Text
\chapter{Main}
\section{Section}
Text
\begin{thebibliography}{99}
\bibitem{Test} test reference
\end{thebibliography}
\end{document}

Screenshot:

TikZ fancy chapter headings

See also pdf output of this test & demo file.

Category: pgf/TikZ, Sectioning, Layout | 49 Comments »

TikZ: shaded cube

10 August 2008 by Stefan Kottwitz

With just a few lines of code you can create a drawing of a cube with a 3D impression, though if it’s not really perspective or raytraced:

\begin{tikzpicture}[on grid]
  \shade[yslant=-0.5,right color=gray!10, left color=black!50]
    (0,0) rectangle +(3,3);
  \draw[yslant=-0.5] (0,0) grid (3,3);
  \shade[yslant=0.5,right color=gray!70,left color=gray!10]
    (3,-3) rectangle +(3,3);
  \draw[yslant=0.5] (3,-3) grid (6,0);
  \shade[yslant=0.5,xslant=-1,bottom color=gray!10,
    top color=black!80] (6,3) rectangle +(-3,-3);
  \draw[yslant=0.5,xslant=-1] (3,0) grid (6,3);
\end{tikzpicture}

This example uses the positioning tikz library.

Output screenshot:

pgf/TikZ 3D cube

By adding some slanted numbers we will get a Sudoku 3D cube:

pgf/TikZ sudoku 3D

TeX source code and pdf output are provided.

I’ve programmed it to answer a question posted on matheplanet.de. At first I wanted to use the matrix tikz library but it seemed to me that matrices of nodes cannot be slanted, at least I didn’t find a way.

Category: pgf/TikZ | 4 Comments »

pgf/TikZ 3D drawings

7 August 2008 by Stefan Kottwitz

Today Tomek posted very impressive 3D drawings made with pgf/TikZ in the LaTeX Community Forum.

Here’s one of the drawings showing a cylindrical projection:

pgf/TikZ 3D example

More examples and their complete sourcecode by Tomasz M. Trzeciak can be found in the corresponding posting in the LaTeX Community Forum.

For even more examples demonstrating the capabilities of pgf/TikZ visit the TikZ example gallery provided by Kjell Magne Fauske.

Category: pgf/TikZ, Mathematics | 2 Comments »

New TikZ package: tikz-inet

18 June 2008 by Stefan Kottwitz

The today released package tikz-inet extends TikZ with commands for drawing interaction nets. At least Version 2.0 of pgf/TikZ is required.

For imformation about interaction nets see “Interaction Nets” by Y.Lafont, in POPL’90, 95-108 or for example the Diploma of Simon Gay, 1991.

Here is a small code example similar to one given in the documentation:

\documentclass[a4paper,10pt]{article}
\usepackage[fancy]{tikz-inet}
\renewcommand*\inetcolor{blue!25}
\begin{document}
\begin{tikzpicture}
\inetcell{A}
\inetprombox{(A)}(pa)
\inetcell[at=(bpa.east),right=5pt]{B}
\inetwire(B.middle pax)(A.middle pax)
\inetprombox{(bpa)(pa)(B)}(p)
\inetwire(A.pal)(pa.middle pax)
\inetwirefree(pa.pal)
\inetwirefree(p.pal)
\inetwire(B.pal)(p.middle pax)
\end{tikzpicture}
\end{document}

A screenshot of the output:

tikz interaction nets

Category: pgf/TikZ | No Comments »