21

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

4
  • 4
    You can try \string~ for the tilde and enclosing the whole file name in a pair of double quotes " for the space. Commented Nov 15, 2011 at 14:22
  • @egreg: this should be an answer! Commented Nov 15, 2011 at 15:16
  • @Herbert Martin's already covers it. Commented Nov 15, 2011 at 15:20
  • See also David's answer on how to quote special characters in tex.stackexchange.com/a/53646/175 Commented Jun 28, 2013 at 10:34

3 Answers 3

20

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.

7
  • Actually it's generated through Sweave so I can't avoid tildes and spaces, but thanks this works fine ! :) Commented Nov 15, 2011 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. Commented Mar 11, 2013 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" Commented May 16, 2015 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. Commented May 18, 2015 at 20:59
  • For me, only \includepdf{\detokenize{"spaces are fun".pdf}} worked, not braces. Commented Jan 21, 2019 at 10:25
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}} 
2
  • 2
    Doesn't seem to work on Windows: 'LaTeX Warning: File `../x y.pdf' not found on input line 55.' Commented Sep 2, 2015 at 23:17
  • Neither method works for me on windows. Commented Nov 28, 2019 at 0:47
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} 
1
  • 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. Commented Oct 12, 2021 at 21:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.