如何修剪从外部文件读取的每一行的行尾字符\jobname.list
?请注意,实际场景不使用filecontents
包来生成列表。我使用批处理文件来准备列表。以下 MWE 只是为了简单起见。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.list}
a.jpg
b.pdf
c.eps
d.png
\end{filecontents*}
\parindent=0pt
\usepackage{graphicx}
\newread\reader
\begin{document}
\makeatletter
\openin\reader=\jobname.list\relax
\advance\endlinechar \@M
\loop
\read\reader to \x
\unless\ifeof\reader
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{../../Images/\x}%
\repeat
\advance\endlinechar -\@M
\makeatother
\end{document}
上面的代码无法编译,因为\includegraphics
不喜欢在文件路径中添加行尾字符。我浪费了很多时间才找到问题的根源。
编辑:
如果将图像文件放入,a.jpg
则建议的解决方案不再有效。如何解决?b.pdf
../../Images/
我主要用它xelatex
来容纳4种图像格式。
答案1
Herbert 的另一种方法是删除行末的空格,这就是 TeX 将行末转换为的空格(当然假设没有空行)
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.list}
example-image-a.pdf
example-grid-100x100pt.png
\end{filecontents*}
\parindent=0pt
\usepackage{graphicx}
\newread\reader
\begin{document}
\def\stripend#1 \stop{\unexpanded{#1}}
\openin\reader=\jobname.list\relax
\loop
\read\reader to \x
\unless\ifeof\reader
\edef\x{%
\unexpanded{%
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]%
}%
{\expandafter\stripend\x \stop}%
}%
\x
\repeat
\closein\reader
\end{document}
答案2
不分组:
\newread\reader
[...]
\begin{document}
\makeatletter
\advance\endlinechar \@M
\openin\reader=\jobname.list\relax
\loop
\read\reader to \x
\unless\ifeof\reader
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\x}%
\repeat
\closein\reader
\advance\endlinechar -\@M
\makeatother
[...]
如果图像保存在另一个目录中,则使用(例如 /tmp/ 中的图像):
[...]
\edef\filename{/tmp/\x}
\unless\ifeof\reader
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\filename}%
[...]
答案3
当 中的文件名具有某些前缀时,以下方法也有效(问题中提到的问题可能与中的\includegraphics
某些设置有关)\endlinechar
graphics.sty
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.list}
example-image-a.jpg
example-grid-100x100pt.png
\end{filecontents*}
\parindent=0pt
\usepackage{graphicx}
\newread\reader
\begin{document}
\openin\reader=\jobname.list\relax
\begingroup\endlinechar=-1
\loop
\read\reader to \x
\unless\ifeof\reader
\edef\x{../Images/\x}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\x}
\repeat
\endgroup
\closein\reader
\makeatother
\end{document}
这是一个expl3
实现
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.list}
example-image-a.jpg
example-grid-100x100pt.png
\end{filecontents*}
\usepackage{graphicx}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\includefromfile}{O{} m m}
{
\garbcoll_include:nnn { #1 } { #2 } { #3 }
}
\ior_new:N \g__garbcoll_read_ior
\cs_new_protected:Npn \garbcoll_include:nnn #1 #2 #3
{
\ior_open:Nn \g__garbcoll_read_ior { #3 }
\ior_map_inline:Nn \g__garbcoll_read_ior
{
\tl_set:Nx \l_tmpa_tl { #2 \tl_trim_spaces:n { ##1 } }
\includegraphics[ #1 ]{ \l_tmpa_tl } \par
}
}
\ExplSyntaxOff
\parindent=0pt
\begin{document}
\includefromfile
[width=\textwidth,height=\textheight,keepaspectratio]
{../Images/}{\jobname.list}
\end{document}