3

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}
rpapa
  • 12,350

3 Answers3

7

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}
egreg
  • 1,121,712
  • 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 '15 at 08:12
  • @rpapa \tl_set_from_file:Nnn is essentially the same as \CatchFileDef. – egreg Oct 19 '15 at 08:55
3

After the texlive update of 2024-03-28 you may use the l3sys-query tool (which is allowed in the restricted shell escape, so does not need --shell-escape).

The following small example would input all files matching abc*.tex or would print a notice that there were no such files. The comand has several options to recursively search subdirectories, or to sort the results, but a basic use is:

\documentclass{article}
\usepackage{l3sys-query}
\begin{document}

\QueryFilesTF{abc.tex} {\input{#1}\clearpage} {\section{input files matching \texttt(abc.tex}} {\textbf{There are no matching files}}

\end{document}

David Carlisle
  • 757,742
0

Good morning I answer my question with the solution that I finally adopted.

I think using csvsimple with the command

\csvreader{liste.dat}{}{....}

allows me to have direct access to my list stored in liste.dat

The code below is an example of what I use to import images from a directory.

I was bothered by some file names that contained underscores _ hence the use of math mode to write the file name. I also need to resolve names with accents.

\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{geometry}
\geometry{a4paper}
\geometry{margin=1cm}

\usepackage{graphicx} \usepackage{tikz} \usetikzlibrary{positioning}
\usepackage{csvsimple}

\begin{document}

\csvreader{liste.dat}{}{ \begin{tikzpicture} \node(img){ \includegraphics[width=0.45\textwidth]{\csvcoli}}; \node[below=1em of img]{$\csvcoli$}; \end{tikzpicture} \hspace{2em} }

\end{document}

enter image description here

rpapa
  • 12,350
  • Off topic, but don't use $ to get italics. The spacing is wrong because TeX treats it as the product of single letter variables. Use \emph. But then you'll probably need to look into how to handle the _ in the top right image. – Teepeemm Mar 30 '24 at 16:52