6

I have a TeX document that is embedding a huge amount of figures from a fig folder that is full of another humongous amount of other figures.

I would like to move the document along with its figures, but I don't want to take the whole fig folder, just the figures in there that are embedded by the document.

Is there a package (or any other way) that allows me to, for example, print out the filenames of all the figures embedded by the document? Something like:

fig/figurename1.eps
fig/figurename2.eps
...
etc

I could then just copy-paste that list and add some cp's in a plain text file and automate the copy of those files to my desired location by running the file in my shell:

$ cp fig/figurename1.eps wherever/fig/
$ cp fig/figurename2.eps wherever/fig/
...
etc

Of course this is just an idea, if somebody has an alternative to the "file listing and copy-pasting in a shell file" I would be happy to listen =)

  • 1
    More of a Unix question than a TeX one, but the following (untested) should work assuming no spaces in your filename: for name in `grep includegraphics document.tex | cut -d{ -f2 | cut -d} -f1`; do cp $name otherfolder; done – Mike Renfro Sep 26 '14 at 15:45
  • It tells me cp: incorrect option -- «{» – Xirux Nefer Sep 26 '14 at 15:55
  • Odd. Replace the cp with an echo to see what the for loop is using, but the two cut commands should have extracted whatever was between the {} characters on an \includegraphics line. Or use one of the suggestions below. – Mike Renfro Sep 26 '14 at 16:15
  • David Carlisle has given a solution here. Won't it be relevant to this question? – abyshukla Jan 13 '17 at 4:47
  • @manucpp err... this question was asked Sep 26 '14 at 15:34 and already has an accepted answer from Sep 26 '14 at 15:55. The answer you link to is from Oct 8 '14 at 21:27. I didn't have a time machine at the time I asked this.... all are equally valid answers though. – Xirux Nefer Jan 13 '17 at 20:40
8

You can modify the \includegraphcs macro to add to a list and print the list at the end of the document. The MWE below yields the following output:

Figures included were
 images/figA.jpg
 images/figB.png

Notes:

  • In this case \let would have work as well (as per egreg's comment at Resize all images in Latex to a percentage width), but I have gotten used to using \LetLtxMacro from the letltxmacro package for macros which have optional parameters. A detailed description of \LetLtxMacro can be found at this question about a closed square root symbol.

  • The [demo] option is used so as to place a black box where the figure would go for demo purposes, in your real usage (when you actually have the figures available), you need to remove this option.

  • If you wanted to you could use \immediate\write18 and execute the cp shell command within the \foreach loop and have a directory at the end of typesetting which has the images that were included. No further processing would be required.

Code:

\documentclass{article}
\usepackage[demo]{graphicx}% Remove [demo] option in real usage.
\usepackage{letltxmacro}
\usepackage{pgffor}


%% https://tex.stackexchange.com/questions/14393/how-keep-a-running-list-of-strings-and-then-process-them-one-at-a-time
\newcommand\FigList{}
\newcommand\AddFigToList[1]{\edef\FigList{\FigList#1,}}

\LetLtxMacro{\OldIncludegraphics}{\includegraphics}
\renewcommand{\includegraphics}[2][]{%
    \AddFigToList{#2}%
    \OldIncludegraphics[#1]{#2}%
}

\newcommand*{\ShowListOfFigures}{%
    \typeout{Figures included were}%
    \foreach \x in \FigList {%
        %\par\x% <-- uncomment if you want the list in the PDF as well
        \typeout{ \x}
    }%
}
\AtEndDocument{\ShowListOfFigures}

\begin{document}
\includegraphics{images/figA.jpg}

\includegraphics{images/figB.png}
\end{document}
  • I get the following error: ! LaTeX Error: Option clash for package graphicx. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ...l.25 \usepackage{letltxmacro} ? – Xirux Nefer Sep 26 '14 at 16:10
  • @XiruxNefer: I assume that is in your actual use case and not the MWE above. Did you remove the demo option? – Peter Grill Sep 26 '14 at 16:11
  • I removed the demo option now, and it compiles, but it doesn't show the list of embedded figures. I guess it should be the last thing in the document, just after the references list, but I have searched the whole document and there is nothing printed =( – Xirux Nefer Sep 26 '14 at 16:23
  • @XiruxNefer: If you want it printed in the doc the uncomment the line \par\x. I designed it to be printed at the end in the log/console output which is what \typeout does - easier to cut and paste from there. – Peter Grill Sep 26 '14 at 16:24
  • Oh my, it is also not printing anything to the terminal =( =( =(. Agh, sorry for all the time you are investing in this. Infinite thanks. I'll try uncommenting the line \par\x. – Xirux Nefer Sep 26 '14 at 16:36
1

This is not TeX related question.

You can open the folder in Nautilus for example, select the figures you want and press Ctrl+C. Then open some text editor, such as Kate or gedit and press Ctrl+V. There you have it - the full paths of all of your selected figures.

Another way is to get all the informations from the .tex file. Here are the commands:

$ cd /path/to/your/dir
$ sed '/include/!d' yourfile.tex > outputfile
$ sed -i 's/.*{//g' outputfile && sed -i 's/}//g' outputfile

The first sed will delete all lines which do not have the string "include". The second and third sed will leave your output file with the bare filename you had in your .tex file.

0

On Linux, something like this will do, without modifying the source:

strace -fe open  make 2>&1  1>/dev/null | grep plots | sed 's/.*\"\(.*\)\".*/\1/' > plots.list

Where :

  • make is the command you use to build your output (could be some variation of pdflatex file.tex).

  • strace displays the syscalls. With the options we are providing, we keep only the "open" syscalls (-e option) in order to get the figures that are inserted (as well as other opened files foe now). The -f options is to display syscalls of subprocesses and the redirections are to keep only the strace output.

  • grep plots selects the lines containing a common string in the path ("plots" in this case).

  • sed selects what is inside the quotes.

  • The final redirection dumps the result in a file.

You may get duplicate filenames this way, but could be good enough depending what you want.

0

There is a perl script on CTAN, texdepend that does all of this and more. It handles all sorts of internal and external dependencies, and can generate the information in Makefile, perl or 1-per-line format.

>texdepend
Find LaTex dependencies, Version: 0.96, Michael Friendly (friendly@yorku.ca)
Usage: C:\batchfiles\texdepend.pl <options> texfile[.tex]
  where <options> may be abbreviated to unique truncations, and are:
   -help               print this measly help
   -expand             expand package/include file to full path names
   -format = make      print dependencies in Makefile format
             perl      print in the form of perl assignments (LatexMk)
             1         print one per line (with # separators)
   -ignore = list      list of file types to be ignored in .log [default: fd]
   -out = filename     send output to filename
   -print =            Any one or more of i (includes) p (packages)
                       f (figs) b (bibfiles) s (styles) d (all dependencies)
   -styles = list      list of file types for  from .log [default: sty]
   -verbose

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.