Showing posts with label best practices. Show all posts
Showing posts with label best practices. Show all posts

Friday, October 16, 2009

A quick interlude into fonts

Dealing with fonts in LaTeX is one of the hardest aspects of using it. Fortunately, if one does not want to use Knuth's Computer Modern fonts, there is a very simple way to change font families. Full details are here, but for a publication that requires a Times roman typeface, one should use
\usepackage{mathptmx}
\usepackage[scaled=.92]{helvet}
\usepackage{courier}
which causes the roman and math fonts to be Times, the sans serif fonts to be in Helvetica—scaled so that it matches the other fonts better—and courier for the typewriter family. These three look nice together. In addition, output font encoding can be changed to T1 with
\usepackage[T1]{fontenc}
which is recommended. For more details see the above link

Monday, October 5, 2009

Defining new math operators

Defining a new math operator that behaves similar to \sin or \lim is very easy to do using the amsmath package. It provides a \DeclareMathOperator macro that works in the preamble to declare a new operator. It also contains a starred version that behaves similar to \lim with respect to subscripts. For example:
\DeclareMathOperator\arcsec{arcsec}
\DeclareMathOperator*\Lim{Lim}
In addition, \operatorname or \operatorname* can be used for one-time uses that don't warrant defining a new control sequence for the operator name. These are better than using \mathrm to define operator names if for no other reason than spacing is handled correctly in the presence or absence of parentheses.

Friday, October 2, 2009

Getting publication quality tables is easy

One of the problems with reading about various aspects of typography is I start to see the short comings in other's work, and far more importantly, in my own. Creating tables is one area where this is certainly true. In my experience, nearly every document prepared with LaTeX that contains a table, contains an ugly table. I'm not sure what the reason for this is, but everybody seems to want to make tables that look like the following.
\begin{tabular}{|c|c|c|}
\hline
A & B & C\\
\hline\hline
foo & bar & baz\\
\hline
zab & rab & oof\\
\hline
\end{tabular}
(As usual, try this out here.) There's no reason at all for each cell to be boxed that way. I rather suspect this comes from looking at too many ugly HTML tables one gets by default. Fortunately, the solution is very simple. Use the booktabs package. I strongly encourage anyone writing a table to read the documentation (pdf). The use is very simple.
\begin{tabular}{ccc}
\toprule
A & B & C\\
\midrule
foo & bar & baz\\
zab & rab & oof\\
\bottomrule
\end{tabular}
(You'll need to select the booktabs package in the previewer above to try this out.) Notice that in addition to looking better, this actually requires less work to produce! There is a \cmidrule that works similar to \cline, but is more flexible and can actually be used in adjacent columns; however, I don't often find a use for any rules but the three above. Finally, the author of booktabs gives 2 guidelines for making publication quality tables. 1) Do not use vertical rules; and 2) Do not use double rules. These are excellent guidelines. Follow them.

Thursday, September 24, 2009

Floating figures

One of the most frequently asked questions about LaTeX seems to be: How does one put a figures right here? The answer is, of course, very simple: Don't put material you don't want to float in a floating environment. This is the LaTeX equivalent of, "Doctor, it hurts when I do this." Shortly after giving this answer, the questioner usually responds that she had no idea that \includegraphics—you are using \includegraphics for including your graphics, aren't you?—could be used outside of the figure environment. There is nothing special about \includegraphics from TeX's point of view: It simply creates a box, and everything is a box (unless it's glue, or a penalty, or a whatsit, or a...). Want to stick a graphic right here? No problem, \includegraphics{foo} will insert foo.eps if you're running latex or foo.{jpg,png,pdf} if you're running pdflatex. Well that's simple, but something's missing. In fact, two things are missing. First, one cannot simply use \caption to produce a caption for this graphic. Second, one cannot reference this graphic using \label and \ref since this requires, among other things, a \caption. Here's the metric I use when deciding whether to make a graphic floating or not. If I want to give it a caption or reference it in text, then I make it float and give it a caption. If I don't reference it in text and it has no caption, then I might not let it float since it might seem out of place if floating. Of course, there are very few reasons why one might want a graphic in a scholarly publication that isn't referenced. This is a fairly simple rule to follow and solves the problem. At this point, there are two things I feel I should mention. The first is that there is a package that will allow one to place a figure environment right here; however, using that violates my rule, so I'll say nothing more about it. The second thing to mention is that LaTeX2e has a bug in its figure placement algorithm when using the twocolumn documentclass option (at least for the standard classes) and the figure* environment. This bug can cause two column figures to be placed out of order with respect to single column figures. The fix for this is very simple: place \usepackage{fixltx2e} into the preamble of your document (between \documentclass and \begin{document}). Actually, the package does more than just fix that bug and should probably be used in every LaTeX document.