Creating tiled background patterns

We’ve covered before how to use the wallpaper package to create a background for your documents using, amongst others, tiled images in a previous post. But at times I really want to create patterns from existing LaTeX font ornaments or TikZ drawings, rather than using an extra image file.

The tcolorbox package has a fill tile picture option that can be used with arbitrary TikZ code:

tcolorbox-fill-tile-picture

But from experience, repeatedly running TikZ code can result in long compilation times, especially if you were planning to use design a tile pattern as the background to all pages. Saving a TikZ tile drawing as a LaTeX box, and then repeating that, would compile much faster.

So, OK, let’s give this a try and design some tiles! *crackles knuckles*

Designing a Tile

I’m no designer, so I thought I would just fumble along and try to mock up something first, before cropping/clipping things down to the required tile element. The adforn package provides some subtle ornaments, so I’ll use those as a start to play around on a grid to help me lay things out:

\usepackage{adforn}
 
\begin{document}
\definecolor{blossompink}{HTML}{FFD7D7}
\definecolor{fruitgold}{HTML}{FFEEBE}
\definecolor{stictchbrown}{HTML}{E7D192}
 
\begin{tikzpicture}[x=1cm,y=1cm,domain=0:3]
\draw[step=1cm,black!10,very thin] (0cm,0cm) grid (3cm,3cm);
\foreach \x in {0,1} {
  \foreach \y in {0,1} {
     \node[font=\Huge,text=blossompink] at (2*\x, 2*\y) {\adforn{5}};
     \node[font=\Huge,text=fruitgold] at (2*\x+1, 2*\y+1) {\adforn{35}};
   }
}
\begin{scope}[dashed,stictchbrown]
  \draw plot (\x,\x-1);
  \draw plot (\x,\x+1);
  \draw plot (\x,-\x+1);
  \draw plot (\x,-\x+3);
\end{scope}
\end{tikzpicture}

scratch-design-1 That looks pretty good to me! And I only need a 4-by-4 unit of this sketch (the dark gray square) to make me a tile, which I can get with a \clip (0,0) rectangle (2,2); at the start of the TikZ code.

This can then go into a \savebox for later use:

\newsavebox{\tileone}
\sbox{\tileone}{%
  \begin{tikzpicture}[x=1cm,y=1cm]
  \clip (0,0) rectangle (2,2);
  \foreach \x in {0,1} {
    \foreach \y in {0,1} {
       \node[font=\Huge,text=blossompink] 
         at (2*\x, 2*\y) {\adforn{5}};
       \node[font=\Huge,text=fruitgold] 
         at (2*\x+1, 2*\y+1) {\adforn{35}};
     }
  }
  \begin{scope}[dashed,stictchbrown]
    \draw plot (\x,\x-1);
    \draw plot (\x,\x+1);
    \draw plot (\x,-\x+1);
    \draw plot (\x,-\x+3);
  \end{scope}
  \end{tikzpicture}%
}

If you \usebox{tileone}, you’ll see this:
tile-1

And we can now use this with tcolorbox‘s fill tile picture option:

\begin{tikzpicture}
\path[draw,fill tile picture={%
  \node[inner sep=0pt,outer sep=0pt,fill=blue!40!black]
       {\scalebox{0.5}{\usebox{\tileone}}};
}] 
(2.75,-0.75) -- (3,0) -- (2.75,0.75)
\foreach \w in {45,90,...,315}
{ -- (\w:1.5cm) } -- cycle;
\end{tikzpicture}

This can also be easily made into a whole-page background. I usually (ab)use the page header to get it in:

\usepackage{fancyhdr}
\usepackage{tikzpagenodes}
\fancyhf{}
\renewcommand{\headrule}{}
\fancyhead[L]{%
\begin{tikzpicture}[overlay,remember picture]
  \path[fill tile picture={%
    \node[inner sep=0pt,outer sep=0pt] {\usebox{\tileone}};
  }] (current page.south east) rectangle (current page.north west);
  \fill[opacity=0.4,white] ([shift={(-1em,1em)}]
        current page text area.north west) rectangle
           ([shift={(1em,-1em)}]current page text area.south east);
\end{tikzpicture}
}
\begin{document}
\thispagestyle{fancy}

pagebg-tileone-fs8pagebg-tilethree-fs8

The extra thumbnails shows designs that uses pgfornament-han and pgfornament just for fun; might be a bit over the top there though. Enjoy designing your new backgrounds, but do take care not to be over-flamboyant to the point of being too distractive, and repeating things too many times on too many pages will still take a long time to compile, despite already using \usebox!

Setting page size and margins

There’s quite a bit of interest in the .tex code behind the Grid Computing Cluster report, but I think posting the raw code in its entirety would be a bit too overwhelming to quickly glean useful tips from it. (Also to avoid getting into any issues with my university… etc…)

