Wiki source code of Customize the Preamble

Last modified by Vincent Massol on 2026/08/30 19:36

Show last authors
1 Every ##.tex## file a LaTeX export produces opens with a preamble: the ##\usepackage## lines and the macro definitions the rest of the document depends on. It is written by the ##Preamble## template, so a ##latex/Preamble## template of your own takes its place — [[Template Mechanism>>doc:documentation.extensions.dev.latex.template-mechanism.WebHome]] explains that lookup. In this tutorial you add a package the default preamble does not load, widen one of the lines it does load, and read both changes back out of a real export.
2
3 1. Read the preamble you are starting from. Export any page to LaTeX and open ##index.tex## in the package: everything before ##\begin{document}## is the preamble, and the line the last step widens is in there.(((
4 {{code language="tex"}}
5 %% For the TOC macro, to have local tocs
6 \usepackage{etoc}
7 %% For the Formula macro
8 \usepackage{amsmath}
9 %% For links to attachments (we embed the attachments and link to them)
10 \usepackage{attachfile}
11 {{/code}}
12 )))
13 1. Add a package. Render the default template first, then write your own lines after it: everything the default writes is kept, and only your addition is new. This one adds ##tabu##, for tables. Put it in a ##latex/Preamble## template and [[install it on the skin>>doc:documentation.extensions.dev.latex.override-template.WebHome]].(((
14 {{code language="tex"}}
15 #set ($template = $latex.processor.getTemplate('default/Preamble'))
16 $latex.processor.render($template)
17 %% For nice tables!
18 \usepackage{tabu}##
19 {{/code}}
20 )))
21 1. Widen a line the default writes. The default loads ##amsmath## for formulas, and the whole AMS set is wanted instead. Appending cannot do that, so edit the default's content before rendering it, with the [[four-step override strategy>>doc:documentation.extensions.dev.latex.override-template.WebHome]]. Both changes belong in the same template, since a wiki has one ##latex/Preamble##.(((
22 {{code language="tex"}}
23 ## Step 1: get the default template being overridden
24 #set ($template = $latex.processor.getTemplate('default/Preamble'))
25 ## Step 2: compute the new content
26 #set ($newContent = $stringtool.replace($template.content.content, '\usepackage{amsmath}', '\usepackage{amsmath, amsthm, amssymb, amsfonts}'))
27 ## Step 3: set the new content
28 #set ($discard = $template.content.setContent($newContent))
29 ## Step 4: render the modified template
30 $latex.processor.render($template)
31 %% For nice tables!
32 \usepackage{tabu}##
33 {{/code}}
34 )))
35 1. Export the page again and open ##index.tex##. The AMS line is wider and still sits where the default put it, which is what makes replacing the way to place a package among the others.(((
36 {{code language="tex"}}
37 %% For the Formula macro
38 \usepackage{amsmath, amsthm, amssymb, amsfonts}
39 {{/code}}
40
41 The appended package comes after everything the default wrote, at the very end of the preamble.
42
43 {{code language="tex"}}
44 %% Use LaTeX quotes by default
45 \MakeOuterQuote{"}
46
47 %% For nice tables!
48 \usepackage{tabu}
49
50 \begin{document}
51 {{/code}}
52
53 Every ##.tex## file of the package carries the preamble, so both changes are in the page files as well as in ##index.tex##.
54 )))

Get Connected