I have several plots saved as .pdf in a folder and I would like to know if there is a way of adding all of them in a document at once, without typing the path to each file.
-
3short answer no, longer answer, if you are prepared to allow latex to escape to the shell and run system commands, and don't mind your document being system dependent you could get it to run ls *.pdf(or whatever is needed) and construct the list. (or you could just do this before starting latex)– David CarlisleFeb 18, 2014 at 17:39
1 Answer
It is best to do this externally to latex (although you can use the shell-escape (write18) feature to execute the commands from within latex if you want.
I'll assume bash syntax but other command line systems can do similar.
Suppose you have a directory/folder img
with three pdf files
$ ls img
a.pdf b.pdf other.pdf
and you want to include them in alphabetic order (or any other order you can achieve by using arguments to ls
or some other command)
then executing
ls img/*.pdf | sed -e "s/.*/\\\\includegraphics{\\0}/" > img/imglist.tex
would leave a file img/imglist.tex
that looks like
\includegraphics{img/a.pdf}
\includegraphics{img/b.pdf}
\includegraphics{img/other.pdf}
so you just need \input{img/imglist}
in your main file, and arrange to regenerate imglist.tex
every time you update the image directory (or every time you run latex).
-
Could you explain how
sed
works? It gave me zeros like so\includegraphics{0}
instead of filenames (on macOS 10.15.1). I ended up just putting the unaltered filenames into a file, so your solution still worked fine. Feb 19, 2020 at 15:29 -
1@BadAtLaTeXProgramming
sed
is a stream editor. Here we use it for its regex search and replace potential. In quotation marks you can reads/match/replacewith/
, and the\0
in theory refers to the zero-th match (being.*
, that is every character, as many times as possible). You got 0 because your version ofsed
interpreted 0 as a literal character, and not the matched group 0 (which you wanted to be your filenames).– mazunkiMar 11, 2021 at 11:42 -
2as @mazunki said, the difference is probably in the shell you are using rather than sed itself, I would have tested that with bash but the number of
\\\\
you need, and whether you need to quote the{}
depends on the shell, typing regular expressions on the commandline when the shell wants to use the same special characters can be an adventure. Mar 11, 2021 at 11:54 -
1For reference, it's nice to know that even
"
(doble quotation) versus'
(single quotation) marks make a difference in Bash, which is what many use. Usually, double marks means Bash should work with the string inside, while single marks are plain text from Bash' point of view. Test this: Assign a variable (export variable=hello
) and runecho "$variable"
andecho '$variable'
.– mazunkiMar 11, 2021 at 12:20 -
I had the same problem (on macOS 13.1) with the output being just
\includegraphics{0}
for every file. A quick skim of the man file forsed
showed that I could use an&
for the replacement. The followingsed
command worked for me... replace the\\0
with&
like so:sed -e "s/.*/\\\\includegraphics{&}/"
– SteveFeb 24 at 16:14