0

I would like to be able to input a file by stating only a directory name.

There would be a directory "mydir" containing several tex files, and I would like to be able to type:

\input{mydir}

and have some *.tex file selected and put into my document.

11
  • unless you use shell escape tex can not read the directory. So it is better to use ls (or dir on windows) to get a directory listing to a known file name and then read that with tex to input the files. Jun 15, 2017 at 15:57
  • One file at random?
    – egreg
    Jun 15, 2017 at 16:00
  • I'd be happy if any tex file could be inputed, merely by specifying the directory (with no knowledge of the name of the file in the directory)
    – Bart Snapp
    Jun 15, 2017 at 16:27
  • 1
    Since latex cannot read the contents of the dir, your probably better of having predictable file names and then looping over such a list combined with \InputIfFileExists . For instance naming the files myfileN.tex where N is a number, and knowing an upper limit to the number of files makes it easy to incluse also of those using a loop.
    – daleif
    Jun 15, 2017 at 16:52
  • @BartSnapp Any, some or all? You're not trying to explain yourself.
    – egreg
    Jun 15, 2017 at 17:07

2 Answers 2

3

You can do that using LuaLaTeX:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{luacode}

\begin{luacode*}
    function input_dir(dir)
        local p = io.popen('find "./'..dir..'" -type f -name "*.tex"')  
        for file in p:lines() do                  
            print("Ajout de "..file)
            tex.print("\\input{"..file.."}")       
        end
    end
\end{luacode*}

\begin{document}

\directlua{input_dir("mydir")}

\end{document}

Compile it with lualatex --shell-escape yourfile.tex

0
3

You could try texosquery if you have Java installed.

Example (where the sub-directory is called subdir):

\documentclass{article}

\usepackage{texosquery}

\makeatletter

% \inputfiles{dir}
\newcommand*{\inputfiles}[1]{%
 \TeXOSQueryRegularFileList{\result}{,}{#1}%
 \ifx\result\empty
   Query failed!
 \else
  \@for\thisfile:=\result\do{\input{#1/\thisfile}}%
 \fi
}

\makeatother

\begin{document}

\inputfiles{subdir}

\end{document}

That will input all regular files in the subdirectory. Alternatively you could apply a filter:

\documentclass{article}

\usepackage{texosquery}

\makeatletter

% \inputfiles{dir}
\newcommand*{\inputfiles}[1]{%
 \TeXOSQueryFilterFileList{\result}{,}{.+\string\.tex}{#1}%
 \ifx\result\empty
   Query failed!
 \else
  \@for\thisfile:=\result\do{\input{#1/\thisfile}}%
 \fi
}

\makeatother

\begin{document}

\inputfiles{subdir}

\end{document}

This will only match files ending with .tex.

Note: texosquery may require some setting up before first use. There should be a file called texosquery.cfg in your TeX distribution, which contains instructions on how to set it up. It will need editing if you have Java 8 (especially if you want to use it with the restricted shell escape) or if you have Java 5 or 6. The default is set up for Java 7 and unrestricted mode only.

0

You must log in to answer this question.

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