0

Here in the MWE:

\documentclass[table]{standalone}
\usepackage{lipsum, graphicx, polyglossia, enumitem, amssymb, pifont, booktabs, cancel, animate, ulem, ragged2e}
\usepackage[most]{tcolorbox}
\graphicspath{ {/home/sandip/Downloads/} {images/} }
\linespread{2}

\newtcolorbox{highlightbox}{% enhanced, colframe=black, boxrule=2mm, coltext=white, colback=black}

\newtcolorbox{mybox}[2][]{% enhanced, before upper=\setlength{\parskip}{\bigskipamount}, colframe=black, boxrule=2mm, bottomrule=8mm, colbacktitle=black, fonttitle=\bfseries\centering, coltitle=white, colback=yellow!10!white, coltext=black, watermark zoom=1, watermark color=black, watermark opacity=.05, clip watermark, watermark text={\rotatebox{65}{Watermark}}, title=#2,#1}

\newcommand{\xxbody}{ \lipsum[2,4] }

\newcommand{\xxheading}{% Heading }

\begin{document}

\begin{mybox}{\LARGE \xxheading{}}

\xxbody{}

\end{mybox}

\end{document}

In folder ./images, I have several image files.

I want to place all of them around the text randomly with random rotations. But at the same time, I do not want them cluttered around together.

The images are tiny icons and placing them around the text randomly improves the visual appeal of the document, at least, in my view :)

How do I do that?

The question here and here have something similar. But there is no text involved.

deshmukh
  • 2,435
  • 1
  • 26
  • 46
  • LaTeX can't read the contents of your image folder, you have to generate a list of the files you want to consider (one could easily script that). Then you could pull random samples from such list, for example using \seq_rand_item:N from expl3. – Skillmon Oct 03 '21 at 12:00
  • Somewhat related: https://tex.stackexchange.com/questions/144193/how-do-i-put-a-die-roll-at-the-top-of-each-page – John Kormylo Oct 03 '21 at 15:02
  • @Skillmon Thanks. It would be of great help if you could elaborate. I am a newbie. But after creating the PDF from the above document, I convert it to PNG and use an image editing program to randomly place those images. I was looking forward to automating it. – deshmukh Oct 04 '21 at 14:48
  • @JohnKormylo Yes. I can see the connection. But my problem is in a way simpler. No need to select the dice number that will show, etc. We are going to pick up images and place them. At the same time, my problem is a bit trickier. We are placing these images around the text. For a newbie like me, this is overwhelming. :-) – deshmukh Oct 04 '21 at 14:50
  • With "around the text" do you mean it must be outside the text block? Or just anywhere on the page? – Skillmon Oct 04 '21 at 15:47
  • @Skillmon Sorry, I could not answer this any earlier. The images should not cover the text. And if they can be randomly rotated, even better. – deshmukh Oct 08 '21 at 07:48

1 Answers1

1

The following places up to 10 pictures (random how many) per page on random spots. The pictures are randomly pulled from a list of valid pictures.

New code

Places anywhere in the margin, no collision protection (but depending on the size of your images very unlikely, just change the seed).

\documentclass[]{article}

\usepackage[]{graphicx}

\ExplSyntaxOn \sys_gset_rand_seed:n { \int_from_alph:n { desh } + \int_from_alph:n { mukh } }

% list of image files here \seq_const_from_clist:Nn \c_deshmukh_pictures_seq { example-image, example-image-a, example-image-b, example-image-c, example-image-duck } \int_const:Nn \c_deshmukh_max_pictures_per_page_int { 10 }

\dim_new:N \l__deshmukh_x_dim \dim_new:N \l__deshmukh_y_dim

