121

I want to import graphics into my main input file using the macro \includegraphics. It does not work if the filename contains spaces.

Include image with spaces in path directory to be processed with dvips also discusses this subject, but there is no solution there.

My compilation routine is latex->dvips->ps2pdf (because of PSTricks).

  • 1
    Is it the path that contains spaces (e.g., with the \graphicspath command), or the filename? Those aren't the same. – frabjous Jan 7 '11 at 3:43
  • @frabjous, for generalization, both \graphicspath and \includegraphics contain paths with spaces. – xport Jan 7 '11 at 3:52
  • @TH., there is no solution there :-) – xport Jan 7 '11 at 3:53
  • Jimi Damon wonders which of the below solutions work if the filename contains a comma in the name. Comments? – Benjamin McKay Jul 19 '16 at 20:27
43
+250

Try etoolbox package:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\Ginclude@eps}{"#1"}{#1}{}{}
\makeatother
...
\includegraphics{"file name.eps"}

The macro \Ginclude@eps is defined in the file dvips.def. It uses \special{PSfile="#1"\space ... which causes problems if additional "" are used. I patched this, such that it behaves like it was written PSfile=#1 and it worked for me in a test with an eps file with a space in the name and dvips.

  • Thanks for answering. However, it does work only when we don't use \graphicspath. – xport Jan 16 '11 at 1:19
  • @xport: I'm glad to hear that it works. Since \graphicspath was not part of the question, it did not test with it. I'll do later. – Stefan Kottwitz Jan 16 '11 at 2:20
111

Use package grffile:

\usepackage{graphicx}
\usepackage[space]{grffile}
[ ... ]
\includegraphics[...]{A file with spaces}
  • 1
    all permutations have been tried, but none works. :-) – xport Jan 7 '11 at 7:11
  • 2
    It works perfectly for me, and the @stefankottwitz doesn't. – Peregring-lk Feb 3 '17 at 22:36
  • @xport According to the doc of grffile, to support whitespace in the filename, one should use pdfLaTeX. – Eli4ph Aug 15 at 2:33
38

A simpler solution would be...

\includegraphics{{"../Current folder/1.This file"}.png}

(For Windows users,) note that the slashes all need to be forward slashes.

  • 1
    It does not work. The following kind of works: \includegraphics{{C:/Users/User/Desktop/b c}.jpg} - It includes the correct graphics file, but also prints " <space> c ". – Antonio Sesto Nov 29 '16 at 14:14
  • The printing is a part of the package I think. But I have used my solution in multiple documents of different types and they all worked. It's strange it didn't work in yours. – Argyll Dec 4 '16 at 23:02
  • 2
    Sorry for not updating earlier. This has worked without any issue: \includegraphics{{"C:/Users/User/Desktop/b c"}.jpg}. The graphics file was 'b c.jpg' in the Desktop. – Antonio Sesto Dec 5 '16 at 14:48
  • The ../ part is for relative path. – Argyll Dec 8 '16 at 15:04
  • 1
    Works great in overleaf! Wonder why png is outside of quotes and brackets? – Ben Usman Mar 29 at 19:51
1

Since this issue was never quite sufficiently answered, I just want to add that for some commands in packages where a file path needs to be specified, if you put the space in quotations (i.e., " "), this will also work (dependent on the package and how the programmer designed the package, of course).

For example:

% Preamble
\newcommand{\relativedir}{./../../Some/Directory/Directory" "Name}
...
...
% Within the Document
\input{\relativedir/NameOfFile.tex}

This would include a file in ../../Some/Directory/Directory Name/NameOfFile.tex (e.g., in a relative directory: with the single dot ./ representing the current directory and the double-dot ../ representing a directory higher in the hierarchy).

1

Just surround the path with double quotes like this "./path with spaces/images/". That would the simplest solution.

protected by Community May 22 '18 at 5:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.