---
Title: Block ads using LaTeX
Date: 2019-04-23 09:00
Modified: 2019-04-23 14:23
Author: Fabrice
Category: tips
Tags: LaTeX, inkscape, ads, git
Slug: latex-ad-block
og_image: images/thumb_stop-pub.png
twitter_image: images/thumb_stop-pub.png
Header_Cover: images/cover_velov.jpg
Summary: Blocking ads in your print-yourself tickets has never been so easy. A script is available at the end of the article.
---

I'm quite annoyed with ads. As of many, I'm using an adblocker on my computer, but there is one kind of ads that annoys me the most: ads on printable ticket. Not only it poisons our eyes, but it consumes ink to print it.

I'm aware that we can just open the QR/barcode on your smartphone, but still, isn't it better if we can get rid of the ad directly?

A first obvious solution could be to import your pdf in any [image editing software](https://www.gimp.org/) and simply use any rectangle shape selection tool to remove the ad.
However, this produces a new pdf file (or image file) that does not contain any more information about the text, and which may grow in size.

A simple workaround is then to use a vector graphic editor to keep this information: for instance, opening the PDF with [inkscape](https://inkscape.org/), and remove the image corresponding to the ads.
Yet more elegant, this approach still has a serious drawback: it breaks the fonts. Also, some of them (such as Air France's “_Excellence in Motion_” font) are proprietary and cannot be found easily/legally for free.

But inkscape can still be of use in order to remove those ads.
Indeed, it allows finding the coordinates and the dimensions of those ads as illustrated in the following (click to zoom):

[![Inkscape ad dimensions](/examples/inkscape-adblock.png)](/examples/inkscape-adblock.png)

**Explanations:** After opening your pdf file, start by selecting the ad (<span style="color:#8b0074">purple</span>), you may have to ungroup elements (`ctrl+shift+g`), then set the dimensions in cm or your favourite length unit (<span style="color:#0000ff">blue</span>) and finally note the dimensions of the ad (<span style="color:#ff0000">red</span>).

Then we just use LaTeX to add a white (or any background color, I let you devise it by yourself, you can use RGB codes with [xcolor](https://www.ctan.org/pkg/xcolor)) rectangle in front of the ad.
I already used the [wallpaper](https://www.ctan.org/pkg/wallpaper) package in [another post](/latex-letterhead.html), but it has some limitations: it doesn't allow us to import multiple pages (such as a round-trip ticket), and tikz doesn't interact well with the induced page geometry.

Thus, I used this [answer on stackexchange](https://tex.stackexchange.com/questions/12838/can-i-add-tikzpictures-to-pages-included-with-pdfpages).
To put it short, we use the package [pdfpages](https://ctan.org/pkg/pdfpages) with its options `pages={-}` to include every page, and the option `pagecommand` to include the rectangle overlay with the right dimensions `X`, `Y`, `L`, `H`.
That gives us the following `.tex` file which can simply be compiled with your favorite latex typesetter (for instance `pdflatex file.tex` twice).

```tex
\documentclass[a4paper]{article}
% Tikz with pdfpages
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pdfpages}
% avoid page numbering
\pagestyle{empty}
\begin{document}
\includepdf[pages={-},% include all pages
  pagecommand={% is called at the beginning of each inclusion
    \begin{tikzpicture}[remember picture,overlay]
      \draw[color=white,fill=white] ($(current page.north west) +%
         (X, -Y)$) rectangle ++ (L, -H);%
    \end{tikzpicture}%
}]%
{original file.pdf}
\end{document}
```

**Remark:** You may have noticed the minus sign in front of `Y` and `H`. This is because tikz computes coordinates from bottom left of the page, while inkscape (and gimp) starts at top left (and the frame is oriented accordingly).

Some examples of dimensions to copy-paste (mostly for myself):

* Rhônexpress:
```tex
… + (1.5cm, -14.65cm)$) rectangle ++ (18cm, -9cm);
```
* Air France/KLM foldable tickets.
```tex
… + (11cm, -18cm)$) rectangle ++ (9cm, -9cm);
```
You may have noticed that the dimensions are larger than in the above picture, this is because Air France sometime uses square ads.  
However, if you plan to use your smartphone, these companies also attach an ad-free `png` with minimal information.


As I don't buy a plane ticket every day, I didn't feel the need to script it, and I don't have enough examples to make an interesting enough database of ad locations.  
However, it is a great opportunity to get some more data about it, therefore there you can `git clone` the script from [here](https://git.epheme.re/fmouhart/hidepdfads).
As it is a self-hosted private repository, if you are eager to contribute, you may want to do a pull request on its [github repository](https://github.com/Chouhartem/hidepdfads) or send me an email on <img style="height:2em" src="/images/mel.png" alt="courriel"/>. In any case, feel free to contact me for any further remarks, comments or questions.

<center>
![xkcd 1319 Randall Munroe](https://imgs.xkcd.com/comics/automation.png)]  
[XKCD #1319](https://xkcd.com/1319/) by Randall Munroe.
</center>
