4

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.

3 Answers 3

5

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

2
  • 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
    – latexforti
    Dec 7, 2021 at 19:37
  • @latexforti You need to update your TeX distribution. In the meantime, add \usepackage{xparse}.
    – egreg
    Dec 7, 2021 at 20:55
3

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}

\newcommand{\addfig}{\includegraphics[width=.3\linewidth]{example-image-a}}

\usepackage{showframe}
\begin{document}
    \begin{center}
    \addfig\quad\addfig\quad\addfig\quad
    \\[\baselineskip]% adds vertical line spacing
    \addfig\quad\addfig\quad\addfig \quad
    \addfig\quad\addfig\quad\addfig \quad
    \addfig\quad\addfig\quad\addfig \quad
    \addfig\quad\addfig\quad\addfig \quad
    \addfig\quad\addfig\quad\addfig \quad
    \addfig\quad\addfig\quad\addfig
\end{center}
\end{document}

Add

If your figures are different, then add an argument to the \addfig command. like \addfig{example-image-a}. Here, you can change the figure name (example-image-a) as your wish.

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

\newcommand{\addfig}[1]{\includegraphics[width=.3\linewidth]{#1}}

\begin{document}
    \begin{center}
    \addfig{example-image-a}\quad\addfig{example-image-b}\quad\addfig{example-image-c}\quad
    \\[\baselineskip]% adds vertical line spacing
    \addfig{example-image-a}\quad\addfig{example-image-c}\quad\addfig{example-image-a} \quad
    \addfig{example-image-c}\quad\addfig{example-image-a}\quad\addfig{example-image-a} \quad
    \addfig{example-image-b}\quad\addfig{example-image-a}\quad\addfig{example-image-a} \quad
    \addfig{example-image-a}\quad\addfig{example-image-a}\quad\addfig{example-image-a} \quad
    \addfig{example-image-a}\quad\addfig{example-image-a}\quad\addfig{example-image-a} \quad
    \addfig{example-image-a}\quad\addfig{example-image-a}\quad\addfig{example-image-a}
    \end{center}
\end{document}
3

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}
\repeatthis{3}{\includegraphics[width=.3\linewidth]{example-image-a}\quad}
\\[\baselineskip]                                       % vertical line spacing
\repeatthis{14}{\includegraphics[width=.3\linewidth]{example-image-a}\quad}%
\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:

enter image description here


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}
\repeatthis[\quad]{3}{\includegraphics[width=.3\linewidth]{example-image-a}}
\\[\baselineskip]          % vertical line spacing
\repeatthis[\quad]{15}{\includegraphics[width=.3\linewidth]{example-image-a}}%
\end{center}

\repeatthis[, ]{10}{Hello} % the "stuff between" is not placed after the last item

\end{document}

The output of this would be:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.