# Changing options based on generated document length

I am creating lots of letters using the scrlttr2 package and some variables that change per letter. The contents of these variables might cause the letter to take up an additional page that I would like to avoid. For example, some people have addresses that take up more lines than others, but there are other variables that can cause this to happen as well. When I have room, I want to set \KOMAoptions{parskip=full} because it looks nicer, but if I'm out of space I would like to set \KOMAoptions{parindent}. Is there a good way to do this automatically? Here are some options I thought of:

1. Define a command with the body of the letter. Use \KOMAoptions{parskip=full} and \savebox with the address and the body command to get its height. If the height is over a certain amount, then set \KOMAoptions{parindent}. Then use the body command again to output the actual letter. I'm not sure how to get the height and switch on it, and defining the whole letter and its included environments in a single command sounds clunky.

2. Make two versions of the document. In my Makefile, somehow identify the length in pages (how?) of the parskip version and substitute the parindent version if necessary

3. Just set another variable to define this, and set it manually for each letter (this is what I am doing now).

Other ideas welcome.

-
If it goes over 2 pages and switching to parindent does not make it one page, do you want to stick with parskip=full? –  StrongBad Oct 3 '12 at 17:39
If it still goes over two pages, I am going to remove some text, but if it's easier we can assume that's not going to happen. –  Michael Hoffman Oct 3 '12 at 17:45

This can be done by using the same strategy I used here. It has the same dependence on nothing screwing up \AtEndDocument.

\documentclass{scrlttr2}
\usepackage[english]{babel}
\usepackage{lipsum}

\makeatletter
\AtEndDocument{%
\ifnum\value{page} = 1%
\immediate\write\@auxout{\global\let\noexpand\@singlepage\noexpand\@empty}%
\fi%
}
\newcommand{\oneormorepages}{\ifdefined\@singlepage\KOMAoptions{parskip=full}\fi}
\makeatother

\begin{document}
\oneormorepages
\lipsum[1-5]
\end{document}

-
Thanks, brilliant answer. For my scrlttr2 document, \value{page} called within \AtEndDocument always seems to be one more than the number of pages produced, so I had to change the code to \value{page} < 3. –  Michael Hoffman Oct 4 '12 at 13:38
I occasionally get that problem when completely filling up the previous page in documentclasses such as article, even when I don't take up any space in the next page. Is there a way to see whether any text exists on page two at all? –  Michael Hoffman Oct 26 '12 at 3:11