2

I want to include multiple files in my source. The files are to be included in a file liste.tex in the following format:

fichier1.tex
files2.tex
test3.tex
....

I relied on answering the questions TikZ read out file but the last comma added problem

How not add (or remove) the last comma

\documentclass{article}
\usepackage{catchfile,tikz}

\begin{document}

  \CatchFileDef{\tempa}{assocFile.dat}{\endlinechar=`,}
\edef\tempb{\unexpanded{\foreach\tt in }{\unexpanded\expandafter{\tempa}}}
\tempb{\tt \par}

\tempb{    
\input{\tt}
\clearpage
}

\end{document}

2 Answers 2

6

Here's an implementation along the same idea, where the trailing comma is not a problem because \clist_map_inline:nn ignores empty items:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\multinput}{sm}
 {
  \IfBooleanTF{#1}
   {
    \rpapa_multinput_list:n { #2 }
   }
   {
    \rpapa_multinput_file:n { #2 }
   }
 }

\tl_new:Nn \l_rpapa_multinput_files_tl

\cs_new_protected:Nn \rpapa_multinput_file:n
 {
  \tl_set_from_file:Nnn \l_rpapa_multinput_files_tl { \endlinechar=`, } { #1 }
  \rpapa_multinput_list:V  \l_rpapa_multinput_files_tl
 }
\cs_new_protected:Nn \rpapa_multinput_list:n
 {
  \clist_map_inline:nn { #1 } { \input{##1}\par }
 }
\cs_generate_variant:Nn \rpapa_multinput_list:n { V }
\ExplSyntaxOff

\begin{document}

\multinput{assocFile.dat}

\bigskip

\multinput*{fichier1,files2,test3}

\end{document}

The input files just contain `I am ". Note that the *-variant takes as argument a comma separated list of file names.

enter image description here

A second implementation, where the “filename” case is dealt with doing a mapping on the file itself:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\multinput}{sm}
 {
  \IfBooleanTF{#1}
   {
    \rpapa_multinput_list:n { #2 }
   }
   {
    \rpapa_multinput_file:n { #2 }
   }
 }

\cs_new_protected:Nn \rpapa_multinput_list:n
 {
  \clist_map_inline:nn { #1 } { \input{##1}\par }
 }

\ior_new:N \g_rpapa_multinput_file_stream

\cs_new_protected:Nn \rpapa_multinput_file:n
 {
  \ior_open:Nn \g_rpapa_multinput_file_stream { #1 }
  \ior_map_inline:Nn \g_rpapa_multinput_file_stream
   {
    \input{##1}\par
   }
 }
\ExplSyntaxOff

\begin{document}

\multinput{assocFile.dat}

\bigskip

\multinput*{fichier1,files2,test3}

\end{document}
2
  • 1
    Merci, but I find it very difficult (especially as I do not control this syntax) with respect to your answer to the question referred.
    – rpapa
    Oct 19, 2015 at 8:12
  • @rpapa \tl_set_from_file:Nnn is essentially the same as \CatchFileDef.
    – egreg
    Oct 19, 2015 at 8:55
1

Say you have ch1.tex, ch2.tex, ch3.tex and you want to include them in main.tex

main.tex

\documentclass{article}

\usepackage{pgffor}    % for \foreach
\usepackage{blindtext} % for blind text in ch1, ch2, ch3

\begin{document}

\foreach \i in {ch1,ch2,ch3}{
        \input{ \i }
}

\end{document}

ch1.tex

\begin{center}
\Large{Chapter 1.}
\end{center}
\blindtext

ch2.tex

\begin{center}
\Large{Chapter 2.}
\end{center}
\blindtext

ch3.tex

\begin{center}
\Large{Chapter 3.}
\end{center}
\blindtext

The output is

enter image description here

3
  • The list of files is in another file.
    – egreg
    Oct 21, 2015 at 6:23
  • @egreg, what do you mean?
    – CroCo
    Oct 21, 2015 at 22:33
  • Please, read the question more carefully.
    – egreg
    Oct 21, 2015 at 22:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .