Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Beautiful Designs

Save for later
  • 360 min read
  • 2015-09-15 00:00:00

article-image

In this article written by Stefan Kottwitz, author of the book LaTeX Cookbook, the author wants us to learn about the following topics:

  • Adding a background image
  • Preparing pretty headings

(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.

Adding a background image

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.

How to do it...

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:

  • Loading the background package
  • Setting up the background using the command backgroundsetup with options

Here we go:

  1. Load the background package using the following command:
    usepackage{background}
  2. Setup the background. Optionally, specify scaling factor, rotation angle, and opacity. Provide the command for printing on the background. We will use includegraphics here with a drawing of the CTAN lion:
    backgroundsetup{scale = 1, angle = 0, opacity = 0.2,
    
    contents = {includegraphics[width = paperwidth,
    
    height = paperheight, keepaspectratio] {ctanlion.pdf}}}
  3. Compile at least twice to let the layout settle. Now all of your pages will show a light version of the image over the whole page background, like this:

    beautiful-designs-img-0

How it works...

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.

There's more...

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:

  • Placed at the top
  • With customizable rotation, here 0 degrees
  • Scaled four times the size of normal text
  • Colored with 80 percent of standard blue (like mixed with 20 percent of white)
  • Vertically shifted by 2ex downwards
  • With dashes around
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:

beautiful-designs-img-1

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime

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:

beautiful-designs-img-2

Here is a summary of selected options with their default values:

  • contents: Text, images, or drawing commands, Draft is the default
  • placement: The center, top or bottom, center is the default
  • color: A color expression which TikZ understands, default is red!45
  • angle: A value between -360 and 360, 0 is default for top and bottom, 60 for center
  • opacity: A value for the transparency between 0 and 1, default is 0.5
  • scale: A positive value, default is 8 for top and bottom, 15 for center
  • hshift and vshift: Any length for horizontal or vertical shifting, default is 0 pt

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.

Preparing pretty headings

This recipe will show how to bring some color into documents headings.

How to do it...

We will use TikZ for coloring and positioning. Follow the following steps:

  1. Set up a basic document with blindtext support:
    documentclass{scrartcl}
    
    usepackage[automark]{scrpage2}
    
    usepackage[english]{babel}
    
    usepackage{blindtext}
  2. Load TikZ, beforehand, pass a naming option to the implicitly loaded package xcolor for using names for predefined colors:
    PassOptionsToPackage{svgnames}{xcolor}
    
    usepackage{tikz}
  3. Define a macro which prints the heading, given as an argument:
    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}}
  4. Use the new macro for the headings, printing headmark, and complete the document with some dummy text:
    clearscrheadings
    
    ihead{tikzhead{headmark}}
    
    pagestyle{scrheadings}
    
    begin{document}
    
    tableofcontents
    
    clearpage
    
    blinddocument
    
    end{document}
  5. Compile and take a look at a sample page header:

    beautiful-designs-img-3

How it works...

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:

  • Referring to the current page node for positioning
  • Using the drawing macro within a header command

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.

Summary

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.

Resources for Article:


Further resources on this subject:


Modal Close icon
Modal Close icon