





















































In this article written by Stefan Kottwitz, author of the book LaTeX Cookbook, the author wants us to learn about the following topics:
(For more resources related to this topic, see here.)
Non-standard documents, such as photo books, calendars, greeting cards, fairy tale books, may have a fancier design. The following recipes will show some decorative examples.
We can add background graphics such as watermarks, pre-designed letter-heads, or photos to any LaTeX document. This recipe will show us a way to add a background image.
We will use the background package. In this recipe, you can use any LaTeX document. You may also start with the article class and add some dummy text.
You just need to insert some commands into your document preamble, which means, between documentclass{…} and begin{document}. It would be:
Here we go:
usepackage{background}
backgroundsetup{scale = 1, angle = 0, opacity = 0.2,
contents = {includegraphics[width = paperwidth,
height = paperheight, keepaspectratio] {ctanlion.pdf}}}
The background package can place any text, drawing, or image on the page background. It provides options for position, color, and opacity. The example already showed some self-explanatory parameters. They can be given as package options or by using the backgroundsetup command. This command can be used as often as you like to make changes.
The contents option contains the actual commands which shall be applied to the background. This can simply be includegraphics, some text, or any sequence of drawing commands.
The package bases on TikZ and the everypage package. It can require several compiling runs until the positioning is finally correct. That is because TikZ writes the marks into the .aux file, which gets read in and processed in the next LaTeX run.
Instead of images, you could display dynamic values such as the page number or the head mark with the project title, instead of using a package such as fancyhdr, scrpage2, or scrlayer-scrpage.
The following command places a page number at the background:
backgroundsetup{placement = top, angle = 0,
scale = 4, color = blue!80,vshift = -2ex,
contents = {--thepage--}}
Here is a cut-out of the top of page 7:
To see how you can draw with TikZ on the background, let's take a look at an example. It draws a rounded border, and fills the interior background with light yellow color:
usetikzlibrary{calc}
backgroundsetup{angle = 0, scale = 1, vshift = -2ex,
contents = {tikz[overlay, remember picture]
draw [rounded corners = 20pt, line width = 1pt,
color = blue, fill = yellow!20, double = blue!10]
($(current page.north west)+(1cm,-1cm)$)
rectangle ($(current page.south east)+(-1,1)$);}}
Here, we first loaded the calc library, which provides syntax for coordinate calculations that we used at the end. A TikZ image in the overlay mode draws a rectangle with rounded corners. It has double lines with yellow in-between. The rectangle dimensions are calculated from the position of the current page node, which stands for the whole page. The result looks like this:
Here is a summary of selected options with their default values:
Further options for TikZ node parameters are explained in the package manual, which also contains some examples. It also shows how to select just certain pages for having this background. You can open it by typing texdoc background at the command line, or at http://texdoc.net/pkg/background.
There are more packages which can do a similar task like we showed in this recipe, for example watermark, xwatermark, and the packages everypage and eso-pic, which don't require TikZ.
This recipe will show how to bring some color into documents headings.
We will use TikZ for coloring and positioning. Follow the following steps:
documentclass{scrartcl}
usepackage[automark]{scrpage2}
usepackage[english]{babel}
usepackage{blindtext}
PassOptionsToPackage{svgnames}{xcolor}
usepackage{tikz}
newcommand{tikzhead}[1]{%
begin{tikzpicture}[remember picture,overlay]
node[yshift=-2cm] at (current page.north west)
{begin{tikzpicture}[remember picture, overlay]
path[draw=none, fill=LightSkyBlue] (0,0) rectangle
(paperwidth,2cm);
node[anchor=east,xshift=.9paperwidth,
rectangle, rounded corners=15pt,
inner sep=11pt, fill=MidnightBlue,
font=sffamilybfseries] {color{white}#1};
end{tikzpicture}
};
end{tikzpicture}}
clearscrheadings
ihead{tikzhead{headmark}}
pagestyle{scrheadings}
begin{document}
tableofcontents
clearpage
blinddocument
end{document}
We created a macro which draws a filled rectangle over the whole page width and puts a node with text inside it, shaped as a rectangle with rounded corners. It's just a brief glimpse at TikZ' drawing syntax.
The main points are as follows:
The rest are drawing syntax and style options, described in the TikZ manual. You can read it by typing the texdoc tikz command at the command prompt, or by visiting http://texdoc.net/pkg/tikz.
In this article we learnt how to add a background image to our document and also how to create pretty and attractive headings for our documents.
Further resources on this subject: