Friday, September 25, 2009

Numbering every paragraph

One occasionally finds cause to number every graf in a document. Well, I never have, but I've seen it done. There's a neat little trick that is mentioned in passing in an exercise in the TeXbook that easily enables this. This relies on a TeX primitive \everypar which is essentially a token register which is to say that it holds a list of tokens that can be used again and again. What exactly a token is is a topic for another post. When TeX starts a new paragraph (or more precisely, when it enters horizontal mode), it does two things. First, it inserts an empty box of width \parindent (this is the indentation that occurs at the start of every graf) and then it will process the tokens defined by \everypar before going on to process the rest of the tokens that make up the graf. The upshot of this is that we can cause TeX to do something at the start of every graf, but after it inserts the indentation glue. The way to use this is to doing something like the following.
\newcounter{grafcounter}
\setcounter{grafcounter}{0}
\everypar={\addtocounter{grafcounter}{1}%
        \arabic{grafcounter}. }
Thus, at the start of every graf, 1 will be added to our counter and then the value of the counter typeset as arabic numerals followed by a period and a space will be prepended each graf. This is close, but not exactly what we want. We'd really like the graf number to appear in the margin. To do this, we use \llap—which is supposed to be Left overLAP. The way this works is that the argument to \llap will be typeset overlapping anything to the left. In this case, we can change the \everypar line to be
\everypar={\addtocounter{grafcounter}{1}%
        \llap{\arabic{grafcounter}.\hskip2\parindent}}
which will cause the graf number to appear indented a single \parindent into the margin—one \parindent to cancel the initial indentation and then a second to indent left into the margin. If there are already tokens in \everypar, we might not want to lose them. In this case, we can use \expandafter to append tokens, similar to how we appended to a macro.
\everypar=\expandafter{\the\everypar
        \addtocounter{grafcounter}{1}%
        \llap{\arabic{grafcounter}.\hskip2\parindent}}
Of course, we might want the other tokens to follow our graf numbering. Prepending; however, is a topic for another day.

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.

Wednesday, September 23, 2009

Appending to a macro

On occasion, one wishes to modify an existing macro by appending additional text or macros. For a simple example, suppose we have a complicated macro \foo and what we'd like is to replace all instances of \foo by \foo bar&emdash;that is, the macro \foo followed by the three tokens "bar". Now, potentially, one could replace all instances of \foo with "\foo bar", but this isn't always feasible, especially if \foo is normally expanded by some other macro in a package or class file. A first attempt at a solution—which won't work— is to try
\def\foo{\foo bar}
This doesn't work for the obvious reason that while expanding \foo, it will expand \foo, which will expand \foo, and then that will expand... A second attempt which is more successful is to use \let along with a second, helper macro.
\let\fooHelper\foo
\def\foo{\fooHelper bar}
This works but seems inelegant in that you have to keep the \fooHelper macro around. Any changes made to it change \foo. What we really want is a way to expand \foo in the replacement text in our first attempt above. We can use the TeX primitive \expandafter for this. \expandafter is an expandable control sequence that causes the second macro that follows it to be expanded one "level." That is, \expandafter\a\b has the effect of expanding \b once and then expanding \a. For example
\def\a{A}
\def\b{\c}
\def\c{C}
\expandafter\a\b
has the following expansion:
  • \expandafter\a\b
  • \a\c
  • A\c
  • AC
Since \expandafter is itself expandable, multiple \expandafters can be used completely alter the order of expansion. For our purposes, we need
\expandafter\def\expandafter\foo\expandafter{\foo bar}
This will expand \foo in the replacement text before making the new definition of \foo. The LaTeX kernel provides a \g@addto@macro that takes two arguments and globally appends its second argument to the first argument. That is, \g@addto@macro\foo{bar} globally performs the assignment we did above. This has the drawback that it can't be used inside a group where we want the definition to be local to that group. In a later post, I'll show how to add replacement text to places other than at the end.

Tuesday, September 22, 2009

Reason for being and variable hanging indentation

People often ask me LaTeX questions. "How can I do X?" for any number of X. Some questions are easy, some are hard, and some seem to have little practical value. However, I occasionally find myself implementing the solution, giving the answer and then forgetting about it only to find someone else asking a similar question later. My purpose here is to collect some of these, or maybe just neat tricks I hear about, and record them. These are mostly for my own reference, but if anyone else can use them, well, good. Most of these will likely be horrible hacks to some degree or another, don't hold it against me. The first one I want to write about was a request for paragraphs where the first line is not indented and subsequent lines are indented to the first word. For example:
Here is the first line of the graf
     and here is the second
     and here is the third
and so forth. Personally, I think this would look horrible since multiple grafs would be indented differing amounts, but since this was the question, so be it! Here is my solution.
\def\whee#1 {%
  \noindent
  \setbox0\hbox{#1 }%
  \hangindent\wd0
  \hangafter1
  \box0
}%
\whee This is my first graf which as you can
see is not indented but all lines following the
first are indented to the ``is.''

\whee Subsequent grafs, however, will look
very strange since the hanging indentation
doesn't match the first graf.
(Feel free to copy and paste this into Troy Henderson's handy LaTeX Previewer.) The first thing to note is that I used \def rather than \newcommand. The reason for this is that I wanted the power of TeX's macro creation that isn't exposed by LaTeX. (In fact, I used other TeXisms and that's likely going to be the case in most of these posts.) In this case, I wanted the power of delimited parameters which Knuth describes on page 203 of the TeXbook and maybe I'll talk about in a later post. For our purposes here, the space after the #1 means that the \whee macro will take one argument that consists of all characters upto the first blank. Thus, in the first usage of \whee above, "This" is the argument to the macro. Next, I saved the argument to \whee (the first word of the graf) into a box, followed by a space, and set the hanging indentation to be the width of the box. The \hangafter control sequence says to use that indentation starting on line 1 and continuing until the end of the graf. Finally, the box is typeset to produce the first word followed by a space. There are two major flaws with this approach. The first flaw is as I pointed out in the example, it looks really awful. The second flaw is slightly more subtle. The width of the space typeset after the first word is the "natural" width without any stretching or shrinking to make the rest of the line look good. The result is that the spacing between the first and second word is not consistent with the rest of the graf. I don't know any good way to deal with the second problem. Rather than simply typesetting the box, it could be "unboxed" first which would allow the space to stretch or shrink, but then subsequent lines would not line up correctly with the second word.