3

Is it possible to iterate through a folder containing pictures while first filtering file extension tikz,png,pdf or eps, and then getting a list of filenames of the directory for use in a for loop, and add them using their filename as image text in my latex code?? I tried googling code examples but no luck yet.

4

2 Answers 2

8

This answer had been prepared long time ago and I am waiting for someone to ask it. Here (click) is the history.

Batch.bat

rem batch.bat
echo off

rem %1 path (relative to the main input file) to the files to be iterated 
rem %2 output file name
rem remaining args represent the extension of file to be iterated

set curdir=%CD%
cd "%~1"
shift

rem output must be enclosed with "" to allow spaces in the path
set output="%curdir%\%~1.list"

if exist %output% del %output%
copy nul %output%
shift

:loop
if "%~1"=="" goto :eof
dir /b *.%~1 >> %output%
shift
goto :loop

filename.tex

% filename.tex must be compiled with 
% pdflatex -shell-escape filename.tex 
\documentclass[preview,border=12pt]{standalone}
\usepackage{graphicx}

\newread\reader
\newcount\TotalFiles    


\makeatletter
\newcommand\IterateImages[2]{%
% #1: directory path with a trailing /
% #2: a list of file extensions: eps pdf jpg png
\immediate\write18{batch "#1" \jobname\space #2}
\openin\reader=\jobname.list\relax
\loop
    \read\reader to \filename
    \unless\ifeof\reader
    \filename@parse{\filename}
    \section*{\filename}
    \begin{center}
        \includegraphics[scale=0.1]{"#1\filename@base"}
    \end{center}
    \endgraf
    \advance\TotalFiles1\relax
\repeat
\closein\reader
}
\makeatother



\begin{document}
% ./ also works
\IterateImages{Sample Pictures/}{jpg png pdf}

\section*{Summary}
There is(are) \the\TotalFiles\ file(s) in total.
\end{document}

Output

To prove that I did not lie.

enter image description here

Notes

For security reason, filecontents does not allow us to create batch.bat on the fly.

4
  • Thanks!! Absolutely outrageous!! To answer this so fast so comprehensive. Cool to think of a batch script to handle this. Last minor request: How to skip extensions in the resulting output image text?? Would it be possible without batch scripts?? I guess not?
    – BigChief
    Aug 2, 2014 at 17:17
  • 1
    @BigChief: You can use \filename@base if you want the filename without extension. Using batch file is the best approach in my opinion. Aug 2, 2014 at 17:20
  • 1
    You can create the bat-file on the fly by first writing a batch.txt and then use write18 to rename it. But I would probably prefer a .lua script instead of a batch script. Aug 2, 2014 at 17:41
  • forget about it got everything up and running
    – BigChief
    Aug 4, 2014 at 15:09
1

Running on OSX I need to add
\endlinechar=-1
to remove the default character added to the end of the input line.

\documentclass[]{article}
\usepackage{graphicx}
\usepackage[colorlinks=true, pdfstartview=FitV, linkcolor=blue, citecolor=blue, urlcolor=blue]{hyperref}
\usepackage[hyphenbreaks]{breakurl}
\usepackage[space]{grffile}% used to allow spaces within the filenames
\newread\reader
\newcommand\IterateLines[2]{
    \immediate\write18{ls -1 "#1" > \jobname.txt}
    \openin\reader=\jobname.txt\relax
    \endlinechar=-1
    \loop
    \read\reader to \line
    \unless\ifeof\reader
    {\line}\par
    \includegraphics[width=2cm]{{#1\line}} %
    \par
    \repeat
    \closein\reader
}
\makeatother
\begin{document}
Images from \url{https://www.pexels.com/public-domain-images/}\par
\IterateLines{testimages/}{png}
\end{document}

Output:
Sample ooutput

1
  • Can you add complete code to show where that command should be placed and post the output demonstrating that it works. Dec 8, 2017 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .