2

I am developing a complex system that does a bunch of things with included PDFs. So complicated that I want to define a macro. Unfortunately, many of my users have filenames that have spaces and underbars in them. I haven't been able to figure out how to create a macro that runs \includepdf with the filename as a macro that allows files that include spaces.

That is, I want to get this program to work:

\documentclass[10pt]{book}
\usepackage{pdfpages}
\newcommand{\mycommand}[1]{
  Including file: #1 (on next page).
  \clearpage
  \includepdf{#1}
}

\begin{document}
\mycommand{demo_include1.pdf}
\end{document}

But the errors I get are:

! Missing $ inserted.
<inserted text> 
                $
l.11 \mycommand{demo_include1.pdf}

? ^D
! Emergency stop.
<inserted text> 
                $
l.11 \mycommand{demo_include1.pdf}

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on d.log.

So how do I fix my LaTeX file?

Fran
  • 80,769
vy32
  • 4,780
  • Underscores always worked fine for me. You don't need the \clearpage. Maybe it only works for images, though. – cfr Feb 18 '17 at 03:53
  • Then again, I always load fontenc. – cfr Feb 18 '17 at 03:56
  • @cfr -- It's this part of the command Including file: #1 (on next page). when the #1 has an underscore in it. – jon Feb 18 '17 at 03:57

1 Answers1

2

Like this? (Note the use of fontenc.)

\documentclass[10pt]{book}
\usepackage[T1]{fontenc}
\usepackage{pdfpages}

\newcommand{\mycommand}[1]{%
  Including file: \detokenize{#1} (on next page).
  \includepdf{#1}}

\begin{document}
\mycommand{demo_include.pdf}
\end{document}
jon
  • 22,325