132

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).

5
  • possible duplicate of Include image with spaces in path directory to be processed with dvips
    – TH.
    Jan 7, 2011 at 3:35
  • 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, 2011 at 3:43
  • @frabjous, for generalization, both \graphicspath and \includegraphics contain paths with spaces.
    – xport
    Jan 7, 2011 at 3:52
  • @TH., there is no solution there :-)
    – xport
    Jan 7, 2011 at 3:53
  • Jimi Damon wonders which of the below solutions work if the filename contains a comma in the name. Comments? Jul 19, 2016 at 20:27

7 Answers 7

46
+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.

4
  • Thanks for answering. However, it does work only when we don't use \graphicspath.
    – xport
    Jan 16, 2011 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, 2011 at 2:20
  • 1
    Can you give a full example? I cannot make it work.
    – Gqqnbig
    Nov 2, 2019 at 0:13
  • @Gqqnbig this patch against an old 2011 version of the code is not suitable. In current latex releases spaces in filenames should work with no special code needed. Dec 28, 2019 at 15:40
123

Use package grffile:

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

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.

8
  • 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 ". Nov 29, 2016 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, 2016 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. Dec 5, 2016 at 14:48
  • The ../ part is for relative path.
    – Argyll
    Dec 8, 2016 at 15:04
  • 2
    Works great in overleaf! Wonder why png is outside of quotes and brackets?
    – Ben Usman
    Mar 29, 2019 at 19:51
9

In the LaTeX2e 2019-10-01 Patch Level 2 release that should be arriving in distributions any day soon, you can use

\includegraphics[...]{A file with spaces}

just using the core graphicx package, no additional packages or quoting of spaces should be needed.

The same is true with \input{A file with spaces} which will input A file with spaces.tex

Despite the fact that some effort has gone into making this work, it is still a good idea not to use spaces in filenames.

2
  • Thank you for this comment! I had a slightly different problem: my PDFs suddenly stopped rendering images! The output log showed some extraneous quotes around the image path. I realized I had \usepackage[multidot]{grffile} in my preamble, after removing it - the images began rendering again! Feb 13, 2020 at 22:17
  • Works fine with .pdf pictures, but when trying to include .eps pictures I'm getting rectangle box instead of the figure. Replacing spaces with underscores solves the issue.
    – Lev K.
    Jul 23, 2020 at 0:45
4

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.

0

this post is quite old now but I encountered the same problem when compiling with biber. For me the solution was to provide the path to \addbibresource replacing 'Folder with spaces' with 'Folder*'. So for example:

\addbibresource{D:/OneDri*/LaTeXFiles/bibliography/biblio.bib}

Just make sure that there are no other folders that starts with 'Folder' under the same folder level (here no other folder under D:/ should start with OneDri)

Cheers, Piotr

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