136

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

Display Name
  • 46,933

7 Answers7

127

Use package grffile:

\usepackage{graphicx}
\usepackage[space]{grffile}
[ ... ]
\includegraphics[...]{A file with spaces}
52

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.

Argyll
  • 849
  • 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 " 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 04 '16 at 23:02
  • 3
    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 05 '16 at 14:48
  • The ../ part is for relative path. – Argyll Dec 08 '16 at 15:04
  • For me this works as well. Relief! – Johannes Linkels Nov 11 '18 at 19:09
  • 3
    Works great in overleaf! Wonder why png is outside of quotes and brackets? – Ben Usman Mar 29 '19 at 19:51
  • @BenUsman: Probably has something to do with the way the filename is parsed in includegraphics. I remember some of the graphics packages don't always require you to input file extensions. So they could all be treating the part of the filename after the last . separately -- removing and recombining the extensions when they make calls to system paths. – Argyll Apr 17 '19 at 17:08
  • 1
    This is the only thing that worked for my horrible filenames which include arithmetic symbols and parentheses. – Gouda Jun 25 '19 at 00:32
  • Works on MacOS as well. Thanks. – pms Jul 13 '23 at 17:22
47

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.

LCarvalho
  • 1,611
Stefan Kottwitz
  • 231,401
11

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.

David Carlisle
  • 757,742
  • 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! – Igal Tabachnik Feb 13 '20 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 '20 at 00: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).

CarLaTeX
  • 62,716
1

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

KareemJ
  • 147
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