## 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.