What commands with the embedfile package will embed an entire tree of source files into the output from pdfLateX?

The sort of tree, in very simplified form, that I have is as follows, where (d) denotes a directory:

              /          |             \
       preamble (d)   main (d)     one.bib,two.bib biblatex.cfg, ...
                     /     |        \
               chap1 (d)  chap2 (d)  chap3 (d)
              /     \        :          :
         sec11.tex  sec12.tex

That is, aside from the main .tex file there are individual files in the same top-level directory as mainsourcefile.tex. That top-level directory has several subdirectories (for preamble, frontmatter, mainmatter, backmatter). And the mainmatter subdirectory in turn has subdirectories for individual chapters, each such subdirectory consisting of multiple .tex files (one for each section of the chapter).

The skeleton of the tree structure is also indicated by the form of mainsourcefile.tex:



% frontmatter.tex inputs several files

% mainmatter.tex includes main/chap1/chap1.tex;
%      chap1.tex inputs sec11.tex, sec12.tex. etc.
% mainmatter.tex includes main/chap2/chap2.tex; etc.

% backmatter.tex inputes back/indexes.tex, back/bibs.tex.


In the documentation for embedfile I could not find sufficient examples guiding me how to embed such a tree of files.

  • Since posting my question, I've found it sufficient for my purposes to use the snapshot package to produce a list of all files used (even though it does nothing likeembedfile.
    – murray
    Jun 18, 2017 at 14:29
  • 1
    Similar question: Is there some way to embed LaTeX source code in a PDF file? with embedall as solution.
    – koppor
    Mar 5, 2018 at 18:47
  • Maybe \newcommand{\includeFile}[1]{\input{#1}\embedfile{#1}} or use renewcommand
    – Capt_
    May 11, 2021 at 10:17

1 Answer 1


Except for the shell escape feature (running an external program) LaTeX cannot read directory listings. Also, if a file is included it is not known to the TeX language, where the file comes from. A file can be in the current directory, below the current directory, or it can come from a standard location.


The .log file contains file names with path, but parsing it is no fun because of the undefined structure and the hard line breaks. However, many TeX compiler (pdfTeX, XeTeX, LuaTeX) provide a file name recorder by option --recorder. This can be used to detect the input file paths. For example, this is used by the project mkjobtexmf.

The procedure would be as follows:

  1. Run mkjobtexmf to generate a directory, which contains all files, used by the LaTeX compilation.

  2. Delete unwanted files and maybe add other important source files.

  3. Generate an archive file (zip and friends).

  4. Include the archive file by embedfile. I do not think it make to much sense to include the sources as many single files.

Version control system, e.g. git

An easier way to get all source files is to ask the version control system for it. As an example, git is used.

  1. Put the project into version control:
    cd "Project directory"
    git init .
  1. Add the source files (git add) and commit (git commit).

  2. Ask the version control system for the files. git makes it easy by its archive feature:

    git archive --format=zip -o project.zip master

This puts all versioned files of branch master to the archive file project.zip.

  1. Include the archive file with embedfile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.