latex as an ad blocker

This commit is contained in:
Fabrice Mouhartem 2019-04-23 02:26:05 +05:30
parent 8f629682af
commit 69f79ac8ba
3 changed files with 72 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 KiB

View File

@ -0,0 +1,72 @@
---
Title: Block ads with LaTeX
Date: 2019-04-23 08:00
Author: Fabrice
Category: tips
Tags: LaTeX, inkscape, ads
Slug: latex-ad-block
Header_Cover: images/cover_velov.jpg
---
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?
To do this, I tried the most obvious solution: open it with inkscape, and remove the image.
However, this approach has a serious drawback, it breaks the fonts, and 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`.
```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}%
}]%
{af.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, if you are interested in it, feel free to send me dimensions and type of pdf (for instance a brand name) by email at <img style="height:2em" src="/images/mel.png" alt="courriel"/>.
<center>
![xkcd 1319 Randall Munroe](https://imgs.xkcd.com/comics/automation.png)]
[XKCD #1319](https://xkcd.com/1319/) by Randall Munroe.
</center>