So instead, I’ll be doing a series of short posts on how I achieved certain effects and book design, starting with this one. It’s probably faster for people to “zero-in” on what they need. 😉 Posts in this series will be tagged “book design”. But before you implement your next major book project with LaTeX, do check with your printing company about the printing process used. If you’ll be using on-demand laser printing, everything should go fine since the RGB colour model of your PDF should be adequate. However, if you’re going with offset printing, your printing company will most likely prefer CMYK colour model to be used instead, and I haven’t quite figured out how to tweak that perfectly with (pdf)LaTeX. In our case, only the book cover was printed with offset printing, and the effect was acceptable — so that ended well…

IMPORTANT DISCLAIMER: I am NOT a graphics designer, so my choice of colours/illustrations/etc probably sucks. Don’t shoot me. 🙂

Now there are multiple ways to achieve the same effect in LaTeX (that’s FOSS for you). For this report, I’ve decided to use the memoir document class, which incorporates the functionalities of many other packages. (Do take some time to browse or search through the excellent manual with texdoc or mthelp.)

The flowfram package looks like a very interesting solution for designing dynamic layout, but I didn’t find out about it until too late into my own project. Do find some time to look at it yourself. If you want to go with the standard document classes (report, book) or the koma-script classes, I’ll indicate what extra packages you’ll need where relevant.

In this first post we’ll just look at how (custom) page sizes and margins can be specified. Basically my page design is:

  • Stock paper size is 303mm x 216mm. The stock paper is the raw original paper that a commercial printer will print the page on, which will be trimmed down to the page size
  • Page size is A4 i.e. 297mm x 210mm.
  • The printer advised us to leave a good 10mm from the page edges around any printed text.
  • I decided the main typeblock will be 20mm from the top, bottom and spine (inner) edge of the page, and 15mm from the outer edge.
  • Also, the running header should occupy the height of a line of text, 7mm from the top edge of the typeblock. Whereas the baseline of the running footer is 7mm from the bottom edge of the typeblock.

These are not necessarily good measurements for an aesthetically-pleasing book design, but more because I was trying to strike a balance between aesthetics and to fit everything in (Lesson: Get in touch with the printing company early to check with their printing specifications per minimum page margins, etc!)

These can be implemented using commands from the memoir class. (The notion of ‘stock’, although a common concept in publishing and printing industries, is only available in memoir, not in the standard document classes.)

\documentclass[twoside]{memoir}

%% The stock paper size
\setstocksize{303mm}{216mm}

%% The ‘real’ page size
\settrimmedsize{297mm}{210mm}{*}

%% The stock paper will be trimmed 3mm from the
%% top edge and 3mm from the left edge

\settrims{3mm}{3mm}

%% Spine and trim page margins from main typeblock
\setlrmarginsandblock{20mm}{15mm}{*}

%% Top and bottom page margins from main typeblock
\setulmarginsandblock{20mm}{20mm}{*}

%% Header occupies height of a single line;
%% Bottom edge of footer is 7mm from bottom edge
%% of typeblock

\setheadfoot{\baselineskip}{7mm}

%% Bottom edge of header is 7mm from top edge of
%% typeblock

\setlength\headsep{7mm}

%% Apply and enforce layout
\checkandfixthelayout

In my case, due to some later issues with colouring the 3mm bleeding edges too (as per our printing company’s request), I decided to make the trimmed page size the same as my stock size and increased the page margins instead:

\documentclass[twoside]{memoir}

%% Custom stock paper and page size
\setstocksize{303mm}{216mm}
\settrimmedsize{\stockheight}{\stockwidth}{*}

%% Adjust margins accordingly to achieve
%% same previous layout

\setlrmarginsandblock{23mm}{18mm}{*}
\setulmarginsandblock{23mm}{23mm}{*}

\setheadfoot{\baselineskip}{7mm}
\setlength\headsep{7mm}

\checkandfixthelayout

If you just need to print on exactly A4-sized paper, you can forget about setting the stock and trimmed size explicitly and just do

\documentclass[a4paper,twoside]{memoir}

\setlrmarginsandblock…
\setulmarginsandblock…

\checkandfixthelayout

One last thing: if you’re not using the memoir class, you can still change the page margins via a high-level interface with the geometry package:

\documentclass{article}
\usepackage{geometry}
\geometry{
   paperwidth=216mm, paperheight=303mm,
   left=23mm,  %% or inner=23mm
   right=18mm, %% or outer=18mm
   top=23mm, bottom=23mm,
   headheight=\baselineskip,
   headsep=7mm,
   footskip=7mm
}

Again, if you’re just going to print on A4 (or letter) paper, you can just do

\usepackage[a4paper]{geometry}
\geometry{
   left=20mm,  %% or inner=20mm
   right=15mm, %% or outer=15mm
   top=20mm, bottom=20mm,
   headheight=\baselineskip,
   headsep=7mm,
   footskip=7mm
}

i.e. just pass the a4paper option to the geometry package; there’s then no need to set the paper height and width explicitly.