# dynamically imports tex files

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}

• You can use a "command-line" approach using pdflatex \def\dynavar{abc} \input{file} to have \dynavar accessible inside file.tex. I'm not sure I understand the usage of file1.tex and file2.tex and how it fits in with your command-line request/approach.
– Werner
Mar 21 '14 at 4:11
• @Werner thanks i'll update the question so it makes more sense why i'm doing this approach, if you know a better way please let me know Mar 21 '14 at 12:22
• Good question, but this is certainly one of those instances where I would properly script a solution with Python or something similar. Mar 21 '14 at 14:57
• @SeanAllred true python would be awesome, my username has py, i would use it if i could Mar 22 '14 at 5:04

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

Consider a workflow with git. See here for more information. You could tag each version. That is, after a commit type,

git tag -a "v1" -m "This version has all the bare bones necessary for the project"


Then more commits and more tags, then all you have to do is

git checkout -b v4_branch tags/v4


and your working directory will change to the status it was at the time when you tagged "v4" previously and you will be on a new branch called v4_branch. Read here for more about checking out tags correctly (there are a few different ideas about the best way to do this).

(just now realized that this solution does not utilize command-line input as poster specified)

Another solution is to create a tex file that uses the \input command to input just the files for a particular version. So, for example, you could create a file called ver1files.tex, which would consist of these lines:

\input{./ver1file1.tex}
\input{./ver1file2.tex}
\input{./ver1file3.tex}
...
\input{./ver1file100.tex}


Then, in your main LaTeX document, you can easily input all of your ver1 files simply by inputting your ver1files.tex file, like so:

\input{./ver1files.tex}


You can create a ver2files.tex that contains \input commands for all the ver2 files, and so on and so forth for all your versions.

Note that LaTeX allows you to "nest" \input commands like this. That is to say, you have an \input in your main LaTeX file that inputs a file that contains additional \input commands. That's fine to do with \input. However, you cannot utilize this type of nesting with the \include command.

If your main LaTeX file is not in the same folder as your ver1file1.tex and ver1files.tex etc. files, then be sure you update the path in the \input command to point to where those files actually are. I just used "./" (current directory) for the quick example here.

• Command line interaction can be added pretty simply by using the technique in Werner's answer. The only downside this brings is the creation of additional files, but these could easily be generated and then thrown away by a supervising script. Mar 22 '14 at 1:22