[Tex/LaTex] \includegraphics with spaces, dots and underscores

includegraphicsluatexunderscore

I've already tried 10 billion advices like using \usepackage{grffile}, wrapping the filename into {} and/or "" (in any combination).
Nothing is working!

So, I have a file with path plots/task-1/ABC123_/qa_pt:0.20-2.00/VtxZ (-10.00, 10.00)/charged; eta (-0.80, 0.80)/FB: 16; eta (-0.80, 0.80)/eff/eta_phi.pdf and I need to include it into beamer presentation, which is compiled by LuaLaTeX (if it is important).

Changing the path is not an option I can live without spaces (but can't without dots and underscores), but it is better to keep them, and there are multiple files with the same name but different paths which need to be used.

Minimal non-working example (find any .pdf file to include and place it at the path):

\documentclass[aspectratio=1610,10pt]{beamer}

\begin{document}

\begin{frame}
    \includegraphics{plots/task-1/ABC123_/qa_pt:0.20-2.00/VtxZ (-10.00, 10.00)/charged; eta (-0.80, 0.80)/FB: 16; eta (-0.80, 0.80)/eff/eta_phi.pdf}
\end{frame}

\end{document}

The error:

! Missing $ inserted.
<inserted text> 
$
l.10 \end{frame}

? 

! Package luatex.def Error: File `plots/task-1/ABC123_/qa_pt:0.20-2.00/VtxZ (
-10.00, 10.00)/charged; eta (-0.80, 0.80)/FB: 16; eta (-0.80, 0.80)/eff/eta_phi
.pdf' not found: using draft setting.

See the luatex.def package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.10 \end{frame}

? 
! Missing $ inserted.
<inserted text> 
$
l.10 \end{frame}

?

Best Answer

This works in pdflatex but fails in the lua part of luatex's inclusion (this must be the most crazy filename ever seen:-)

\documentclass[aspectratio=1610,10pt]{beamer}

\begin{document}

\begin{frame}
    \includegraphics{"plots/task-1/ABC123_/qa_pt:0.20-2.00/VtxZ (-10.00, 10.00)/charged; eta (-0.80, 0.80)/FB: 16; eta (-0.80, 0.80)/eff/eta_phi".pdf}
\end{frame}

\end{document}

This version works in lualatex and pdflatex

\documentclass[aspectratio=1610,10pt]{beamer}

\begin{document}

\begin{frame}
    \includegraphics{eta_phi.pdf}
\end{frame}

\end{document}

To ensure the file is found prepend the plots directory to the input path for example this commandline works:

 TEXINPUTS=./plots//: lualatex mainfile

as (assuming bash shell) this locally sets TEXINPUTS to search all directories under plots


If you really have painted yourself into a corner with duplicated filenames in "interesting" directory paths then this does work in lualatex

\documentclass[aspectratio=1610,10pt]{beamer}

\makeatletter
\def\Gread@@pdftex#1{%
  \edef\Gin@attr@hash{%
    \ifx\Gin@pagebox\@empty
    \else
      :\Gin@pagebox
    \fi
    \ifx\Gin@page\@empty
    \else
      :P\Gin@page
    \fi
    \ifx\Gin@decode\@empty\else
      :D[\Gin@decode]%
    \fi
    \ifGin@interpolate
      :I%
    \fi
  }%
  \@ifundefined{#1 image\Gin@attr@hash}%
    {%
      \saveimageresource
        \ifnum0%
          \ifx\Gin@decode\@empty\else 1\fi
          \ifGin@interpolate 1\fi
          >0 %
          attr{%
            \ifx\Gin@decode\@empty\else/Decode[\Gin@decode]\fi
            \ifGin@interpolate/Interpolate true\fi
          }%
        \fi
        \ifx\Gin@page\@empty\else page \Gin@page\fi
        \Gin@pagebox
        {\expandafter\zz#1}%
      \setbox\@tempboxa=\hbox{\useimageresource\lastsavedimageresourceindex}%
      \def\Gin@llx{0}\let\Gin@lly\Gin@llx
      \Gin@defaultbp\Gin@urx{\wd\@tempboxa}%
      \Gin@defaultbp\Gin@ury{\ht\@tempboxa}%
      \expandafter\xdef\csname #1 image\Gin@attr@hash\endcsname
        {\useimageresource\the\lastsavedimageresourceindex}%
      \expandafter\xdef\csname #1 height\Gin@attr@hash\endcsname
        {\the\ht\@tempboxa}%
      \expandafter\xdef\csname #1 width\Gin@attr@hash\endcsname
        {\the\wd\@tempboxa}%
      \Gin@log{%
        <#1, %
        id=\the\lastsavedimageresourceindex, %
        \ifx\Gin@page\@empty\else page=\Gin@page , \fi
        \ifx\Gin@pagebox\@empty\else\ifx\Gin@pagebox\GPT@cropbox\else
          pagebox=\Gin@pagebox , \fi\fi
        \ifx\Gin@decode\@empty\else decode=[\Gin@decode], \fi
        \ifGin@interpolate interpolate=true, \fi
        \the\wd\@tempboxa\GPT@space x \the\ht\@tempboxa
        >%
      }%
    }{%
      \def\Gin@llx{0}\let\Gin@lly\Gin@llx
      \Gin@defaultbp\Gin@urx{\csname #1 width\Gin@attr@hash\endcsname}%
      \Gin@defaultbp\Gin@ury{\csname #1 height\Gin@attr@hash\endcsname}%
    }%
}

\def\zz"#1"{#1}
\begin{document}

\begin{frame}

    \includegraphics{"plots/task-1/ABC123_/qa_pt:0.20-2.00/VtxZ (-10.00, 10.00)/charged; eta (-0.80, 0.80)/FB: 16; eta (-0.80, 0.80)/eff/eta_phi".pdf}
%    \includegraphics{eta_phi.pdf}
\end{frame}

\end{document}
Related Question