如何修复导入路径中带有空格的 EPS 图像时出现的错误?

如何修复导入路径中带有空格的 EPS 图像时出现的错误?

我使用的是 Windows。目标是导入子目录中的所有图像。我将其分为 2 个类别,分别为 PNG 和 EPS,如下所示。

对于 PNG

此类别运行正常。因此您可以跳过它。

\documentclass{article}

{
    \catcode`\^0
    \catcode`\\12
    ^gdef^dirsep{\}
}

\usepackage[a6paper,margin=15mm]{geometry}
\usepackage{graphicx}
\edef\subdir{Sub Dir\dirsep}

\graphicspath{{"\subdir"}}

\immediate\write18{cmd /c dir /b "\subdir*.png" > imagelist.txt}


\begin{document}
\makeatletter
\newread\reader
\openin\reader=imagelist.txt\relax
\begingroup
\endlinechar=-1\relax
\loop
    \readline\reader to \data
    \unless\ifeof\reader
            \filename@parse{\data}\noindent  
            \includegraphics[scale=.5,ext=.\filename@ext,width=\textwidth,height=\textheight,keepaspectratio]{"\filename@base"}\newpage
\repeat
\endgroup
\closein\reader
\makeatother
\end{document}

对于 EPS

\documentclass{article}

{
    \catcode`\^0
    \catcode`\\12
    ^gdef^dirsep{\}
}

\usepackage[a6paper,margin=15mm]{geometry}

\usepackage{graphicx}
\edef\subdir{Sub Dir} % must be no double quotes used

\graphicspath{{\subdir/}}

\usepackage[space]{grffile}
\usepackage{etoolbox}
\makeatletter
\patchcmd\Gread@eps{\@inputcheck#1 }{\@inputcheck"#1"\relax}{}{}
\makeatother

\immediate\write18{cmd /c dir /b "\subdir\dirsep*.eps" > imagelist.txt}


\begin{document}
\makeatletter
\newread\reader
\openin\reader=imagelist.txt\relax
\begingroup
\endlinechar=-1\relax
\loop
    \readline\reader to \data
    \unless\ifeof\reader
            \filename@parse{\data}\noindent  
            \includegraphics[scale=.5,ext=.\filename@ext,width=\textwidth,height=\textheight,keepaspectratio]{\filename@base}\newpage% must be no double quotes used.
\repeat
\endgroup
\closein\reader
\makeatother
\end{document}

编译失败,出现以下一些错误。

This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/W32TeX)
 restricted \write18 enabled.
entering extended mode
(./for eps.tex
LaTeX2e <2011/06/27>
Babel <3.9g> and hyphenation patterns for 78 languages loaded.
(c:/texlive/2013/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(c:/texlive/2013/texmf-dist/tex/latex/base/size10.clo))
(c:/texlive/2013/texmf-dist/tex/latex/geometry/geometry.sty
(c:/texlive/2013/texmf-dist/tex/latex/graphics/keyval.sty)
(c:/texlive/2013/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(c:/texlive/2013/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
(c:/texlive/2013/texmf-dist/tex/generic/ifxetex/ifxetex.sty))
(c:/texlive/2013/texmf-dist/tex/latex/graphics/graphicx.sty
(c:/texlive/2013/texmf-dist/tex/latex/graphics/graphics.sty
(c:/texlive/2013/texmf-dist/tex/latex/graphics/trig.sty)
(c:/texlive/2013/texmf-dist/tex/latex/latexconfig/graphics.cfg)
(c:/texlive/2013/texmf-dist/tex/latex/graphics/dvips.def)))
(c:/texlive/2013/texmf-dist/tex/latex/oberdiek/grffile.sty
(c:/texlive/2013/texmf-dist/tex/latex/oberdiek/kvoptions.sty
(c:/texlive/2013/texmf-dist/tex/generic/oberdiek/ltxcmds.sty)
(c:/texlive/2013/texmf-dist/tex/generic/oberdiek/kvsetkeys.sty
(c:/texlive/2013/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
(c:/texlive/2013/texmf-dist/tex/generic/oberdiek/etexcmds.sty
(c:/texlive/2013/texmf-dist/tex/generic/oberdiek/ifluatex.sty))))
(c:/texlive/2013/texmf-dist/tex/generic/oberdiek/pdftexcmds.sty))
(c:/texlive/2013/texmf-dist/tex/latex/etoolbox/etoolbox.sty
(c:/texlive/2013/texmf-dist/tex/latex/etex-pkg/etex.sty))
No file "for eps".aux.
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Bad character code (-1).
\Gread@eps ...catcode `\\
10 \catcode \endlinechar 
                                                  5 \@makeother \:\@makeothe...
l.36 \repeat

? 

可下载完整的 MWE

可以在此处下载包含 PNG 和 EPS 格式虚拟图像的完整代码

答案1

正如错误消息显示该文件正在尝试执行

\catcode \endlinechar 5

在 EPS 文件中查找 BoundIngBox 注释时规范化行尾处理

我没想到你会这么做

 \endlinechar=-1\relax

因此最终会尝试设置字符 -1 的 catcode,从而产生您显示的错误。

我认为你可以做

            {\endlinechar=13
            \includegraphics[scale=.5,ext=.\filename@ext,width=\textwidth,height=\textheight,keepaspectratio]{\filename@base}}

这样在读取EPS文件时endlinechar就恢复正常了。

可以说这是一个 bug^H^H^Hfeature(或者至少是缺乏防御性编码),graphics.sty并且第 260 行graphics.sty应该说

\ifnum\endlinechar>\z@\catcode\endlinechar5 \fi

而不是

\catcode\endlinechar5 %

相关内容