TeX Snippets

This page contains small snippets of TeX and LaTeX code that I use regularly. They are so small that they don't warrant a distribution of their own.

Draft and Final Version

You might want to know about overfull boxes while you are preparing a paper but don't want to point your readers to the remaining ones once you submitted a paper. For such problems create a new \ifdraft conditional and then use \ifdraft ... \fi to enclose material that should not be processed in your final version. Use the code and example below.

\newif\ifdraft\drafttrue

\ifdraft
\overfullrule3pt
\fi

PDF Paper Size

A document created with pdflatex sometimes is not declaring the right paper size. The reason is that many standard document classes are not set up to tell pdflatex the paper size. This is especially a problem with TeX installations on systems where DIN A4 is the default paper size. To correct the problem, use the following code in your preamble.

\ifx\pdfoutput\undefined
\else
    \pdfpagewidth=8.5in
    \pdfpageheight=11in
\fi

Access to Subversion $Id$

It's a good idea to put papers under version control. To get access of the current revision of the paper, you can put the following code into the preamble of a document.

\def\svn$I#1: #2 #3 #4 #5 #6 ${\def\svnrev{#3}\def\svndate{#4}\def\svndoc{#2}}
\svn$Id: draft.tex 122 2007-02-28 08:58:45Z lindig $

The current revision is now available as \svnrev, for example. This macro only works when $Id$ is already expanded by Subversion, like in the code above. It does not work with \svn$Id$, that is, before $Id$ is expanded.

Hyphenation without a Hyphen

Sometimes I want to hyphenate an overlong word but don't wont a hyphen to mark this. The code below introduces \+ as an alternative to the standard \-.

\def\+{\discretionary{}{}{}}

Macro for Inline Code

When illustrating programming problems, I like to use \texttt for identifiers. To make this a little easier I'm using a macro:

\def\<#1>{\texttt{#1}}

Now I can write \<some code here>, which feels more natural in the editor than \texttt{some code here}.

Margin Notes

By putting notes about a document into the margin, a possibly delicate layout is not altered by such notes. I'm using the following macros:

\let\mymarginpar\marginpar
% \documentclass{} goes here
\marginparwidth=1cm
\marginparsep=5pt
\newcommand\remark[1]{%
    \mymarginpar{\raggedright\hbadness=10000\tiny\it #1\par}}

Some document classes modify \marginpar. The solution is to load them (using \usepackage) after the \let\mymarginpar\marginpar because this saves the original \marginpar in \mymarginpar. A remark is simply entered as \remark{I have discovered a truly remarkable proof which this margin is too small to contain}.

Bars in TeX

Bar Graph

When you have lots of numbers it is often better to show them as a diagram rather than the exact numerical values. Above is a table where we used TeX to produce these bars. The length of these pairs of bars is controlled by two numbers in the range 0..1.

Aelfred &\rr{1.00}{1.00}& \r{0.13}{0.08}& ... \\

There are two macros, \rr and \r. Macro \rr uses a white line to separate the upper and the lower bar, where \r uses a black line.

\newdimen\qd\qd=1cm % length of bar for 1.0
\def\r#1#2{\leavevmode\hbox to 1.2cm{\hfil\vbox{%
    \hbox{\vrule\vbox{\hrule\hbox to 1\qd
            {\vrule depth0pt height0.7ex width #1\qd\hfill}\hrule}\vrule}
    \nointerlineskip%
    \hbox{\vrule\vbox{\hbox to 1\qd
            {\vrule depth0pt height0.7ex width #2\qd\hfill}\hrule}\vrule}}\hfil}}
\def\rr#1#2{\leavevmode\hbox to 1.2cm{\hfil\vbox{%
    \hbox{\vrule\vbox{\hrule\hbox to 1\qd
            {\vrule depth0pt height0.7ex width #1\qd\hfill}\vskip0.4pt}\vrule}
    \nointerlineskip%
    \hbox{\vrule\vbox{\hbox to 1\qd
            {\vrule depth0pt height0.7ex width #2\qd\hfill}\hrule}\vrule}}\hfil}}

You will probably have to adjust these to your own needs but this should give you a starting point.

Beautiful Tables

Use the Booktabs package for tables and make sure you read the documentation about table design.

Table

Below is a table that uses a trick to line up numbers neatly in a column. Columns with numbers are aligned centered (c) but I want numbers within such column to be right-aligned. The trick is to make the underscore (_) stand for an invisible 0. I'm using this to pad all numbers in a column to the same width.

You could any other character as well, like ?, when you need the underscore for math.

\begin{table}
    \centering
    \catcode`_=\active
    \def_{\phantom0}
    \begin{tabular}{lccccc}
    \toprule
    Project         & Count & Width & \#  & Violators & Gap \\
    \midrule                                          
    Ruby 1.8.4      & 143   & 2.67  & 39  & 1.49    & 2.26  \\
    Linux 2.6.0     & 112   & 2.52  & 19  & 1.21    & 1.05  \\
    Python 2.4.3    & 163   & 2.32  & _8  & 1.00    & 1.62  \\
    Lua 5.1         & __5   & 2.00  & _0  & 0.00    & 0.00  \\
    Apache 2.2.2    & _25   & 2.08  & _1  & 1.00    & 1.00  \\
    \bottomrule
    \end{tabular}
    \caption{\label{tab:violations} Patterns and 
        violations in the call relation of C Programs.}
\end{table}