Tuesday, September 21, 2010

LaTeX's architecture

Someone asked about the architecture of LaTeX. After avoiding the question for a day, I decided to answer. Since it's possible the question will be closed and possibly deleted, I figured I'd reproduce the relevant portion of my answer here. Briefly, LaTeX consists of several layers. The lowest layer is the three hundred or so TeX primitives such as \hbox or \vskip. On top of this, there is the LaTeX kernel. This is a set of macros such as \documentclass, \usepackage, or \begin. The documented source of the kernel is documented in the source2e document. Each document written in LaTeX begins (more or less) with \documentclass{foo} which loads the file foo.cls, for example article.cls or book.cls. This file is another set of macros, this time written using a combination of TeX primitives and macros from the LaTeX kernel. These class files can also load another class to extend it or modify the macros from that class. It can also load packages which are up next. LaTeX packages—that is, files ending in .sty (and their related files)—are additional sets of macros written using any combination of macros from lower levels. Some are specific to particular document classes, others are more general. These packages can be loaded using \usepackage from user documents, or \RequirePackage from classes and other packages. At the highest level, the users' documents use a class file and optionally packages. These documents contain macros written using any combination of TeX primitives and class- and package-defined macros as well as the prose, verse, and mathematics to be typeset. There is a lot more detail that one could go into regarding things like fonts and class and package options, but this isn't so "brief" after all.

Wednesday, September 8, 2010

Private bibliographies

I have two versions of my cv: one for public consumption and one which contains extra private bibliography entries for work that is in submission. Obviously these are mostly the same so I had one one that had a \newif\ifpublic and then later included the commands to for the private bibliography entries \unless\ifpublic ... \fi. Of course, the source still leaked information about the papers in submission which made it so that I could not distribute the source. The solution to this problem lies with \InputIfFileExists. So, for example, one can write
\InputIfFileExists{private}{}{}
which will \input{private} if private.tex exists. This is pretty good except that now we have to have 2 additional files, both private.tex and the private bibliography file, call it private.bib. Here, we can use a feature of BibTeX to reduce the number of extra files. Since BibTeX ignores all text outside of @{...} and TeX ignores everything in a file after \endinput, we can have the necessary LaTeX code for including the private bibliography at the start of private.bib and end the code with \endinput before the BibTeX entries begin. Then the \InputIfFileExists can be changed to
\InputIfFileExists{private.bib}
Now the source for the cv can be distributed. If a public version of the cv needs to be generated, then one can insert another \endinput at the start of private.bib or temporarily move private.bib aside while building.

Saturday, September 4, 2010

Computing the maximum width of two boxes

On occasion, one has cause to compare two boxes and compute the maximum of the two widths. If the two boxes are 0 and 2, then the standard way to do that is something like
\ifdim\wd0>\wd2
    \dimen0 \wd0
\else
    \dimen0 \wd2
\fi
Of course, this works perfectly well. But sometimes, you want something that's a little less easy to read. Thus, I propose
\dimen0 \wd\ifdim\wd0>\wd2 0 \else2 \fi
The spaces around the numbers are to keep TeX from reading more input looking for additional digits.

Friday, August 27, 2010

http://tex.stackexchange.com/

New stack exchange site for TeX and LaTeX. I've probably spent too much time there. I've been meaning to write more here...and I will soon.

Wednesday, May 12, 2010

BibTeX hacking

I've avoided editing bst files but I finally encountered something that I felt needed to change. When abbreviating the name Foo Bar Baz, the abbrv bibliography style produces
F.~B.~Baz
which has too much space between the F and the B for my tastes. After making a copy of abbrv.bst (and renaming it), we can change the argument to the format.name$ function (see Section 18) from "{f.~}{vv~}{ll}{, jj}" to "{f{.\nobreak\,}.~}{vv~}{, jj}" to produce a nonbreaking thin space between the F. and the B. I hope I never again have to edit a a bst.

Sunday, May 9, 2010

Including overly large graphics

Sometimes a graphic (or table or what have you) is simply too large to fit on the page without extending into the margins. Here's a simple way to center the graphic in the page without getting any Overfull hbox warnings.
\par
\hbox to\textwidth{\hss\includegraphics{foo}\hss}%
\par
The two \par aren't really essential. They're just to ensure the graphic is in a paragraph by itself. Replace \includegraphics with any other centered material. Note that this actually causes the graphic to extend an equal amount into the margins. If the margins aren't the same size, this is not centering on the page, but it's probably what you want anyway.

Monday, April 5, 2010

Ordinal numbers

Ever want TeX to write an ordinal number correctly? There are a number of fairly complicated solutions on the web so I decided to write one that was easy to read and follow.
\def\ord#1{\begingroup
        \count@=#1\relax
        \the\count@
        \ifnum\count@>99
                \count0\count@
                \divide\count0 by100
                \multiply\count0 by100
                \advance\count@ by-\count0
        \fi
        \ifnum\count@>19
                \count0\count@
                \divide\count0 by10
                \multiply\count0 by10
                \advance\count@ by-\count0
        \fi
        \ifcase\count@ th%
                \or st%
                \or nd%
                \or rd%
                \else th%
        \fi
\endgroup}
Note that this requires @ to be a letter, so \makeatletter or \catcode`@11. The first \ifnum block mods out by 100 since that plays no role in determining the ordinal suffix. The next block mods out by 10 if \count@ is at least 20. This is because 10th through 19th use th but 20th through 99th only examine the ones digit. Lastly, if the one's digit is 0, 4, 5, 6, 7, 8, or 9, (or the tens digit is 1) use th, otherwise use st, nd, or rd as appropriate. Using old-style superscripts requires a trivial modification.