# Simplify code for repeating same image

I want to print an image in the layout,

My current code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{showframe}
\begin{document}
\begin{center}
\end{center}
\end{document}

My output:

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.

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}
\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}

• thank you. this works good in overleaf but when i run in texmaker offline, error: "l.11 \NewDocumentCommand {\repeatthings}{m o m +m +O{}} The control sequence at the end of the top line" and output: i.imgur.com/jZeVkQc.png Dec 7 '21 at 19:37
• @latexforti You need to update your TeX distribution. In the meantime, add \usepackage{xparse}. Dec 7 '21 at 20:55

Create a \newcommand if all the figures are same as shown in your code. I have created \addfig command with no arguments.

New MWE

\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}

\usepackage{showframe}
\begin{document}
\begin{center}
\end{center}
\end{document}

\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{showframe}

\begin{document}
\begin{center}
\end{center}
\end{document}

You can use the very basic \loop ... \if ... \repeat construction and define your own repetition macro:

\documentclass{article}
\usepackage{graphicx}
\usepackage{showframe}

\newcounter{repeatcount}
\newcommand{\repeatthis}[2]{%
\setcounter{repeatcount}{0}%
\loop%
{#2}%
\stepcounter{repeatcount}%
\ifnum\value{repeatcount}<#1%
\repeat%
}

\begin{document}

\begin{center}
\\[\baselineskip]                                       % vertical line spacing
\includegraphics[width=.3\linewidth]{example-image-a}   % last item without \quad
\end{center}

\end{document}

This macro would then essentially repeat everything in its second argument as many times as stated in the first argument. So, for example, \repeatthis{10}{Hello } would output the following:

Update

egreg's nice idea to have an (optional) argument to define something that should be placed between the items can be implemented here as well:

\documentclass{article}
\usepackage{graphicx}
\usepackage{showframe}

\newcounter{repeatcount}
\newcommand{\repeatthis}[3][]{%
\setcounter{repeatcount}{0}%
\loop%
{#3}%
\stepcounter{repeatcount}%
\ifnum\value{repeatcount}<#2%
{#1}%
\repeat%
}

\begin{document}

\begin{center}