About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-10-15: New blog post: Tales for Our Times Book Launch.

Exercise 8: Writing a Letter using the scrlttr2 class (Solution)

These are solutions to Exercise 8. The basic solution is:
\documentclass[12pt,parskip=full]{scrlttr2}

\usepackage{graphicx}

\usepackage[british]{babel}

\KOMAoption{subject}{afteropening,right,underlined,titled}
\KOMAoptions{backaddress=false,foldmarks=false,fromalign=right,locfield=wide}

\setkomavar{signature}{Mr Big Head, Managing Director}
\setkomavar{subject}{A sample letter}
\setkomavar{fromaddress}{University of Somewhere\\Some City\\AB3 4YZ}
\setkomavar{fromurl}{www.somewhere.ac.uk}
\setkomavar{location}{Secret Lab of Experimental Stuff\\Gov.\ Grant No.~1234}
\setkomavar{fromphone}{0123456789}
\setkomavar{fromemail}{big.head@somewhere.ac.uk}

\KOMAoption{fromlogo}{true}
\setkomavar{fromlogo}{\includegraphics[width=2cm]{dummy-logo}}

\begin{document}

\begin{letter}{Mrs Mabel Canary\\24 The Street\\
Some Village\\Some Town\\Noshire\\AB1 2YZ}

  \setkomavar{myref}{ABC/123}
  \setkomavar{invoice}{123456}
  \setkomavar{customer}{2}

  \opening{Dear Mrs~Canary}

  This is an imaginary letter.

  This is the second paragraph of the letter.

  \closing{Yours sincerely}

  \ps PS: this is a postscript.

  \encl{Photocopy of something interesting\\
   Photocopy of something rather dull}

  \cc{Prof Important Person\\Dr Bor Ing}
\end{letter}

\end{document}
Download scrlttr2ex1a.tex or scrlttr2ex1a.pdf.

For the more adventurous section, you need the sample files people.csv and country-codes.csv. Alternatively, if you want to use data from the samples SQL database, remember to use datatooltk to import the data to a .dbtex file and load it using \DTLloaddbtex as described in Loading Data From a .dbtex File.

\documentclass[12pt,parskip=full]{scrlttr2}

\usepackage{graphicx}

\usepackage[british]{babel}

\usepackage{datatool}

\DTLloaddb{people}{people.csv}
\DTLloaddb{countries}{country-codes.csv}

\KOMAoption{subject}{afteropening,right,underlined,titled}
\KOMAoptions{backaddress=false,foldmarks=false,fromalign=right,locfield=wide}

\setkomavar{signature}{Mr Big Head, Managing Director}
\setkomavar{subject}{A sample letter}
\setkomavar{fromaddress}{University of Somewhere\\Some City\\AB3 4YZ}
\setkomavar{fromurl}{www.somewhere.ac.uk}
\setkomavar{location}{Secret Lab of Experimental Stuff\\Gov.\ Grant No.~1234}
\setkomavar{fromphone}{0123456789}
\setkomavar{fromemail}{big.head@somewhere.ac.uk}

\KOMAoption{fromlogo}{true}
\setkomavar{fromlogo}{\includegraphics[width=2cm]{dummy-logo}}

\begin{document}

\newcommand*{\Id}{2}% customer id

\xDTLassignfirstmatch{people}{id}{\Id}{%
  \Surname=surname,%
  \Forenames=forenames,%
  \Title=title,%
  \AddressI=address1,%
  \AddressII=address2,%
  \Town=town,%
  \County=county,%
  \Postcode=postcode,%
  \CountryCode=country%
}
\xDTLassignfirstmatch{countries}{code}{\CountryCode}{\CountryName=name}

\begin{letter}{%
 \DTLifnullorempty{\Title}{}{\Title\ }\Forenames\ \Surname\\%
 \AddressI\\
 \DTLifnullorempty{\AddressII}{}{\AddressII\\}%
 \Town\\%
 \DTLifnullorempty{\County}{}{\County\\}%
 \Postcode\\\CountryName}

  \setkomavar{myref}{ABC/123}
  \setkomavar{invoice}{123456}
  \setkomavar{customer}{\Id}

  \opening{Dear \DTLifnullorempty{\Title}{\Forenames}{\Title} \Surname}

  This is an imaginary letter.

  This is the second paragraph of the letter.

  \closing{Yours sincerely}

  \ps PS: this is a postscript.

  \encl{Photocopy of something interesting\\
   Photocopy of something rather dull}

  \cc{Prof Important Person\\Dr Bor Ing}
\end{letter}

\end{document}

Download scrlttr2ex1b.tex or scrlttr2ex1b.pdf.

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs