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!

Mail-Merge Batch Generating Documents with datatool Package

If you aren’t aware of the possibility before this, you read the title right, it’s possible to do mail-merging in LaTeX with e.g. the versatile datatool package. This makes LaTeX quite a handy Swiss knife in a pinch.

The example scenario: you need to produce a batch of letters, name cards, or certificates really quickly. Let’s say certificates of appreciation for some long-serving employees. The recipients list has most probably been compiled by someone, as a spreadsheet like this:

Now all spreadsheet applications should be able to export the worksheet as a comma-separated values (CSV) plain text file, so we have namelist.csv with the following contents:

Name,ID,Gender,Years in Service
Abdul Ali,382473856,M,15
Francesca Joestar,461276432,F,10
Chan Ker Mei,463724631,F,5
Hikaru Yagami,154954739,M,10

The datatool package can then load namelist.csv as a simple database, which each line being a record, consisting of fields delimited by commas. By default, the field names are given by the first line of the .csv file. After assigning macros to the field names, we can then use the macros to insert ‘mail merge’ fields into a LaTeX document.

Here’s a quick example, using the wallpaper package and an external image (image courtesy of fromoldbooks.org) for the decorative frame:

% Use scrartcl to allow larger base font size
\documentclass[16pt]{scrartcl}
\usepackage[left=3cm,right=3cm,top=6cm,bottom=5cm]{geometry}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tgschola}
\usepackage{fourier-orns}
\usepackage{graphicx}
\usepackage{wallpaper} % For background image frame

\usepackage{datatool}
% Load database ‘names’ from file ‘namelist.csv’
\DTLloaddb{names}{namelist.csv}

\begin{document}
\linespread{2}\selectfont
\pagestyle{empty}
% Set old-border.jpg as background image
\TileWallPaper{\paperwidth}{\paperheight}{old-border}

% For each record (line) in database ‘names’
% Assign field values by name to macros
\DTLforeach{names}
{\name=Name, \ID=ID, \gender=Gender, \yr={Years in Service}}{

  {\centering
    {\LARGE\bfseries Certificate of Appreciation}\par
    {\LARGE\decofourleft\quad\decoone\quad\decofourright}\par
    \vskip2em
    We thank\par
    % Insert ‘\name’ field
    {\large\bfseries\MakeUppercase{\name}}\par
    % Insert ‘\ID’ field
    {\large (ID: \ID)}\par
    for having been with\par
    {\large XYZ Company}\par
    % Insert ‘\yr’ field
    {for \emph{\LARGE\yr} glorious years}\par
    \vskip1em
    % Test ‘\gender’ field and insert him/her, his/her
    We commend \DTLifeq{\gender}{M}{him}{her} on
    \DTLifeq{\gender}{M}{his}{her} excellent service.\par
  }
  \vfill
  \hfill
  \begin{minipage}{.4\textwidth}
    Managing Director\par
    \vskip1cm
    \rule{8em}{.5pt}
    \vskip-1em
    (MD’s Name)\par
  \end{minipage}

  \pagebreak
}
\end{document}

And voilà, the output document has 4 pages, each containing the certificate for a recipient listed in namelist.csv.


Just for fun, here’re the same certifates using ornaments from the adforn font package. For more flamboyant ornaments, see the webomints fonts (installable via getnonfreescripts) or the psvectorian package.


The .tex source codes can be downloaded here.

datatool has many more macros for testing field values and even arithmetics. Other accompanying packages in the bundle, such as datapie, dataplot and databar, even lets you draw charts from .csv files via TikZ/PGF, so do check out the documentation.

Now I’m off to my holidays! Happy Chinese New Year to everyone, and may the Year of the Dragon brings only good tidings for you!