I want an easy (automatic) way to copy all image files called by latex into as single directory. A similar issue was addressed here: Create list of all external files used by master LaTeX document? and I like the solution proposed by @imriss except that it requires all files to reside in the same directory as the tex file to work. Some of my image file reside elsewhere and are accessed through TEXINPUTS and are not found by this technique because the dep file produced by snapshot only contains the names of the files, not their path.

  • You should probably look up \graphicspath see texdoc.net/texmf-dist/doc/latex-dev/graphics/grfguide.pdf section 4.5 – daleif Mar 23 '20 at 17:22
  • 1
    To get a list of all used graphic files, with full file path: Compiling with pdflatex -recorder xxx.main will create an aux file xxx.fls which contains a full (with repetition) list of input and output files, with full file path. Then parse this xxx.fls by file extensions. – muzimuzhi Z Mar 23 '20 at 20:30

Building of @muzimuzhi Z's comment and @imriss' answer to the question Create list of all external files used by master LaTeX document?, here is way to copy all external files used by a latex doc to the directory img, based on a choice of file extensions.

Starting with a latex file file.tex type

pdflatex -recover file

which creates file.fls. Create a file, say copy_fig_fls.py, containing the following python code:

"""Copy figures used by document."""
import os
import shutil

FLS_FILE = 'file.fls'
TARGET_DIR = 'img/'
EXTENSIONS = ['pdf', 'png', 'jpg']

def copy_image_files():
    with open(FLS_FILE, 'r') as f:
        for line in f:
            if 'INPUT' not in line:
            source = line.split(' ')[1].rstrip()
            _, e = os.path.splitext(source)
            e = e.lower()[1:]
            if e in EXTENSIONS:
                shutil.copy(source, TARGET_DIR)

if __name__ == '__main__':

Run it by typing

python3 copy_fig_fls.py

It will copy all the files used by file.tex with extensions listed in the EXTENSIONS variable into the img directory, irrespective of where the original image file resides, in the current directory or elsewhere.

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.