
我想迭代某個目錄中的所有圖像,並\includegraphics
從MainInput.tex
.批次檔IterateFiles.bat
幫助我們在指定目錄中建立映像名稱清單。
rem IterateFiles.bat
echo off
rem %1 represents the path (relative to the main input file) to the files to be iterated
rem %2 represents the output file name
rem the remaining args represent the extensio of file to be iterated
set curdir=%CD%
cd %1
shift
set output=%curdir%\%1.list
copy nul %output%
shift
:loop
if "%1"=="" goto :eof
dir /b *.%1 >> %output%
shift
goto :loop
巨集的目前實作\IterateFiles
需要 3 個參數。前 2 個參數是MainInput.tex
保存影像的目錄的路徑(相對於)。這裡的壞處是我們需要以兩種形式指定相同的路徑: ..\dirsep Images\dirsep
和../Images/
。
% MainInput.tex
\documentclass{article}
\usepackage{graphicx}
{
\catcode`\^0
\catcode`\\12
^gdef^dirsep{\}
}
\newread\myfile
\newcount\TotalFiles
\makeatletter
\newcommand{\IterateFiles}[3]{%
\immediate\write18{IterateFiles #1 \jobname\space #3}
\openin\myfile=\jobname.list\relax
\loop
\read\myfile to \mydata
\unless\ifeof\myfile
\filename@parse{\mydata}
\section*{\mydata}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{#2\filename@base}
\advance\TotalFiles1\relax
\repeat
\closein\myfile
}
\makeatother
\begin{document}
\IterateFiles{..\dirsep Images\dirsep}{../Images/}{jpg png pdf eps}
\section*{Summary}
There are \the\TotalFiles\ files in total.
\end{document}
我的問題:如何將單一檔案路徑參數../Images/
(而不是兩個參數)傳遞給上面的巨集?
答案1
使用xstring
包將 s 替換/
為\
s(反之亦然):
\StrSubstitute{../Images}{/}{\dirsep}}
答案2
天哪,顯然 Windows 命令 shell 也可以解析../Images/
.我不知道這個。
% MainInput.tex
\documentclass{article}
\usepackage{graphicx}
\newread\myfile
\newcount\TotalFiles
\makeatletter
\newcommand{\IterateFiles}[2]{%
\immediate\write18{IterateFiles #1 \jobname\space #2}
\openin\myfile=\jobname.list\relax
\loop
\read\myfile to \mydata
\unless\ifeof\myfile
\filename@parse{\mydata}
\section*{\mydata}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{#1\filename@base}
\advance\TotalFiles1\relax
\repeat
\closein\myfile
}
\makeatother
\begin{document}
\IterateFiles{../Images/}{jpg png pdf eps}
\section*{Summary}
There are \the\TotalFiles\ files in total.
\end{document}
\filenamb@base
警告:當使用代替時,這裡存在一個隱藏的錯誤\mydata
。如果存在具有不同副檔名的相同檔案名,則僅包含其中之一。更詳細的解決方案請參見如何修剪從外部文件讀取的每一行的行尾字元?。