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.

Bart Snapp
  • 1,339

2 Answers2

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

JPG
  • 2,019
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.

Nicola Talbot
  • 41,153