Using \expandafter for macro redefinitions
Sometimes I want to redefine a macro of a class or package but want to use its original definition. Instead of copy&paste one could use the original macro directly:
- come up with a new macro, ensure that it’s not defined yet,
- save the definition of the original macro to the new macro,
- redefine the original macro using the new macro with the saved original code,
I will give a very simple example for a demonstration. Today a LaTeX Community member said that he’s using the quote environment but wants the enclosed text to have a smaller size. A straightforward solution is to use \renewenvironment copying the original code, let’s say from article.cls, and to make a small modification:
\renewenvironment{quote} {\list{}{\rightmargin\leftmargin}% \item\relax\small} {\endlist} |
Just the \small was added. This solution needs the writer to examine the class source, that may also be changed later.
The second solution is the method I’ve described above:
\newcommand*\origquote{} \let\origquote\quote \renewcommand*\quote{\origquote\small} |
The \newcommand line may be omitted, I’ve just used it to ensure that an error would be raised if a macro with that name was already defined by somehing else.
The last code works like the first solution, but respects the original class code and it will also respect further changes of the class, therefore I’m preferring it.
But the third solution is similar, shorter and doesn’t need the creation of another macro. It’s doing the same job but uses the original macro directly, avoiding a recursion problem by using \expandafter:
\expandafter\def\expandafter\quote\expandafter{\quote\small} |
You can find the same method applied in my comment to a previous blog post.
If you want to know how expandafter works you will find explanations in TeX books and documentations. Further there’s an extensive tutorial by Stephan v. Bechtolsheim (TUGboat, Volume 9, 1988), old but still valid of course.
This topic was discussed in the LaTeX Community Forum and on Matheplanet.com.
Ubuntu 8.10 “Intrepid Ibex” Release Party
On October 30, 2008 the version 8.10 of Ubuntu Linux will be released. The user group Ubuntu Berlin is inviting to the Intrepid Ibex Release Party:
The party in the c-base Berlin will start on November 1, 2008, 4pm, admission will be free. There will be presentations showing new features of Ubuntu 8.10, its new network management, talks about first steps with Ubuntu and tuning the desktop. Of course I’ll be there too.
If you cannot come to Berlin but want to celebrate the Intrepid release too, have a look at the Intrepid release parties worldwide.
TikZ: chains with labeled edges
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:
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.
Vertical alignment of graphics
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.
Fancy chapter headings with TikZ
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:

See also pdf output of this test & demo file.
Later question on TeX.SX: Fancy chapter headings – perhaps you have an addition to that?
German LaTeX wiki launched
The German LaTeX site goLaTeX.de has opened its wiki, as stated by J. Aehling yesterday evening. LaTeX users are invited to contribute. The long-term objective is to provide a language reference, useful tips and docs and answers to frequently asked questions.
The wiki has been published under the terms of the GNU Free Documentation License. Its address:
An extension to amsmath matrix environments
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.
How to get rid of those beamer warnings
Many users of the beamer class are irritated by several beamer warnings at every compiler run that are not caused by themselves, I’m referring to the beamer version 3.07. Those warnings are not really important, but it’s a good habit to debug all warnings instead of just ignoring them, otherwise an important warning could easily be overlooked.
When I compile this really small example:
\documentclass{beamer} \begin{document} \begin{frame} Test \end{frame} \end{document} |
I’m getting 6 warnings, after the second compilation of course less, but these 4 warnings remain:
- Package pgf Warning: This package is obsolete and no longer needed on input line 13.
- Package hyperref Warning: Option `pdfpagelabels’ is turned off
(hyperref) because \thepage is undefined.
Hyperref stopped early - LaTeX Font Warning: Font shape `OT1/cmss/m/n’ in size <4> not available
(Font) size <5> substituted on input line 6. - LaTeX Font Warning: Size substitutions with differences
(Font) up to 1.0pt have occurred.
Let’s eliminate those warnings:
- beamer.cls is loading the obsolete package pgfbaseimage.sty that does nothing but loads pgfcore and prints out this warning. If you put a file with the same name pgfbaseimage.sty somewhere into your texmf directory (TEXMFHOME for example) or into the directory of your tex document containing just the line \RequirePackage{pgfcore} the warning will disappear.
- Set pdfpagelabels to false by yourself, by providing a beamer class option: hyperref={pdfpagelabels=false}
- beamerbasefont.sty defines the commands \Tiny and \TINY to choose very small font sizes. Redefine at least \Tiny or load a font providing that size, for instance Latin Modern.
- fixed by 3.
The new file:
\documentclass[hyperref={pdfpagelabels=false}]{beamer} \let\Tiny=\tiny \begin{document} \begin{frame} Test \end{frame} \end{document} |
will not cause warnings any more. Using those workarounds you won’t be annoyed by unnecessary warnings during development of presentations. Though the redefinition of \Tiny will fix it for Computer Modern fonts I recommend to consider to use Latin Modern instead:
\usepackage{lmodern} |
amsmath: cases and \arraystretch
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.
TeXnicCenter: new homepage design

The TeXnicCenter homepage has got a new design, as Sven announced today in the LaTeX Community Forum. Now it has a more modern look and provides better access to its contents.
TeXnicCenter is an integrated development environment for LaTeX, running on the Windows platform. It’s supporting both MiKTeX and TeX Live. The version 2.0 is now under development as you can read in the news section.
Software Freedom Day 2008

The Software Freedom Day is a worldwide celebration of Free and Open Source Software. The goal in this celebration is to educate the worldwide public about of the benefits of using high quality free and open source software in education, in government, at home and in business. The non-profit company Software Freedom International coordinates this event at a global level, volunteer teams around the world organize local SFD events. Already over 500 teams in over 90 countries all around the world have announced to celebrate the SFD.
One event will take place September 20th, 2008 in Berlin, supported by FSFE and FFII Berlin. You can find more details here: Team Freie Software Berlin.
Centering wide tables or figures
When you want to include an image or a table that’s wider than the text width, you will notice that even when \centering or the center-environment is used this wide object will not be centered in relation to the surrounding text. It will be placed at the left margin but go into the right margin. Its frequently requested that wide figures or tables should overlap at both sides in equal measure.
It can easily achieved by putting the table or picture inside a box, giving the box the width of the text, by the \makebox command. Here is a compilable example, where I’m centering a table having 1.5 times the width of the text:
\documentclass[a4paper,10pt]{article} \usepackage[english]{babel} \usepackage{blindtext} \usepackage{tabularx} \begin{document} \blindtext \bigskip \noindent\makebox[\textwidth]{% \begin{tabularx}{1.5\textwidth}{XX} \blindtext & \blindtext \end{tabularx}} \bigskip \blindtext \end{document} |
I’ve used \noindent to suppress the paragraph indentation, otherwise I would get an overfull \hbox. As you may notice there’s no \centering necessary because the width of the box equals \textwidth.
This topic was discussed in the LaTeX Community Forum, on CQF.info and on mrunix.de.



