General ⇒ Is this possible in LaTeX ? (random pictures in background)
Is this possible in LaTeX ? (random pictures in background)
Here are two quick "spots" (in PNG format, with some transparency), to be randomly pasted inside a random page :
I'm not sure yet of the proper size they should have on the page, but they should be randomly pasted anywhere on the page, inside the margins.
The main textures are published on the previous page of this topic. The final set of textures would be published after the code is fully working, and once I've created (or found) some realistic (or satisfying) textures effects. This is difficult and may take a lot of time to do.
I'm not sure yet of the proper size they should have on the page, but they should be randomly pasted anywhere on the page, inside the margins.
The main textures are published on the previous page of this topic. The final set of textures would be published after the code is fully working, and once I've created (or found) some realistic (or satisfying) textures effects. This is difficult and may take a lot of time to do.
NEW: TikZ book now 40% off at Amazon.com for a short time.

- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
Is this possible in LaTeX ? (random pictures in background)
Here's a way with TikZ.
\randomspot puts a spot with given filename at a random place on the page, rotation and scale are random too. You could simply add another factor to \paperheight and \paperwidth to have it on random pages only (where rnd is small enough to land on the page). rnd will be between 0 and 1.
Stefan
\pgfmathsetseed
could be omitted, it's just for haven a defined start, so repeated compilations would have the same result, even if random is used then.\randomspot puts a spot with given filename at a random place on the page, rotation and scale are random too. You could simply add another factor to \paperheight and \paperwidth to have it on random pages only (where rnd is small enough to land on the page). rnd will be between 0 and 1.
Code: Select all
\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{anysize}
\marginsize{1.0in}{1.0in}{0.5in}{0.5in}
\usepackage[]{hyperref}
\usepackage{eso-pic}
\usepackage{changepage}
\strictpagecheck
\usepackage{blindtext}
\usepackage{tikz}
\pgfmathsetseed{1}% define just some fixed start for pseudo random
\newcommand*{\randomspot}[1]{
\begin{tikzpicture}[overlay,remember picture]
\node [xshift = rnd*\paperwidth,yshift = rnd*\paperheight,
rotate=rnd*360]
at (current page.south west) {%
\pgfmathrnd
\includegraphics[scale = \pgfmathresult]
{Textures/#1}};
\end{tikzpicture}
}
\begin{document}
\AddToShipoutPicture{%
\checkoddpage
\ifoddpage
\put(0,0){%
\includegraphics[width=\paperwidth,
height=\paperheight]{Textures/odd_page.jpg}}
\else
\put(0,0){%
\includegraphics[width=\paperwidth,
height=\paperheight]{Textures/even_page.jpg}}
\fi
\randomspot{spot1.png}
\randomspot{spot2.png}
}
\Blinddocument
\end{document}
LaTeX.org admin
Re: Is this possible in LaTeX ? (random pictures in backgrou
Heyhey ! This is working GREAT !
Thanks a lot, Stefan !
Now, how the scaling is working ? What are the min and max sizes ?
If we prefer to have a set of textures to be randomly scaled, while another set isn't randomly scaled, how should we edit that code ? The round coffee mark texture should stay the same size on all pages. Also, it shouldn't be appearing on all pages, just a few of them.
Thanks a lot, Stefan !

Now, how the scaling is working ? What are the min and max sizes ?
If we prefer to have a set of textures to be randomly scaled, while another set isn't randomly scaled, how should we edit that code ? The round coffee mark texture should stay the same size on all pages. Also, it shouldn't be appearing on all pages, just a few of them.
- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
Is this possible in LaTeX ? (random pictures in background)
rnd in TikZ gives a random number between 0 and 1. \pgfmathrnd is the same as a command, which can be used in normal LaTeX, \pgfmathresult gives the result of the previous pgf calculation. So, scale=\pgfmathresult gave the scaling value between 0 and 1.
Just remove the scaling option. It's correct, the cup will stay the same of cause.
You could also introduce an optional parameter for the scaling, with a default value \pgfmathresult - just set 1 if you would like to have scale=1 as no change.
Also the omit rotation if you like. I just added it so show variety.
You can always use
Stefan
Just remove the scaling option. It's correct, the cup will stay the same of cause.
You could also introduce an optional parameter for the scaling, with a default value \pgfmathresult - just set 1 if you would like to have scale=1 as no change.
Also the omit rotation if you like. I just added it so show variety.
You can always use
\pgfmathrnd\pgfmathresult
to get a value between 0 and 1 (analogous, rnd
in TikZ options), and put this as factor to any length.Stefan
LaTeX.org admin
Is this possible in LaTeX ? (random pictures in background)
The random rotation is perfect. I wasn't expecting this.Stefan_K wrote:Also the omit rotation if you like. I just added it so show variety.
But what if we want a particular set of textures to be used just from time to time, I mean not on all pages ? The round coffee marks shouldn't be on each page, or else it would look weird.
I guess that it's preferable to define another
\randomspot
command in this case.- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
Is this possible in LaTeX ? (random pictures in background)
Sure, one could say, if \pgfmathrnd <0.25 then put the spot.
But it may be even easier to say
and now the node would be outside the page in most of the cases, just because the shift value is too high.
So you got only a random part of the pages with coffee stains, not all.
Stefan
But it may be even easier to say
Code: Select all
\node [xshift = 2*rnd*\paperwidth,yshift = 2*rnd*\paperheight,
rotate=rnd*360]

Stefan
LaTeX.org admin
Is this possible in LaTeX ? (random pictures in background)
Ha, great idea ! I'll try this.
Also, what if we want the scale to be between "min" and "max" (some numbers) ? I tried to replace the option
by
so if pgf = 0, then scale = min. If pgf = 1 then scale = max, but it doesn't work. I guess the syntax isn't right.
Also, what if we want the scale to be between "min" and "max" (some numbers) ? I tried to replace the option
Code: Select all
scale=\pgfmathresult
Code: Select all
scale=min + \pgfmathresult*(max - min)
- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
Is this possible in LaTeX ? (random pictures in background)
Yes, the syntax is a bit different. It could go like this:
Stefan
Code: Select all
\pgfmathrnd
\pgfmathmultiply{\pgfmathresult}{0.4}
\pgfmathadd{\pgfmathresult}{0.5}
\includegraphics[scale = \pgfmathresult]
{Textures/#1}};
\pgfmathresult
is always the result of the previous pgfmath operation. So, we take the random value between 0 and 1, multiply with our (max-min) to get the range for random, and add the minimum value.Stefan
LaTeX.org admin
Is this possible in LaTeX ? (random pictures in background)
It's all working great !
This thing is so incredibly cool ! I feel there's no limit here.
Now, about the position. I'm not sure I understand well that part :
The position is defined with respect to what origin ? South west ? Is that the bottom-left of the page ? (odd and even pages). Sorry if I sound dense here.
I would like to define a margin for the texture pasting, depending if the page is even or odd. Actually, I'm not sure this is really necessary, but I found that the textures doesn't match well with the background, near the book's folding edge.
I'll also have to try to add some other textures to the corners and paper borders (some paper tears).
The hardest part would be to design a great textures set. The corners are really tough to do.
I'm not sure this is worth it.

This thing is so incredibly cool ! I feel there's no limit here.
Now, about the position. I'm not sure I understand well that part :
Code: Select all
\node [xshift=rnd*\paperwidth,yshift=rnd*\paperheight,rotate=rnd*360]
at (current page.south west)
I would like to define a margin for the texture pasting, depending if the page is even or odd. Actually, I'm not sure this is really necessary, but I found that the textures doesn't match well with the background, near the book's folding edge.
I'll also have to try to add some other textures to the corners and paper borders (some paper tears).
The hardest part would be to design a great textures set. The corners are really tough to do.
I'm not sure this is worth it.
Is this possible in LaTeX ? (random pictures in background)
The code is working great now. I've added some torn edges to the paper, but the texture doesn't blend well with the background. I'm not sure I could design some good looking textures for this :