Add Terje Larsen
[worg.git] / exporters / koma-letter-export.org
blob54794ee259bc906e969fd5f5c73ebb9ce1a0874e
1 #+OPTIONS:    H:3 num:nil toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t TeX:t LaTeX:t skip:nil d:(HIDE) tags:not-in-toc todo:nil
2 #+STARTUP:    align fold nodlcheck hidestars oddeven lognotestate
3 #+SEQ_TODO:   TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
4 #+TAGS:       Write(w) Update(u) Fix(f) Check(c)
5 #+TITLE:      Creating letters with KOMA-Script =scrlttr2= and Org-mode
6 #+AUTHOR:     Viktor Rosenfeld and Rasmus Pank Roulund
7 #+EMAIL:      v.rosenfeld@gmx.de
8 #+LANGUAGE:   en
9 #+PRIORITIES: A C B
10 #+CATEGORY:   worg
12 This tutorial describes the necessary steps to produce beautiful
13 letters using the Org-mode LaTeX exporter and the KOMA-Script
14 =scrlttr2= letter class.
16 * DONE Quick start guide
18 ** DONE Requirements
20 The code in this tutorial depends on the following:
22 - org-mode version 8.0 or greater (part of Emacs 24.4);
23 - a LaTeX installation including [[http://www.ctan.org/pkg/koma-script][KOMA-Script]], e.g., [[http://www.tug.org/texlive/][TeX Live]].
25 *Note*: The information in this tutorial depends on functionality that
26 has not yet been released and it not part of Emacs. You can either [[https://orgmode.org/worg/org-faq.html#keeping-current-with-Org-mode-development][use
27 the latest development version from the Org-mode git repository]] or
28 [[https://code.orgmode.org/bzg/org-mode/raw/master/contrib/lisp/ox-koma-letter.el][download a current version of the KOMA-Script letter exporter]] and
29 install it in your =load-path=, e.g. =contrib/lisp= in the Org-tree or
30 =~/.emacs.d/lisp=. It is also strongly recommended to read the
31 comments at the beginning of [[https://code.orgmode.org/bzg/org-mode/raw/master/contrib/lisp/ox-koma-letter.el][the file]] for information or changes that
32 have not yet been documented here.
34 ** DONE Minimal configuration of the KOMA-Script letter exporter
36 To use the KOMA-Script letter exporter, you have to add it to Emacs's
37 load path, activate it, and optionally configure a LaTeX class for the
38 LaTeX exporter (the KOMA-Script letter exporter uses the LaTeX
39 exporter internally).
41 1. Add the path containing =ox-koma-letter.el= to the Emacs
42    =load-path=. For example, if you use the version contained in the
43    directory =contrib/lisp= of Org-mode, add the following to your
44    Emacs configuration:
45    #+BEGIN_SRC emacs-lisp
46    (add-to-list 'load-path "/path/to/org-mode/contrib/lisp")
47    #+END_SRC
49 2. Activate the KOMA-Script letter exporter by adding the following to
50    your Emacs configuration:
51    #+BEGIN_SRC emacs-lisp
52    (eval-after-load 'ox '(require 'ox-koma-letter))
53    #+END_SRC
55 3. <<letter-class-definition>> The KOMA-Script exporter comes with
56    minimal latex class =default-koma-letter= that is enabled by
57    default.  You may add your own customized class, =my-letter=, as
58    follows:
60    #+BEGIN_SRC emacs-lisp
61      (eval-after-load 'ox-koma-letter
62        '(progn
63           (add-to-list 'org-latex-classes
64                        '("my-letter"
65                          "\\documentclass\{scrlttr2\}
66           \\usepackage[english]{babel}
67           \\setkomavar{frombank}{(1234)\\,567\\,890}
68           \[DEFAULT-PACKAGES]
69           \[PACKAGES]
70           \[EXTRA]"))
72           (setq org-koma-letter-default-class "my-letter")))
73    #+END_SRC
75    A more complicated example is presented in the commentary section
76    of [[https://code.orgmode.org/bzg/org-mode/raw/master/contrib/lisp/ox-koma-letter.el][=ox-koma-script.el=]].
78    Note that you may have to add the LaTeX Babel package to you
79    preamble, e.g., as
80    #+BEGIN_SRC emacs-lisp
81      (eval-after-load 'ox-latex
82        '(add-to-list 'org-latex-packages-alist '("AUTO" "babel" t) t))
83    #+END_SRC
85    For more information about this step, refer to the documentation of
86    the variables [[https://orgmode.org/worg/doc.html#org-latex-classes][org-latex-classes]], [[https://orgmode.org/worg/doc.html#org-latex-default-packages-alist][org-latex-default-packages-alist]],
87    and [[https://orgmode.org/worg/doc.html#org-latex-packages-alist][org-latex-packages-alist]].
89 ** DONE A simple letter example
91 Printed below is a minimal Org file that can be exported as a
92 KOMA-Script letter. In this file, press =C-c C-e= to bring up the
93 exporter dispatcher and then press =k o= to export the Org file to a
94 PDF file. For your convenience, you can [[https://code.orgmode.org/bzg/worg/raw/master/exporters/koma-letter-new-example.org][download the KOMA letter
95 example]] and you can also [[https://code.orgmode.org/bzg/worg/raw/master/exporters/koma-letter-new-example.pdf][download the example PDF letter]].
97 #+INCLUDE: koma-letter-new-example.org src org
99 Since no =LaTeX_CLASS= has been specified the default class will be
100 used.  The first block of lines specify title, subject and date.
101 =PLACE= specify the closing sentence and the place from which the
102 letter is send.
104 The address of the receiver is specified in the [[#special-headings][special heading]]  with
105 the =:to:= tag.  Note that the headline text is arbitrary and ignored.
107 The main letter is the first non-special heading[fn:1].  Notice that
108 the text of this headline also serves as the opening of the letter.
109 The closing of the letter is set via the closing special heading.
110 Note that closing statement is the headline title and the signature is
111 the content.
113 Finally, the 'backmatter' printed after the signature are defined in
114 the tree special heading with the tags =ps=, =cc= and =encl=.  With
115 the =OPTION= =after-closing-order= we can reorder the ordering of the
116 'backmatter'.  The content of the =after_letter= heading is inserted
117 after =\end{letter}=.  This is useful executing arbitrary latex
118 commands.  For instance, we may want to include PDFs via the
119 [[http://www.ctan.org/pkg/pdfpages][=pdfpages=]] package.
121 =ox-koma-letter.el= also supports an alternative syntax that relies
122 less on headings as illustrated by the example in this [[https://orgmode.org/worg/sources/exporters/koma-letter-example.org][Org-file]] and
123 the corresponding [[https://orgmode.org/worg/images/ox-koma-letter/koma-letter-example.pdf][pdf]].
125 When composing new letters it may be useful to start with template
126 by typing =C-c C-e #= and choosing =koma-letter=.
128 ** TODO Learning more about KOMA-Script Letter
129 ~org-koma-letter.el~ exports a LaTeX document utilizing the ~scrlttr2~
130 class of KOMA-Script.  The document class is described in great
131 details in chapter 4 of the [[http://mirrors.ctan.org/macros/latex/contrib/koma-script/doc/scrguien.pdf][KOMA-Script manual]].  If you have TeXLive
132 you can open the manual with the command ~texdoc KOMA-Script~ and with
133 MiKTeX one might call ~mthelp KOMA-Script~.
135 Elements such as the address-field is automatically typeset using
136 user-provided /metadata/, or /variables/, such as address, sender name
137 etc.  A full list of variables are available in table 4.1 and an
138 illustration of placement of variables is shown in figure 4.9, both
139 the in KOMA-Script manual.  A subset of these are supported directly
140 by ~ox-koma-letter.el~.
142 Placement of elements is typically defined by a LCO file.  If, for
143 instance, the ~DIN~ LCO file is loaded, letters are typeset to fit
144 German DIN envelopes.  See table 4.18 in the KOMA-Script manual for
145 default classes.
147 You can customize the look of your letter by writing your own LCO
148 files and including them with the ~#+LCO:~ keyword.  See [[*Setting
149 letter metadata in LCO files]].
151 * DONE Configuration guide
153 In addition to the actual content a typical letter also contains
154 additional information, such as the sender's and recipient's
155 addresses, a date, and so on. From now on we refer to this additional
156 data as /letter metadata/.
158 A special group of letter metadata controls the appearance of the
159 letter, such as the presence of foldmarks or a back address. These are
160 called /letter options/.
162 ** DONE Setting letter metadata and letter options
164 Letter metadata can be configured in one of four ways, listed below
165 from the most specific to the most general:
167 1. using Org option lines, as show in the [[*A simple letter example][simple letter example]] above,
168 2. by using separate Org LaTeX classes,
169 3. by setting Emacs variables, or
170 4. in a KOMA-Script Letter Class Option file (LCO file).
172 We cover these in turn.
174 *** DONE Multiple LaTeX Classes
175 It is possible to define multiple LaTeX classes for different
176 types of letters using the method used [[letter-class-definition][above]].  For example one could
177 add a special Org LaTeX class, say =foo-copr=, for sending letters to
178 the stakeholders of Foo Corp, that loads the Foo Logo, specifies the
179 footer and so forth.  The class is then loaded by inserting
180 =#+LATEX_CLASS: foo-corp=.
182 *** DONE Setting letter metadata in Org option lines
184 A simple way to set letter metadata on-the-fly is by using Org option
185 lines as used in the [[*A simple letter example][simple letter example]] above where we used both
186 special headings and keywords.
188 Some letter options are set using an =#+OPTIONS:= line in the same
189 manner as other [[https://orgmode.org/manual/Export-settings.html][Org mode export options]].
191 A [[#metadata][list of KOMA letter metadata settings]] is provided below.
193 Metadata set in the file takes the highest priority. Thus, you can set
194 default letter metadata using Emacs variables or in an LCO file (see
195 below).  Then, you can tweak your letter options in the file itself.
197 If you write a letter as a subtree of an Org heading, you must use Org
198 properties inside a =:PROPERTIES:= drawer and prefix every option
199 property with the string =EXPORT_=. See the [[https://orgmode.org/manual/Export-settings.html][chapter Export options in
200 the Org manual]] for details.
202 *** DONE Setting letter metadata in Emacs variables
204 Letter metadata can also be set using Emacs variables, e.g., in your
205 init-file. For example, the Emacs Lisp snippet below sets the letter's
206 closing line and the location.
208 #+BEGIN_SRC emacs-lisp
209   (setq org-koma-letter-closing "See you soon,"
210         org-koma-letter-from-address "Verona")
211 #+END_SRC
213 A [[#settings][list of KOMA letter metadata settings]] is provided below.
215 *** DONE Setting letter metadata in LCO files
217 A third way, letter metadata can be set in so-called letter class
218 option files (LCO files).  LCO files are regular TeX files which are
219 included in the TeX source of the letter. Consequently, one has access
220 to the entirety of KOMA-Script options in LCO files and can also
221 include other LaTeX code.  For more information about LCO files, see
222 the [[http://www.ctan.org/pkg/koma-script][KOMA-Script documentation]].
224 LCO files are set by the =#+LCO: LCO1 LCO2= option line or the Emacs
225 variable =org-koma-letter-class-option-file=.  KOMA-Script comes with
226 a variety of pre-made LCO files, such as =DIN= for German letters,
227 =NF= for French letters, or =UScommercial9= for US-American letters.
229 Letter metadata set in LCO files overwrites [[*Setting letter metadata in Emacs variables][letter metadata set in
230 Emacs variables]] but not [[*Setting letter metadata in Org option lines][letter metadata set in the Org file]].
232 LCO files are convenient as modules that can be loaded to access
233 particular setups across letters.  Examples include the sender's
234 address, the first footer and header, banking information, or the
235 inclusion of a scanned signature.  A particularly useful LaTeX command
236 is =\ifkomavarempty{KOMAVAR}{TRUE}{FALSE}= that can be used to
237 condition the printed output depending on whether =KOMAVAR= is set or
238 not.  See the example under =\setkomavar{frombank}= in the KOMA-Script
239 manual (currently page 183).
241 The following LCO file, called =DefaultAddress.lco=, sets the default
242 address. It can loaded using the Org option line =#+LCO: DefaultAddress=
243 (without the =.lco= extension).
245 #+BEGIN_SRC latex :exports code
246 % Default letter configuration file
247 \ProvidesFile{DefaultAddress.lco}
249 % Default address
250 \setkomavar{fromname}{Jane Doe}
251 \setkomavar{fromaddress}{Some Street 1\\12345 Some City}
252 \setkomavar{fromemail}{jane.doe@email.com}
253 \setkomavar{fromphone}{(555) 526-3363}
254 \setkomavar{signature}{\usekomavar{fromname}}
255 #+END_SRC
257 The following LCO file, called =Banking.lco=, configures a footer with
258 banking information. To load it together with the default address
259 defined above one can use the Org option line =#+LCO: DefaultAddress
260 Banking=.
262 #+BEGIN_SRC latex :exports code
263   % Banking information configuration file
264   \ProvidesFile{Banking.lco}
266   % Banking information in the footer
267   \setkomavar{frombank}{Jane Doe\\Account number: 12\,345\,678\\Somebank\\Bank code number: 876\,543\,21}
268   \setkomavar{firstfoot}{%
269     \footnotesize
270     \parbox[b]{\linewidth}{\centering\usekomavar{frombank}}}
271 #+END_SRC
273 Custom LCO files must be placed in a directory where LaTeX will find
274 them. On GNU/Linux, this defaults to =~/texmf/tex/latex=. On OS X, use
275 =~/Library/texmf/tex/latex=.  In TeX Live, these paths can be
276 configured using the following command:
277 #+BEGIN_SRC sh
278 tlmgr conf texmf TEXMFHOME ~/some/path
279 #+END_SRC
281 You can test whether =foo.lco= is recognized by TeX Live by running
282 the command =kpsewhich foo.lco=. After adding a file to the TeX Live
283 path you may have to run =mktexlsr=.
285 ** DONE List of KOMA-Script letter metadata settings
286    :PROPERTIES:
287    :CUSTOM_ID: settings
288    :END:
290 This section tries to list all Org option lines, Emacs variables, and
291 the corresponding =ox-koma-letter= variables or options that control
292 the behavior of the KOMA-Script letter exporter.
294 To get a complete list of variables please use the Customize
295 interface, e.g. by typing
296 #+BEGIN_EXAMPLE
297 M-x custromize-group org-export-koma-letter
298 #+END_EXAMPLE
301 *** DONE List of =ox-koma-letter= letter metadata
302 :PROPERTIES:
303 :CUSTOM_ID: metadata
304 :END:
306 The following letter metadata can be set by respective Org option
307 lines. In general, they correspond to a LaTeX command such as:
309 #+BEGIN_SRC latex :exports code
310 \setkomavar{<KOMA variable>}{<value>}
311 #+END_SRC
313 | Option line     | Emacs variable                    | KOMA-Script variable | Description                                      |
314 |-----------------+-----------------------------------+----------------------+--------------------------------------------------|
315 | =#+LCO:=          | =org-koma-letter-class-option-file= |                      | The default LCO file.                            |
316 | =#+FROM_LOGO=     | =org-koma-letter-from-logo=         | =fromlogo=             | The sender's logo.                               |
317 | =#+TITLE:=        |                                   | =title=                | The  title of the letter.                        |
318 | =#+SUBJECT:=      |                                   | =subject=              | The subject of the letter                        |
319 | =#+DATE:=         |                                   | =date=                 | The time-stamp of the letter.                    |
320 | =#+LOCATION:=     | =org-koma-letter-location=          |                      | The space opposite the to address                |
321 | =#+PLACE:=        | =org-koma-letter-place=             | =place=                | The place of the letter.                         |
322 | =#+AUTHOR:=       | =org-koma-letter-sender=            | =fromname=             | The sender's name.                               |
323 | =#+FROM_ADDRESS:= | =org-koma-letter-from-address=      | =fromaddress=          | The sender's address.[fn:3]                      |
324 | =#+PHONE_NUMBER:= | =org-koma-letter-phone=             | =fromphone=            | The sender's phone number.                       |
325 | =#+URL=           | =org-koma-letter-url=               | =fromurl=              | The sender's URL, e. g., the URL of her homepage |
326 | =#+EMAIL:=        | =org-koma-letter-email=             | =fromemail=            | The sender's email.                              |
327 | =#+TO_ADDRESS:=   |                                   |                      | The recipient's address.[fn:3]                   |
328 | =#+OPENING:=      | =org-koma-letter-opening=           |                      | The opening line of the letter.[fn:5]            |
329 | =#+CLOSING:=      | =org-koma-letter-closing=           |                      | The closing line of the letter.[fn:5]            |
330 | =#+SIGNATURE:=    | =org-koma-letter-signature=         | =signature=            | The sender's signature.                          |
331 | =#+LATEX_CLASS:=  | =org-koma-letter-default-class=     |                      | Corresponds to =org-latex-default-class=           |
333 *** DONE List of =ox-koma-letter= options
334 :PROPERTIES:
335 :CUSTOM_ID: options
336 :END:
338 The following letter options can be set in an =#+OPTIONS:= line. In
339 general, they correspond to a LaTeX command such as:
341 #+BEGIN_SRC latex :exports code
342 \KOMAoption{<KOMA option>}{<value>}
343 #+END_SRC
345 | Option              | Emacs variable                             | KOMA-Script option | Description                                                          |
346 |---------------------+--------------------------------------------+--------------------+----------------------------------------------------------------------|
347 | =after-closing-order= | =org-koma-letter-special-tags-after-closing= |                    | Order or special headlines such as =ps=, =cc=, and =encl=                  |
348 | =after-letter-order=  | =org-koma-letter-special-tags-after-letter=  |                    | Order or special headlines such as =after_letter=                      |
349 | =backaddress=         | =org-koma-letter-use-backaddress=            | =backaddress=        | Print the sender's address in a  line above the recipient's address. |
350 | =phone=               | =org-koma-letter-use-phone=                  | =fromphone=          | Print the sender's phone.                                            |
351 | =url=                 | =org-koma-letter-use-url=                    | =fromurl=            | Print the sender's URL                                               |
352 | =email=               | =org-koma-letter-use-email=                  | =fromemail=          | Print the sender's email.                                            |
353 | =foldmarks=           | =org-koma-letter-use-foldmarks=              | =foldmarks=          | If and how foldmarks are printed.                                    |
354 | =subject=             | =org-koma-letter-subject-format=             | =subject=            | If and how to print the letter's subject line.                       |
355 | =place=               | =org-koma-letter-use-place=                  |                    | Print the letter's place stamp.                                      |
356 | =from-logo=           | =org-koma-letter-use-from-logo=              | =fromlogo=           | Insert the sender's logo.                                            |
358 *** DONE List of =ox-koma-letter= special headings
359 :PROPERTIES:
360 :CUSTOM_ID: special-headings
361 :END:
362 Special headings may be used to input metadata in =ox-koma-letter=
363 documents.  A special heading is simply a heading with a recognized
364 tag.  Their usages were illustrated in the [[*A simple letter example][example]] above.  Special
365 headings tags are defined in the variables
366 =org-koma-letter-special-tags-in-letter=,
367 =org-koma-letter-special-tags-after-closing=, and
368 =org-koma-letter-special-tags-after-letter=.  Note that these can also
369 be dynamically set via the options =after-closing-order= and
370 =after-letter-order=.
371 Currently the following special headings are recognized.
373 | Tag           | KOMA-Script           | Description                                                               |
374 |---------------+-----------------------+---------------------------------------------------------------------------|
375 | =to=            | =\begin{letter}{to}=    | To-address. Alternative to =#+TO_ADDRESS:=.                                 |
376 | =from=          | =fromaddress=           | From-address.  Alternative to =+FROM_ADDRESS:=.                             |
377 | =closing=       | =closing= and =signature= | headings title and signature.  Alternative to =#+CLOSING:= and =#+SIGNATURE:= |
378 | =after_closing= |                       | Content is inserted after =\closing{.}=.                                    |
379 | =ps=            | =\ps{}=                 | Wrap content in a =ps=-macro.                                               |
380 | =cc=            | =\cc{}=                 | Wrap content in a =cc=-macro.                                               |
381 | =encl=          | =\encl{}=               | Wrap content in a =encl= macro.                                             |
382 | =after_letter=  |                       | Content is inserted after =\end{letter}=.                                   |
383 |---------------+-----------------------+---------------------------------------------------------------------------|
386 * Footnotes
388 [fn:1] A special headings in =ox-koma-letter= is a heading with a
389 recognized tag.  These headings are treated specially.
391 [fn:2] If the letter is exported below an Org headline, the default
392 subject contains the Org headline text.
394 [fn:3] The options lines =#+FROM_ADDRESS:=, =#+TO_ADDRESS:=, can be
395 used multiple times and may also be specified using headlines cf. the [[*A simple letter
396  example][example]] above.
398 [fn:5] The options lines =#+OPENING:= and =#+CLOSING:= cannot be set
399 in an LCO file.
401 [fn:6] See the [[http://www.komascript.de/][KOMA script documentation]] for a list of accepted
402 values.