Suppose that I have a large number of .tex files with names 1.tex, 2.tex, ..., N.tex (with N = some integer).

I would like to write a LaTeX document, where I set some parameters for example:

howMany = 10, from = 21

and then, when I compile this file I obtain the pdf output which contains the contents of the input files 21.tex, 22.tex, ..., 30.tex. If howMany > N or from + howMany > N I would like to show some error message in the pdf output.

Is it possible to do it in LaTeX?


Yes, it is! I defined a command \MultiInput[<ini>]{<num>}{<name template>} which loops from <ini> (default is 1) to <ini>+<num>-1, replaces the #1 in <name template> by the current number and checks if the file exists. If it does the file is input, otherwise it prints !ERROR! to the PDF file and shows an error message in the console.

The code below produces:

enter image description here


\NewDocumentCommand \MultiInput { O{1} m m }
    \int_step_inline:nnn {#1} {#1+#2-1}
        \file_if_exist:nTF {#3}
          { \input {#3} }
            \msg_error:nnn { azonips } { file-not-found } {#3}
\msg_new:nnn { azonips } { file-not-found }
  { File~`#1'~not~found. }



  • Thank you kindly for answer. I have additional question: Is it possible to compute reminder of n divided by k in LaTeX (to avoid checking is the file exists)? – azonips Jun 12 '19 at 11:54
  • @azonips Yes, it is. You can use \int_mod:nn{n}{k} in the code above. However I don't know how this could replace checking if the file exists... If you explain what you have in mind I can change the code for you. – Phelype Oleinik Jun 12 '19 at 12:05
  • I would like to have input files from one with name: (from mod N) + 1, to one with name ((from + howMany) mod N) + 1, then I get as filename i.tex, where i is an integer from 1 to N. – azonips Jun 13 '19 at 12:01
  • @azonips I'm still not sure of what you are trying to accomplish, sorry. However, try replacing the line \int_step_inline:nnn {#1} {#1+#2-1} by this: \int_step_inline:nnn { \int_mod:nn {#1} {3} +1 } { \int_mod:nn {#1+#2} {3} +1 } and see if it helps. – Phelype Oleinik Jun 13 '19 at 14:38
  • Where can I read sth about \int_step_inline instruction? I understand that #1, #2, #3 are arguments of \MultiInput function. If \int_step_inline is sth similar to for(int i;i<n;i++) {} I would like to do sth with i (which is, in general, not #1, #2 or #3). Is it possible to obtain sth like i from \int_step_inline instruction? – azonips Jun 14 '19 at 9:17

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.