[Tex/LaTex] dynamically imports tex files

import

How can I dynamically import a tex file?

I essentially have hundreds of .tex files in one folder, all of these files have multiple versions. I can't move them out of this folder.

Ex:

ver1file1.tex
ver2file1.tex
...
ver8file1.tex
...
ver8file100.tex

I want to be able to set a environment variable or pass a command line argument so that latex will be able to dynamically load all the files from a particular version.

Example:

pdflatex ver1
or
EXPORT LATEX_VER=ver1

to have only the following being imported in the main latex file.

ver1file1
ver1file2
...
ver1file100

Below is an example of what i've tried so far but I can't get the I basically want to have 2 .tex files, file1.tex and file2.tex . I want to be able to accept a command line argument that defines the name of the file to import.

main.tex

\import{c:\path}{aux}

\import{c:\path}{\dynavar} % should load variable from commandline + added string ex: ver1file1

aux.tex

%accept a command line argument
\ifdefined\myflag
  \newcommand\dynavar[1]{\emph{#1}}
\else
  \newcommand\dynavar[1]{defaultval}
\fi


\expandafter\def\expandafter\dynavar\expandafter{\dynavar { }file1}

Best Answer

Here's a small mockup of what I would suggest:

\documentclass{article}
\usepackage{filecontents,multido}
\begin{filecontents*}{dynavar.tex}
\makeatletter
\@ifundefined{dynavar}{%
  \newcommand{\dynavar}{defaultval}%
  \typeout{No command-line value specified for \string\dynavar. Using 'defaultval'.}%
}{}
\makeatother
\end{filecontents*}

% ========== You would not need this
\begin{filecontents*}{ver1file1.tex}
ver1file1
\end{filecontents*}
\begin{filecontents*}{ver1file2.tex}
ver1file2
\end{filecontents*}
% ========== You would not need this

\begin{document}

\input{dynavar}

\multido{\i=1+1}{2}{% Update '2' to (say) '100' if you have 100 files
  File \texttt{\dynavar file\i}: \endgraf \input{\dynavar file\i}\endgraf}

\end{document}

You would call the above file using

pdflatex \def\dynavar{ver1} \input{main}

to run ver1 of the files. Or

pdflatex main.tex

if you wish it to run defaultval of the files. Using \def\dynavar{ver1} as the "command line" option, you'll get the output

enter image description here