20

I'm trying to include a pdf in a document, using pdfpages package, but I'm having troubles when filename contains tilde or space.

e.g.

\includepdf[pages=-]{C:/Users/MYUSER~1/AppData/Local/Temp/mypdfpdf.pdf}

\includepdf[pages=-]{C:/Users/MYUSER FOO/AppData/Local/Temp/mypdfpdf.pdf}

I've tried to escape for example through $\sim$ but it doesn't work...

Is it possible to escape tilde/space in the filename ?

Thanks in advance

digEmAll
  • 537

3 Answers3

21

Note: Because newer versions of LaTeX and graphicx (and therefore also pdfpages) do support file names with spaces on there own, this answer is only of historical interest. From November 2019 grffile is an empty stub for compatibility only.

Spaces at file names of PDF files at \includegraphics or \includepdf are allowed, if you are using package grffile:

\usepackage{grffile}
\usepackage{pdfpages}
% …
\includepdf[pages=-]{test this.pdf}

And for more verbatim interpretation of the file name you may use \detokenize:

\usepackage{grffile}
\usepackage{pdfpages}
% …
\includepdf[pages=-]{\detokenize{test~this.pdf}}
Schweinebacke
  • 26,336
19

About the tilde: Apparently the ~ is taken in its usually meaning, not as literal ~. I get a Package pdfpages Error: Cannot find file `test\nobreakspace {}it' error for a test~it file. Taking the filename verbatim should help. This can be done by \Verbdef from the newverbs package:

\usepackage{newverbs}

% ...

\Verbdef\filename{C:/Users/MYUSER~1/AppData/Local/Temp/mypdfpdf.pdf}
\includepdf[pages=-]{\filename}

It is also possible to write \string~ instead of ~ every time. You shouldn't use math symbols to "escape" these characters in filenames. Typesetting and file reading are two completely different things.

Finally you can use the e-TeX primitive \detokenize{...} to sanitize the file name:

\includepdf[pages=-]{\detokenize{C:/Users/MYUSER~1/AppData/Local/Temp/mypdfpdf.pdf}}

To allow spaces you need to wrap the filename in " ". At best you should avoid any spaces, tildes or any other special characters in filenames.

Martin Scharrer
  • 262,582
  • Actually it's generated through Sweave so I can't avoid tildes and spaces, but thanks this works fine ! :) – digEmAll Nov 15 '11 at 15:36
  • 3
    @Martin Scharrer - thanks for the \Verbdef tip. At my site I've got people who use two or more (!!!) blanks for directory names. FYI: two or more blanks in the directory name get shrunk down to one blank when using \detokenize with \includepdf. – infowanna Mar 11 '13 at 13:59
  • 1
    Also, note that wrapping the filename in " " is likely supposed to not include the extension, because I just got: LaTeX Error: Unknown graphics extension: .pdf" – sdaau May 16 '15 at 13:22
  • 1
    @sdaau: The handling of double quotes depends on the distribution and the OS as far as I know. Try to use { } instead if you have spaces. There you might want to exclude the extension, like \includepdf[...]{{some file name}.pdf}. This works because the TeX code scanes the filename and splits basename and extension. The braces protect the spaces but are removed when the content is matched during the scan. – Martin Scharrer May 18 '15 at 20:59
  • For me, only \includepdf{\detokenize{"spaces are fun".pdf}} worked, not braces. – user1338062 Jan 21 '19 at 10:25
  • @user1338062: Well, having spaces in the file name makes it even more complicated. But \includepdf{{"spaces are fun"}.pdf} should be enough in this case. See the different answer to How to include graphics with spaces in their path? – Martin Scharrer Jan 21 '19 at 10:28
  • @MartinScharrer for me also \includepdf{{"spaces are fun"}.pdf} did not work, but \includepdf{\detokenize{"spaces are fun".pdf}} did! – user1 Mar 27 '19 at 18:22
1

If you have issues while uploading files with space in its name (ie test 1.pdf , test 2.jpg etc).

Before you use the file in includepdf function, you need to add double quotes before and after the filename (ie "test 1".pdf , "test 2".jpg etc).

\usepackage{grffile}

\usepackage{pdfpages}

\includepdf[pages=-]{"test this".pdf}

  • This one actually worked for me (Windows), and without using grffile even: \includepdf[pages=-]{my/dir/"my file".pdf} - Not sure what it does with spaces in the dir name. – srs Oct 12 '21 at 21:01