1

Have anyone an idea how i can use \IfFileExist{}{}{} for file names with spaces?

f.e. didn't work

\newcommand{\runI}{D:/Test/run1 - T2}
\newcommand\IG[2][]{\IfFileExists{#2}{\includegraphics[#1]{#2}}{\fbox{File #2 doesn't exist}}}

\begin{figure}[htbp]
\centering
\IG[trim = 40mm 10mm 40mm 2mm,clip = true, width=0.4\textwidth]{\runI/LatexTestFig2.png}
\caption{run 1}
\label{overflow}
\end{figure}

f.e. work

\newcommand{\runII}{D:/Test/run1-T2-Kopie}
\newcommand\IG[2][]{\IfFileExists{#2}{\includegraphics[#1]{#2}}{\fbox{File #2 doesn't exist}}}

\begin{figure}[htbp]
\centering
\IG[trim = 40mm 10mm 40mm 2mm,clip = true, width=0.4\textwidth]{\runII/LatexTestFig2.png}
\caption{run 1}
\label{overflow}
\end{figure}

what can I do? THX

cgnieder
  • 66,645
  • 1
    Completely untested and just an idea: have you checked out the grffile package? http://ctan.mirrorcatalogs.com/macros/latex/contrib/oberdiek/grffile.pdf – bonanza May 27 '14 at 07:31
  • As graphics will give a warning here anyway, is there a reason you are adding a test by hand? – Joseph Wright May 27 '14 at 07:42
  • grffile would have been my first guess as well, but remember: spaces in filenames are evil http://chat.stackexchange.com/transcript/message/13311129#13311129 – Johannes_B May 27 '14 at 07:42
  • 1
    The kpathsea file library used by most TeX distributions allows " to be used to quote a filename containing bad characters like spaces so "\runI/LaTeXFile.png" should work – David Carlisle May 27 '14 at 08:28
  • @DavidCarlisle I made a test and needed the analog of "\runI/LaTeXFile".png. But this is on TL2010, if it matters. –  May 27 '14 at 14:03
  • @jfbu ah yes the includegraphics macro needs to see the extension but that's me not kpathsea:-) Thanks for the correction. the underlying file access doesn't much care where the " are as long as they surround the spaces – David Carlisle May 27 '14 at 14:27

1 Answers1

4

From 2019-10-01 releases, LaTeX automatically uses the " quoting feature in the background so all of these should work without the user needing any explicit quotes

\IfFileExists{space name.foo}{yes}{no}

\input{my tex file}

\includegraphics{my picture}

\includegraphics{my picture.png}

Despite adding this feature it is still recommended that users don't put spaces in filenames.


Original answer

The kpathsea file library used by most TeX distributions allows " to be used to quote a filename containing bad characters like spaces so

 "\runI/LaTeXFile.png"

should work for \IfFileExists however as pointed out by jfbu the \includegraphics macro would see that as an unknown extension .png" so for \includegraphics you would want

 "\runI/LaTeXFile".png

To quote the spaces while leaving the .png extension visible.

David Carlisle
  • 757,742