\cs_new_protected:Npn __deshmukh_put_picture: { \put ( \l__deshmukh_x_dim , -\l__deshmukh_y_dim ) { \includegraphics [ angle=\fp_eval:n{360rand()}, width=10pt ] { \seq_rand_item:N \c_deshmukh_pictures_seq } } } \cs_new_protected:Npn __deshmukh_set_rand_left: { \dim_set:Nn \l__deshmukh_x_dim { \fp_eval:n { rand() (1in + \hoffset + \oddsidemargin) } pt } \dim_set:Nn \l__deshmukh_y_dim { \fp_eval:n { rand() } \paperheight } } \cs_new_protected:Npn __deshmukh_set_rand_bottom: { \dim_set:Nn \l__deshmukh_x_dim { \fp_eval:n { rand() * \textwidth + 1in + \hoffset + \oddsidemargin } pt } \dim_set:Nn \l__deshmukh_y_dim { \fp_eval:n { rand() * ( \paperheight - 1in - \voffset - \topmargin - \headheight - \headsep - \textheight - \footskip ) + 1in + \voffset + \topmargin + \headheight + \headsep + \textheight + \footskip } pt } } \cs_new_protected:Npn __deshmukh_set_rand_right: { \dim_set:Nn \l__deshmukh_x_dim { \fp_eval:n { rand() * ( \paperwidth - 1in - \hoffset - \oddsidemargin - \textwidth ) + 1in + \hoffset + \oddsidemargin + \textwidth } pt } \dim_set:Nn \l__deshmukh_y_dim { \fp_eval:n { rand() } \paperheight } } \cs_new_protected:Npn __deshmukh_set_rand_top: { \dim_set:Nn \l__deshmukh_x_dim { \fp_eval:n { rand() * \textwidth + 1in + \hoffset + \oddsidemargin } pt } \dim_set:Nn \l__deshmukh_y_dim { \fp_eval:n { rand() * (1in + \voffset + \topmargin) } pt } }

\AddToHook{shipout/background} { \int_step_inline:nn { \int_rand:n { \c_deshmukh_max_pictures_per_page_int } } { \int_case:nn { \int_rand:n { 4 } } { { 1 } { __deshmukh_set_rand_left: } { 2 } { __deshmukh_set_rand_bottom: } { 3 } { __deshmukh_set_rand_right: } { 4 } { __deshmukh_set_rand_top: } } __deshmukh_put_picture: } } \ExplSyntaxOff

\usepackage{duckuments}% <- dummy content, also random example-image-duck

\begin{document} \duckument \end{document}

enter image description here

Old code

Places anywhere on the page.

\documentclass[]{article}

\usepackage[]{graphicx}

\ExplSyntaxOn % list of image files here \seq_const_from_clist:Nn \c_deshmukh_pictures_seq { example-image, example-image-a, example-image-b, example-image-c, example-image-duck } \int_const:Nn \c_deshmukh_max_pictures_per_page_int { 10 } \AddToHook{shipout/background} { \int_step_inline:nn { \int_rand:n { \c_deshmukh_max_pictures_per_page_int } } { \put ( {\fp_eval:n{rand()}\paperwidth}, {-\fp_eval:n{rand()}\paperheight} ) { \includegraphics { \seq_rand_item:N \c_deshmukh_pictures_seq } } } } \ExplSyntaxOff

\usepackage{duckuments}% <- dummy context, also random example-image-duck

\begin{document} \duckument \end{document}

enter image description here

Skillmon
  • 60,462
  • Thanks. This is close to what I am aiming it. Just a few problems. In your solution, the images cover text. They should not. I am using really tiny images --- size of an icon. Secondly, they should not clutter together. Here they do. And if they can be rotated randomly, nothing like it! – deshmukh Oct 08 '21 at 07:50
  • @deshmukh they can't cover text, but they'd be in the background of text... But they can be inside the textblock currently. Is your real document twosided or onesided? – Skillmon Oct 09 '21 at 08:53
  • Thanks. They do not cover text (as above text) but appear under the text. But I am trying to put them strictly 'around' the text. So, there is no overlap. And my document is one sided. – deshmukh Oct 10 '21 at 08:01
  • 1
    @deshmukh then the new code should do (well, there is a possibility that half of the icon is behind the text, you could hopefully easily fix that by adding some offset in the \__deshmukh_set_rand_...: macros. – Skillmon Oct 10 '21 at 17:33
  • Thanks. The new solution does it. And it almost feels like a miracle!! – deshmukh Oct 14 '21 at 10:45