3

i need to load unknown number of "files.tex" in a folder and run LaTeX with \input command. i am trying the code below:

\bash[stdout]<br>
find . -type f | wc -l
\END

and run \input{file.tex} unknown number of times.

1
  • 2
    In random order? In a real life it is not indifferent, when you put on pants and underwear. Sep 12 '16 at 4:49
3

A pipe can be used with the shell escape feature (Option -shell-escape in TeX Live or --enable-pipes/--enable-write18 in MiKTeX):

\documentclass{article}
\newread\mypipe
\begin{document}

% Open a pipe and read the files line by line.
% Line ends are disabled by \endlinechar=-1
\begingroup
  \openin\mypipe="|find . -name \string\*.tex"
  \endlinechar=-1
  \xdef\FileList{}
  \let\do\relax
  \makeatletter
  \@whilesw\unless\ifeof\mypipe\fi{%
    \read\mypipe to \x
    \xdef\FileList{\FileList\do{\x}}%
  }    
\endgroup
% The file list is now in macro `\FileList`, each file name
% as argument to `\do`.

\def\do#1{%
  \texttt{\detokenize{#1}}\par
  % \input{#1}%
}
\FileList
\end{document}
2

Here is a \globinput macro that takes as optional argument a path (default . representing the working directory) and as mandatory argument a string to be passed onto find for searching files.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\globinput}{O{.}m}
 {
  \alexandre_globinput:fn { \tl_to_str:n { #2 } } { #1 }
 }

\ior_new:N \g_alexandre_globinput_stream

\cs_new_protected:Nn \alexandre_globinput:nn
 {
  \ior_open:Nn \g_alexandre_globinput_stream
   {% the pipe
    "|find~#2~-name~#1"
   }
  \ior_map_inline:Nn \g_alexandre_globinput_stream
   {
    \input{##1}
   }
 }
\cs_generate_variant:Nn \alexandre_globinput:nn { f }
\ExplSyntaxOff

\begin{document}

\globinput[tmp]{\*.tex}

\end{document}

Here is the output of ls -R in order to show the setup:

> ls -R
alexandre.aux   alexandre.log   alexandre.pdf   alexandre.tex   tmp/

./tmp:
a.tex   b.tex   c.txt   d.tex

Running LaTeX on the document with -shell-escape enabled will input a.tex, b.tex and d.tex. Indeed, this is what is shown in the console.

> pdflatex -shell-escape alexandre.tex 
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016) (preloaded format=pdflatex)
 \write18 enabled.
entering extended mode
(./alexandre.tex
LaTeX2e <2016/03/31> patch level 3
Babel <3.9r> and hyphenation patterns for 83 language(s) loaded.
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2016/texmf-dist/tex/latex/l3kernel/l3pdfmode.def)))
No file alexandre.aux.
(./tmp/a.tex) (./tmp/b.tex) (./tmp/d.tex) [1{/usr/local/texlive/2016/texmf-var/
fonts/map/pdftex/updmap/pdftex.map}] (./alexandre.aux) )</usr/local/texlive/201
6/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on alexandre.pdf (1 page, 10280 bytes).
Transcript written on alexandre.log.

Note that if you run on the current directory (no path argument), you'll also input the main file, which is not really desirable. A test might be added in the map to exclude \jobname.tex.

0

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.