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!

Background images

I’m sure every LaTeX user tried this code at least once to add a “background image” on a page:

\includegraphics[width=\pagewidth,height=\textwidth]{wallpaper_filename}

But the included graphics, even though it looks the right size, would refuse to budge beyond the page margins, leaving a white border all round it. And you’d probably have problem placing any text at all on the graphics, thus defeating the purpose of a “background” image.

The wallpaper package offers an easy way to add background images or wallpapers in LaTeX, including tiling. The following commands are available:

  • \CenterWallPaper{}{} where is a value between 0 and 1, for specifying the fraction of paper width or height. The aspect ratio of the graphics will not be changed, i.e. you won’t get a “stretched” wallpaper as you would with your desktop wallpapers.
  • \ULCornerWallPaper{}{}
  • \LLCornerWallPaper{}{}
  • \URCornerWallPaper{}{}
  • \LRCornerWallPaper{}{}
The above four commands take the same arguments as \CenterWallPaper, but puts the image at the Upper Left, Lower Left, Upper Right and Lower Right corners of the page respectively.

For tiling wallpapers, there are:

  • \TileWallPaper{}{}{}, where you can specify (in pt, cm, in…) the width and height of the included image.
  • \TileSquareWallPaper{}{}, where the included image would be re-shaped as a square, and is the number of “image tiles” that will fill up the width of the paper.

Note that all the commands above will apply the background images on every page after the command is issued. To clear the wallpaper, use \ClearWallPaper.

Also, for each command \XXXWallPaper command above, there is a \ThisXXXWallPaper command that takes the same arguments but would apply the background image on only the current page.

You can now run wild with your imagination to design letterheads, chapter headings, covers, etc. Have fun!