Handling graphic files from a common directory in LaTeX


I always prefer to place all graphic files under a common directory, for example GraphicsDir, because it is the easy way to manage graphic files at one location and can be reused in multiple reports.

But in latex documents we use includegraphics command to include images in the document. The includegraphics command look for the graphic file in the directory where tex file is present. So the flexibility of having a common directory for all images is constrained by includegraphics command.

Here are some options to include graphics from some where instead of local directory

  • declare the graphicspath command in preamble, to instruct LaTeX where should it look for graphic files to be included.
    To look for images one level above to the current directory

    \graphicspath{{../GraphicsDir/}}

    To look for images in GraphicsDir at home directory

    \graphicspath{{~/GraphicsDir/}}

    Be sure to place doublebraces.
    To look for graphic files at multiple locations say GraphicsDir, SimResultsDir, ExpResultsDir

    \graphicspath{{~/GraphicsDir/}{~/SimResultsDir/}{~/ExpResultsDir/}}

    Note: double-slash (//) on the end of the search path will help in to recursively search the directory. But I did not use it.
    \graphicspath is inefficient in the way in searches for files and expensive in terms of memory[more…]. Therefore I adopted the following approach to overcome the problems associated with graphicspath macro.

  • The environmental variable TEXINPUTS controls where LaTex should search for input files. By default LaTex search for files in the current directory and after that based on the values in TEXINPUTS variable. We can alter the search sequence by setting the TEXINPUTS.
    For example setting

     TEXINPUTS=.:~/GraphicsDir:$TEXINPUTS

    searches for files in current directory followed by GraphicsDir in home directory and then as per by earlier TEXINPUTS settings.

    The variable TEXINPUTS can be set by either of following methods if you are using bash shell. For others follow the instructions here.

    • edit the .bashrc file and add
      export TEXINPUTS=.:~/GraphicsDir/:$TEXINPUTS

      “.” structs the latex to look for graphic files in current directory, and then in ~/GraphicsDir.
      After that source the .bashrc file in the terminal

      source .bashrc
    • from command line
      echo 'export TEXINPUTS=.:~/GraphicsDir/:$TEXINPUTS' >> ~/.bashrc

    To check that TEXINPUTS are updated, type in the terminal

    echo $TEXINPUTS

To include a graphic file in latex,

begin{picture}
\includegraphics{Report/file1.eps}
end{picture}

Now LaTex first look for Report/file1.eps in the current directory. If it is not found then LaTex look for Report/file1.eps in GraphicsDir at home directory. By this way we can place all the graphic files at a common location and the same files can be used in multiple reports.

Leave a Comment