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.



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.


pdflatex ver1

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


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.



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


%accept a command line argument

\expandafter\def\expandafter\dynavar\expandafter{\dynavar { }file1}
  • 4
    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, 2014 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
    – pyCthon
    Mar 21, 2014 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, 2014 at 14:57
  • @SeanAllred true python would be awesome, my username has py, i would use it if i could
    – pyCthon
    Mar 22, 2014 at 5:04

3 Answers 3


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

  \typeout{No command-line value specified for \string\dynavar. Using 'defaultval'.}%

% ========== You would not need this
% ========== You would not need this



\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}


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


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:


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


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, 2014 at 1:22

You must log in to answer this question.

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