Showing posts with label Indentation. Show all posts
Showing posts with label Indentation. Show all posts

Friday, June 3, 2011

Inverted pyramid typesetting

University thesis committees are fairly well-known in the typesetting world for having the most absurd requirements. One example requires heading text to be typeset centered and no more than 4.5 in wide in which the lines become progressively shorter.

This is stupid. Still, I've got a solution, based on egreg's:
\newcommand\stupid[1]{%
        \vbox{%
                \hsize=4.5in
                \parindent=0pt
                \leftskip=0pt plus.5fil
                \rightskip=0pt plus-0.5fil
                \parfillskip=0pt plus1fil
                \emergencystretch=1in
                \parshape6
                0.00in 4.50in
                0.25in 4.00in
                0.50in 3.50in
                0.75in 3.00in
                1.00in 2.50in
                1.25in 2.00in
                \huge
                \bfseries
                \strut
                #1%
        }%
}
The \parshape specifies what to do for the first 6 lines by giving pairs of numbers. The first in the pair is the indentation and the second is the line length. The settings of \leftskip, \rightskip, and \parfillskip come from TeX by Topic and are used to center the last line.

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.

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.