1

I'm looking for a way of repeating an entire part of a document n times. The documents I create look like the following : most of the time, I'm repeating half a page. This is because I print those documents to hand out to my students.

example

Right now, I'm simply copying and pasting in my Tex editor but I'm looking for a better solution, since I sometimes have to repeat 5+ times, and if I want to change something, this is quite a lost of time.

Thanks !

EDIT :

  • I'd like to not have to create another document to do this deed. I create 10s of document a day already and this would get out of hand.

  • I'm asked to show an example of my code, problem is I'm working with my own commands that I've built (poorly) over the years, so my code is quite unreadable. But anyway, it is made of minipages, multicols, math equations, some environments with counters. Basically, I want to be able to repeat n times something that looks like that (a very simplified version) :


    \begin{document}
    \setcounter{exCounter}{1}

    \begin{multicols}{2}
    \begin{ex}
    math math math
    \end{ex}

    \columnbreak

    \begin{ex}
    \begin{minipage}{0.4\textwidth}Image\end{minipage}
    \begin{minipage}{0.6\textwidth}Text\end{minipage}
    \end{ex}

    \end{multicols}

    \end{document}

  • 3
    store this part of your document in \savebox{<name>}{...} and than repeat \usebox{<name>} so many time as you need. – Zarko May 8 at 8:58
  • 1
    @Zarko In case of the box containing counters which get stepped via \refstepcounter and \label-commands for referencing these counters, you might end up with a bunch of LaTeX Warning: Label `name of label' multiply defined.-warnings and a warning about multiply-defined labels. In case of using the hyperref package you might then also get a bunch of pdfTeX warning (ext4): destination with the same identifier (name{<name of anchor>}) has been already used, duplicate ignored-warnings. If you try with \(c/x)leaders instead, you might end up with undefined references... – Ulrich Diez May 8 at 9:15
  • @UlrichDiez, you are right. i anticipated, that this short contents haven't this things. just an untested idea (since an example of document is not known). – Zarko May 8 at 9:23
  • There are many ways to do this and the best option will depend on what you actually wan to do. Please post a minimal working example with your blocks of repeated code so that we can see what you want to do. Some one will then be able to how you might approach this. I have code that does things like this. It is not difficult to do but how best to do it depends on your application. – Andrew May 8 at 9:27
  • If you use LaTeX for creating pdf-files, I suggest creating two documents: One separate document which contains all the text once. Another document which via the pdfpages-package imports the pdf-file which results from compiling the separate document as many times as needed. You may need to learn about tricks for specifying custom paper sizes with the separate document. My answer to Saving an image containing labels produced using LaTeX code provides an example of how to create documents of custom paper-size for import into other documents. – Ulrich Diez May 8 at 9:29
4

Following on from Zarko's idea of using boxes here is a latex3 implementation using boxes. This will work provided that the repeated content is exactly the same, which seems to be the case for your MWE. The code that I have for doing things like this is a little different as my content changes in various ways for each repetition. Happy to propose other solutions if you need this flexibility.

The code below defines a RepeatedBlock environment, which takes one argument. The way that this works is:

\begin{RepeatedBlock}{<number of repetitions>}
...content to be repeated...
\end{RepeatedBlock}

With this in place, a variation of your MWE with 4 repetitions produces:

enter image description here

Here is the code:

\documentclass{article}

\usepackage{xparse}
\ExplSyntaxOn
\box_new:N \l_krirkrirk_box
\NewDocumentEnvironment{RepeatedBlock}{m +b}{
  \box_clear:N \l_krirkrirk_box
  \vbox_set:Nn \l_krirkrirk_box {#2}
  \prg_replicate:nn {#1} {\box_use:N \l_krirkrirk_box }
}{}
\ExplSyntaxOff

\usepackage{enumitem,multicol}

\begin{document}

  \begin{RepeatedBlock}{4}
    \begin{multicols}{2}
      \begin{enumerate}
        \item math math math
      \end{enumerate}

      \columnbreak

      \begin{enumerate}[resume]
        \item \begin{minipage}{0.4\textwidth}Image\end{minipage}
        \item \begin{minipage}{0.6\textwidth}Text\end{minipage}
      \end{enumerate}

    \end{multicols}
  \end{RepeatedBlock}

\end{document}

Caveat: this requires the latest version of latex3 as it uses the +b parameter option from xparse. You may need to update l3packages so that you have a version of the package from 2019-05-03 or later.

  • This seems perfect. However, and I'm sorry to ask for more of your time, but I'm not sure on how to update l3packages (I'm on Ubuntu). Could you provide a link ? Thanks again. – krirkrirk May 8 at 13:52
  • @krirkrirk If you are using TeXLive then tlmgr update l3packages should do the trick, although you might need to use sudo. There's a small chance that you will first need to update to TexLive 2019. – Andrew May 8 at 14:05

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.