
Quiero iterar todas las imágenes en un directorio determinado e importar cada imagen \includegraphics
desde dentro del archivo MainInput.tex
. El archivo por lotes IterateFiles.bat
nos ayuda a crear una lista de nombres de imágenes en el directorio especificado.
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
La implementación actual de \IterateFiles
macro requiere 3 argumentos. Los primeros 2 argumentos son la ruta (relativa a MainInput.tex
) al directorio en el que se guardan las imágenes. Lo malo aquí es que necesitamos especificar la misma ruta en 2 formas: ..\dirsep Images\dirsep
y ../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}
Mi pregunta: ¿Cómo pasar un único argumento de ruta de archivo ../Images/
(en lugar de dos argumentos) a la macro anterior?
Respuesta1
Utilice el xstring
paquete para reemplazar /
s con \
s (o viceversa):
\StrSubstitute{../Images}{/}{\dirsep}}
Respuesta2
Oh, fantasma, aparentemente el shell de comandos de Windows también puede analizar archivos ../Images/
. No sabía esto.
% 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}
Advertencia: hay un error oculto aquí cuando se usa \filenamb@base
en lugar de \mydata
. Si hay los mismos nombres de archivo con diferentes extensiones, solo se incluirá uno de ellos. Para una solución más detallada, consulte¿Cómo recortar el carácter de final de línea para cada línea leída de un archivo externo?.