将字符串传递给宏而不进行解释

将字符串传递给宏而不进行解释

我正在用 Beamer 制作图片幻灯片。为了包含每张图片,我编写了一个宏,它接受文件名。

\documentclass{beamer}
\usepackage{graphicx}
\beamertemplatenavigationsymbolsempty

\newcommand\pictureslide[1]{%
  \begin{frame}
    \transduration{5}
    \transfade[duration=1]
    \includegraphics[width = \linewidth]{#1}
  \end{frame}
}

\begin{document}

%\input{pictureframes.tex}

\pictureslide{/media/ben/External\ Disk/1\ Pictures/_MG_8304.JPG}

\end{document}

我编写了一些其他代码,可以自动写入包含给定文件夹的所有文件的文件。该文件如下所示,并包含在主 .tex 文件中。

 \pictureslide{PATH/_MG_1234.JPG}
 \pictureslide{PATH/_MG_1235.JPG}
 ...

我得到的错误是:

    ERROR: Missing $ inserted.

    --- TeX said ---
    <inserted text> 
                $
l.25 ...1\ Pictures/_MG_8304.JPG}

我怎样才能传递文件名而不在数学模式下对其进行解释?(文件名还包含一些空格,我使用“\ ”对其进行了保护。)我想答案很简单。我尝试在整个文件名周围放置各种花括号 {} 以防止对其进行解释。

答案1

据我所知,问题不在于下划线,而在于空格:用反斜杠转义它们肯定不起作用,因为(反斜杠空格)不会扩展为空格,而是一个 TeX 原语,基本上意味着“在排版输出中放置一个空格”因此它将停止对文件名的任何解释。Missing $ inserted由于对文件名的读取不完整,因此该错误是虚假的。

加载包并在文件名周围grffile使用(将扩展名保留在外面):"

\documentclass{beamer}
\usepackage{graphicx}
\usepackage{grffile}
\beamertemplatenavigationsymbolsempty

\newcommand\pictureslide[1]{%
  \begin{frame}
    \transduration{5}
    \transfade[duration=1]
    \includegraphics[width = \linewidth]{#1}
  \end{frame}
}

\begin{document}

%\input{pictureframes.tex}

\pictureslide{"/media/ben/External Disk/1 Pictures/_MG_8304".JPG}

\end{document}

相关内容