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



\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
    You can try \string~ for the tilde and enclosing the whole file name in a pair of double quotes " for the space. – egreg Nov 15 '11 at 14:22
  • @egreg: this should be an answer! – user2478 Nov 15 '11 at 15:16
  • @Herbert Martin's already covers it. – egreg Nov 15 '11 at 15:20
  • See also David's answer on how to quote special characters in tex.stackexchange.com/a/53646/175 – Charles Stewart Jun 28 '13 at 10:34

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:


% ...


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:


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.

  • 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

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

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

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

% …
  • 2
    Doesn't seem to work on Windows: 'LaTeX Warning: File `../x y.pdf' not found on input line 55.' – koppor Sep 2 '15 at 23:17
  • Neither method works for me on windows. – Martin Argerami Nov 28 '19 at 0:47

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.