Как передать макросу один аргумент пути к файлу?

Как передать макросу один аргумент пути к файлу?

Я хочу перебрать все изображения в определенном каталоге и импортировать каждое изображение из \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

Используйте xstringпакет для замены /s на \s (или наоборот):

\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. Если есть одинаковые имена файлов с разными расширениями, то будет включен только один из них. Для более подробного решения см.Как обрезать символ конца строки для каждой строки, считанной из внешнего файла?.

Связанный контент