5

I wanted to do add some graphics to a document in a way that are rotated randomly. For this this purpose I tried the following code

\documentclass[ngerman,german,14pt,twoside,openany]{scrbook} 
\usepackage[left=2.5cm,right=2.5cm,top=2cm,bottom=2.5cm]{geometry}
\usepackage[ngerman]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{rotating, graphicx}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{tikz-timing}

%https://texblog.org/2011/04/19/latex-pseudo-random-number-generator/
\usepackage[first=-5, last=5]{lcg}
\newcommand{\random}{\rand\arabic{rand}}


\begin{document}

\random\\
\random\\
\random

\begin{figure}
  \centering
   \includegraphics[width=4cm,angle=2]{image.jpg}%
\end{figure}
\begin{figure}
  \centering
   \includegraphics[width=4cm,angle=-7]{image2.jpg}%
\end{figure}

\end{document}

The generation of the random numbers as well the placement of the figures each for itself works flawlessly but when I try to combine these two codes, i.e. angle=\random the compilation did not succeed. Any ideas how to solve this problem please?

3
  • 2
    Unrelated but: Don't use utf8x with recent TeX Live versions, it breaks more than it helps, LaTeX defaults to utf8 support since a few years now, and implements it in a more robust way than utf8x.
    – Skillmon
    Commented Sep 26, 2023 at 6:22
  • Pretty sure the issue is that \rand is not expandable. If you do \rand\includegraphics[width=4cm,angle=\arabic{rand}]{image.jpg} it should work. Commented Sep 26, 2023 at 6:25
  • @Skillmon Thanks for the hint!
    – mario1000
    Commented Sep 26, 2023 at 8:15

1 Answer 1

6

The way your random numbers are generated isn't expandable and hence can't be used where TeX expects a number. You have two options: either move your \rand before \includegraphics and only put \arabic{rand} in the argument to angle, or use an expandable random number generator, like the one built into LaTeX's L3 language:

\documentclass[ngerman,german,14pt,twoside,openany]{scrbook}
\usepackage[left=2.5cm,right=2.5cm,top=2cm,bottom=2.5cm]{geometry}
\usepackage[ngerman]{babel}
\usepackage{rotating, graphicx}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{tikz-timing}

\ExplSyntaxOn
\cs_new:Npn \random { \int_rand:nn {-5} {5} }
\ExplSyntaxOff


\begin{document}

\random\\
\random\\
\random

\begin{figure}
  \centering
   \includegraphics[width=4cm,angle=\random]{example-image-duck}%
\end{figure}
\begin{figure}
  \centering
   \includegraphics[width=4cm,angle=\random]{example-image-duck}%
\end{figure}

\end{document}

enter image description here

1
  • That did the trick! Thank you very much for your help!
    – mario1000
    Commented Sep 26, 2023 at 8:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .