5

How can I use filenames with commas with pdfpages?

The grffile package didn't help, the \detokenize command didn't help either

Also the \string command doesn't work. For ex.

\includepdf{\string"C:/data/tesi/papers_literature_review/stampati/Tyebjee, Bruno - A model of venture capitalist investment activity.pdf"}

Gives me:

Unknown graphics extension: .pdf". ...nture capitalist investment activity.pdf"}

That is to say I have tried any solution of this answer without success

Please don't answer saying that I should not use commas in file names. We are in 2017 and we still have troubles dealing with special characters

Here is a non working example:

\documentclass[a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage[multidot, extendedchars]{grffile} 
\usepackage{pdfpages}
\begin{document}
    \includepdf[keepaspectratio]{\detokenize{C:/data/tesi/papers_literature_review/stampati/Tyebjee, Bruno - A model of venture capitalist investment activity.pdf}}
\end{document}
  • 2
    You should not use commas in file names. And, yes: I read carefully your question. – egreg Jun 1 '17 at 7:14
  • As far as I can see, the problem is with \includepdf, because \includegraphics seems to work. – egreg Jun 1 '17 at 7:18
  • @egreg pdfpages does \AM@setphantomdoc #1,#2\END in its code. So the comma is a stop marker. (As far as I can see is it part of the code to resort page, e.g. for booklets and there are more comma lists involved). – Ulrike Fischer Jun 1 '17 at 7:46
9

Even in 2017, putting spaces and commas in filenames is a bad idea, however

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}

\usepackage{pdfpages}
\begin{document}
\typeout{===}
{\catcode`\,=11    \includepdf{abc,xyz}}
\typeout{===}
\end{document}

works as seen in the log:

===
[1 <./abc,xyz.pdf{/usr/local/texlive/2017/texmf-var/fonts/map/pdftex/updmap/pdf
tex.map}>]
===

Or if you really need a punctuation comma at the same time

{\catcode`\,=11 \gdef\zz{abc,xyz}}   \includepdf[keepaspectratio, pages={-}]{\zz}
  • It does not work if you use the comma to separate options of pdfpages {\catcode`\,=11 \includepdf[keepaspectratio, pages={-}]{C:/data/tesi/papers_literature_review/stampati/Tyebjee, Bruno - A model of venture capitalist investment activity.pdf}} – raffamaiden Jun 1 '17 at 7:27
  • 2
    yep, you can't have the cake and eat it too ;-)... I agree with @egreg here. Why castigate oneself using these filenames? You could use your bibtex keys for the filenames, for example, and forget about that. In a case of an emergency, just create a link... – Rmano Jun 1 '17 at 7:31
  • 2
    @raffamaiden updated for that case (but just renaming the file not to have a comma is easier) – David Carlisle Jun 1 '17 at 7:34
  • 2
    @Rmano self eating cake supplied:-) – David Carlisle Jun 1 '17 at 7:35
3
\ExplSyntaxOn
\RenewDocumentCommand{\includepdf}{O{}m}
  {
   \tl_set:Nx \l_tmpa_tl { \tl_to_str:n { #2 } }
   \tl_replace_all:Nnf \l_tmpa_tl { , } { \char_generate:nn { `, } { 11 } }
   \ORIincludepdf[#1]{\l_tmpa_tl}
  }
\ExplSyntaxOff

The problem is that pdfpages uses the code for splitting the options also for managing the file name and so problems are to be expected when there is a comma in the file name. Indeed, it turns out that the file is loaded correctly, but when it's time to use it, the problem appears.

This can be cured by changing the commas in the file name into characters with different category code.

\documentclass{article}
\usepackage[multidot, extendedchars]{grffile}
\usepackage{pdfpages}
\usepackage{xparse,letltxmacro}

% save the original macro    
\LetLtxMacro\ORIincludepdf\includepdf

\ExplSyntaxOn
\RenewDocumentCommand{\includepdf}{O{}m}
 {
  % store the file name as a string
  \tl_set:Nx \l_tmpa_tl { \tl_to_str:n { #2 } }
  % replace commas (catcode 12) with commas (catcode 11)
  \tl_replace_all:Nnf \l_tmpa_tl { , } { \char_generate:nn { `, } { 11 } }
  \ORIincludepdf[#1]{\l_tmpa_tl}
 }
\cs_generate_variant:Nn \tl_replace_all:Nnn { Nnf }
\ExplSyntaxOff

\begin{document}

\includepdf[keepaspectratio,pages={-}]{file, with space, and comma.pdf}

\end{document}
  • Nice --- although that "actualize the change" seems a bit black magic... ;-) – Rmano Jun 1 '17 at 11:22
  • 1
    @Rmano Replaced one black magic with another. – egreg Jun 1 '17 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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