TeXblog

 Typography with TeX and LaTeX

Archive for the 'Graphics' Category

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 long 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: Graphics, Mathematics | 4 Comments »

Vertical alignment of graphics

18 October 2008 by Stefan Kottwitz

If graphics or graphics and text had to be set side-by-side they would often be placed inside minipage environments. This usually works fine if the minipages should be bottom-aligned. A frequently asked question is how to get top alignment, like here on mrunix.de and here on matheplanet.com. A first approach could be to use t as optional positioning argument of the minipage environment. Here’s a compilable example, showing that graphics would stay bottom-aligned that way, used for a fix:

\documentclass{article}
\usepackage[demo]{graphicx}
\begin{document}
\begin{figure}[ht]
  \fbox{\begin{minipage}[t]{150pt}
    \includegraphics[height=100pt,width=150pt]{test}
  \end{minipage}}
  \hfill
  \fbox{\begin{minipage}[t]{150pt}
    \includegraphics[height=60pt,width=150pt]{test}
  \end{minipage}}
\end{figure}
\end{document}

I’ve used \fbox additionally to visualize the minipages. You may notice that it’s compilable even without the image file, that’s achieved by using demo as option for graphicx.

The reason is that \includegraphics sets the baseline to the bottom. A quick fix is just to insert \vspace{0pt} right before each \includegraphics command, this way the reference point will be the top and both images will be placed top-aligned now.

You could use the same approach if you want to place graphics inside \parbox or inside table cells top-aligned.

Category: Graphics | 1 Comment »

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: Sectioning, Graphics, Layout | 11 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: Graphics | 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: Graphics, Mathematics | 2 Comments »

Jpgfdraw example

7 August 2008 by Stefan Kottwitz

Here’s an example illustration made with Jpgfdraw following a drawing in Singer/Thorpe Lecture Notes in Elementary Topology and Geometry, I made freehand it to illustrate homotopy of paths in my notes.

Screenshot:

Jpgfdraw example homotopy

Output in pdf format: homotopy.pdf
Jpgfdraw binary file: homotopy.jdr
exported tex file: homotopy.tex

LaTeX main document:

\documentclass[a4paper,10pt]{article}
\usepackage[landscape]{geometry}
\usepackage{amsmath,pgf}
\pagestyle{empty}
\begin{document}
\centering
\input{homotopy}
\end{document}

Category: Tools for LaTeX, IDEs and Editors, Graphics, Mathematics | No Comments »