[Tex/LaTex] How to set scrlttr2 refvpos automatically based on address length

koma-scriptlengthsletters

I am creating an letter options (lco) file that I will use for multiple letters. The scrlttr2 class is designed for a fixed placement of the address field, and places the date at a fixed position from the top of the page (refvpos). I would like to change this so that refvpos is automatically adjusted based on the height of the address. This example results in the date and opening of the letter colliding with the address:

\documentclass[paper=letter]{scrlttr2}
\usepackage[american]{babel}
\areaset{6.5in}{9in}

\KOMAoptions{parskip=full,refline=dateleft,fromalign=right}

\makeatletter
\@setplength{toaddrvpos}{0.5in}
\@setplength{refvpos}{1.5in}
\makeatother

\date{16 February 2012}

\begin{document}
\begin{letter}{Dr.~Leo Lion\\Fordstan University\\Room 50\\1600 Pennsylvania Ave\\Somewhere\\United States of America}
\opening{Dear Sir,}
This is a test.
\closing{Sincerely yours}
\end{letter}
\end{document}

Can I fix this so that the date will always be one \baselineskip after the address, regardless of whether the address is two lines or ten?

Best Answer

You could redefine the letter command like this:

\let\oldletter\letter
\renewcommand\letter[1]{%
  \newdimen\height%
  \setbox0=\vbox{#1}%
  \height=\ht0 \advance\height by \dp0%
  \@setplength{toaddrheight}{\height}%
  \@setplength{refvpos}{\useplength{toaddrvpos}}%
  \@addtoplength{refvpos}{\useplength{toaddrheight}}%
  \@addtoplength{refvpos}{2\baselineskip}%
  \oldletter{#1}%
}

It places the address in a vbox, calculates its height, sets the height of the address field and finally calculates the resulting refvpos as a sum of toaddrvpos+toaddrheight+2baselineskip.

Hope it helps.