Simplify code for repeating same image

graphicsloops

I want to print an image in the layout,

My current code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{showframe}
\begin{document}
\begin{center}
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad
\\[\baselineskip]% adds vertical line spacing
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}
\end{center}
\end{document}

My output:

enter image description here

How can I simplify my code, so that I don't need to copy and paste \includegraphics multiple times. An example would be a loop or for-each command.

Best Answer

A fairly general \repeatthings command. It has five arguments:

  1. the number of repeats
  2. (optional) the modulo
  3. the thing to repeat
  4. what to place between items
  5. (optional) what to place at the modulo

Nothing is added at the end.

\documentclass{article}
\usepackage{graphicx}

\ExplSyntaxOn
\NewDocumentCommand{\repeatthings}{m o m +m +O{}}
 {% #1 = number of repeats
  % #2 = optional modulo
  % #3 = thing to repeat
  % #4 = what to put in between
  % #5 = what to put at chunks specified by #2
  \IfNoValueTF { #2 }
   {
    \latexforti_repeat:nnn { #1 } { #3 } { #4 }
   }
   {
    \latexforti_repeat:nnnnn { #1 } { #2 } { #3 } { #4 } { #5 }
   }
 }

\cs_new_protected:Nn \latexforti_repeat:nnn
 {% no modulo
  \int_step_inline:nn { #1 - 1 }
   {
    #2 #3
   }
  #2
 }

\cs_new_protected:Nn \latexforti_repeat:nnnnn
 {
  \int_step_inline:nn { #1 - 1 }
   {
    #3
    \int_compare:nTF { \int_mod:nn { ##1 } { #2 } = 0 } { #5 } { #4 }
   }
   #3
 }
\ExplSyntaxOff

\begin{document}

\begin{center}
\repeatthings{18}[3]{\includegraphics[width=.3\linewidth]{example-image-a}}{\quad}[\\[1ex]]
\end{center}

\clearpage

\repeatthings{20}{I must not drive the principal's car}{\par}

\bigskip

\repeatthings{20}[5]{I must not crash the principal's car}{\par}[\par\medskip]

\end{document}

enter image description here