
\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
の 2 つの形式で指定する必要があることです 。..\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 つのファイル パス引数 (2 つの引数ではなく) を渡すにはどうすればよいでしょうか?
答え1
パッケージを使用して、 s を s にxstring
置き換えます(またはその逆)。/
\
\StrSubstitute{../Images}{/}{\dirsep}}
答え2
ああ、どうやら Windows コマンド シェルでも解析できるようです../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
。拡張子が異なる同じファイル名がある場合、そのうちの1つだけが含まれます。より詳細な解決策については、外部ファイルから読み取った各行の行末文字をトリミングするにはどうすればよいですか?。