I am writing a Latex file that needs to include figures from a directory graphs.

graphs has several sub-directories say, one , two and three. These individually contain several graphics that should be included in the file.

One way is to include every graphic in the file manually. But, that is too cumbersome. Is there a better way to do this?

  • 5
    So you want to include all graphic files from certain directories automatically? In this case see How to iterate through the name of files in a folder, which looks like a duplicate. The adjustments to multiple directories should be easily done. Commented Dec 20, 2011 at 10:00
  • @MartinScharrer: Ah. You beat me to the punch.
    – night owl
    Commented Dec 20, 2011 at 10:08
  • 1
    You question is not well-defined at the moment. Do you only want to reduce the amount of work, e.g. not have to write the full directory names all the time, or do you want to include all graphic files from a certain directory structure automatically? I personally think it is the later, but looking at the two answers so far other people think more about the former. In that case Automatically Locate Included Images looks like a duplicate. Commented Dec 20, 2011 at 10:35
  • @Martin: Yea, I guess the question could be a tad bit more specific at what is the optimal goal here. Whether to reduce the typing or have it auto included.
    – night owl
    Commented Dec 20, 2011 at 10:49
  • @MartinScharrer. My main motive here is to reduce the amount of work as I have about 100 plots to include and including each of them one by one is a pain in the bottom
    – Ankit
    Commented Dec 21, 2011 at 3:29

2 Answers 2


Put into your preamble:


makes sense if you do not have too many subdirectories. Every path is enclosed in braces.

  • Hi, But how do I include the files then as each one, two etc have the same file name like a.pdf and b.pdf
    – Ankit
    Commented Dec 21, 2011 at 3:26
  • @Ankit: you can use \graphicspath several times in your document to define other search rules.
    – user2478
    Commented Dec 21, 2011 at 8:08
  • When using \graphicspath some common file names are somehow reserved by the system (left.pdf, hand.png). The list of reserved names is moving such that you should always use ./file name instead of just file name in command arguments. Commented Nov 18, 2021 at 9:28

Here is a solution. Comments will be added later.

            \@latex@error{Unknown processor \unexpanded{##1}}\@ehc
      % \openin will not take spaces in path or filename:
          \@latex@error{Inputpath '##1' not ended with slash (/)}\@ehc
  cmd/\needvalue{viewport}/0 0 100 100;
% Why didn't I want to say \let\fim@phantomenv\relax?
  \usename{ltxkeys@\fim@setorlaunch keys}[KV]{importfiles}{#1}%
  % Some paths might have been suggested via \input@path
  % and/or \graphicspath. So we heed them here.
      % Hmm, why don't I want to use \IfFileExists?
        \typeout{File '\fileonpath' found}%
    \@latex@error{Command \noexpand\fiminclude cannot be nested}\@ehc
    % I don't know why LaTeX chose to do a loop here. We don't need it here:
      \immediate\write\@mainaux{\string\@input{\[email protected]}}%
      \immediate\openout\@partaux\[email protected]
    \@input@{\[email protected]}%

\section{Test section}

  processor   =includegraphics,
  viewport    =20 21 590 400,
  scale       =.25,
  inputpaths  ={D:/MyTempDoc/Path1/,D:/MyTempDoc/Path2/},
  stuffbefore =\bigskip\centering Importing \currfile\space from path \currpath\endgraf,
  stuffafter  =\endgraf\centering File import succeeded (\currfile).\endgraf

  processor   =include,
  inputpaths  ={D:/MyTempDoc/Path1/,D:/MyTempDoc/Path2/}

  processor   =\input,
  inputpaths  ={D:/MyTempDoc/Path1/,D:/MyTempDoc/Path2/}

This is only a partial listing of the code. The full version can handle stand-alone documents in the manner of docmute and standalone packages. I will release it as a package.

enter image description here enter image description here


You must log in to answer this question.

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