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:
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:
See full LaTeX source code.
During writing of this entry I’ve applied that modification also on CQF.info.
Category: Graphics, Mathematics |
4 Comments »
30 September 2008 by Stefan Kottwitz
Inspired by a question on matheplanet.com and remembering the cases redefinition I’ve shown some days ago I got the idea to extend the internal macro \env@matrix of amsmath.sty. I wanted to use the matrix environments together with array features like alignment, vertical lines, formatting and special commands.
The mathtools package provides something similar by its starred matrix environments that support one optional parameter that will be applied to all matrix columns. The following redefinition will introduce an optional parameter to amsmath array environments that allows column-specific customization:
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{#1}}
\makeatother
If you put these lines into your document preamble the pmatrix, bmatrix, vmatrix, Bmatrix, Vmatrix etc. environments will accept an optional parameter. If you don’t provide this parameter those environments will work like usual. Here’s one example showing an augmented matrix containing a vertical line:
\[
\begin{pmatrix}[cc|c]
1 & 2 & 3\\
4 & 5 & 9
\end{pmatrix}
\]
Another more complex example just showing some array features like different alignment because of the signs, color change and bold font:
\[
\begin{bmatrix}[*2cr@{\quad}|@{\quad}>{\bf\color{red}}r]
a & b & 1 & 4 \\
c & d & -2 & -3
\end{bmatrix}
\]
Though \bf is an obsolete font command standard classes still support it and I’ve just used it because \boldmath is invalid in math mode, in general I advice against using \bf.
Category: Mathematics |
No Comments »
24 September 2008 by Stefan Kottwitz
The amsmath cases environment is using the array environment internally, like its matrix environments. If you want to change the interline spacing of matrices you could redefine \arraystretch, like for any array environment. But it won’t work for cases - amsmath defines an arraystretch value of 1.2 internally.
A solution is to redefine the \env@cases macro. Here’s a redefinition, introducing an optional parameter controlling the spacing:
\makeatletter
\renewcommand*\env@cases[1][1.2]{%
\let\@ifnextchar\new@ifnextchar
\left\lbrace
\def\arraystretch{#1}%
\array{@{}l@{\quad}l@{}}%
}
\makeatother
Now by \begin{cases} … \end{cases} the default value of 1.2 will be used, but by using the optional parameter like \begin{cases}[0.8] … \end{cases} the spacing will be adjusted accordingly.
This topic was discussed on mrunix.de.
Category: Mathematics |
2 Comments »
16 September 2008 by Stefan Kottwitz
Today it has been announced that a new paper “Writing scientific documents using LaTeX” by Andrew J. Bennieston has been uploaded to CTAN, an introduction on 13 pages. I took a brief glimpse at this paper.
Nice to have another introduction, but some odd things catched my eye. The author introduces $$…$$ as shortcut form for display math mode without equation numbering, but \[…\] is recommended instead of this, like mentioned in “Obsolete packages and commands“, $$…$$ should be avoided. The author describes also the obsolete math environment eqnarray. Though he is mentioning that the align environment should be preferred he didn’t say why. As one can see in the output of the equations (2), (3) and (4) there’s something wrong with the spacing next to the relation symbol, the author just didn’t use eqnarray correctly, there’s a & separation symbol missing on each line. It’s compilable, but if you extend the right side then the = symbols will not be aligned any more, compare the syntax with this reference. The right sides of those equations including the = symbol are actually centered, not noticeable because they are having equal width.
This article contains also a wrong syntax of the align environment. The demonstration example doesn’t contain any & separation symbol, the alignment is just a coincidence.
I’m referring to the fourth Edition of September 13, 2008, uploaded to CTAN today. I’m expecting that the mistakes will be corrected soon, of course I will send the author an email containing my comments.
Category: LaTeX General, Mathematics |
No Comments »
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:
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 »
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:
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 »