3

Having difficulties understanding basic latex operations I want to use: if not (number between x and y): next iteration in loop Can anyone help me with a simple if statement for this?

\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
\newcounter{figurecounter}\setcounter{figurecounter}{1}
\loop
    \read\reader to \filename
    \unless\ifeof\reader
    \filename@parse{\filename}
    %\section*{\filename}
    \
    \vfill
    \begin{figure}[!htb]
    \begin{center}
            \includegraphics[scale=0.5]{"#1\filename@base"}
    \caption{{\bf text}
    \label{fig:item\thefigurecounter}
    \end{center}
    \end{figure}
    \vfill
    \
    \stepcounter{figurecounter}
    \endgraf
    \advance\TotalFiles1\relax
    \clearpage
\repeat
\closein\reader
}
BigChief
  • 197

1 Answers1

5

Thanks to the pointer given in the comment by Please don't touch, at Iterate through folder foreach picture, possible?, the following modifications to that MWE allow the picture range to be specified as arguments 3 and 4 to \IterateImages. The logic that allows the ranging of the inserts is given by the code:

\ifnum\TotalFiles<#3\relax\else\ifnum\TotalFiles>#4\relax\else
  [Do something if current index is in the range #3, #4, inclusive]
\fi\fi

Note that the file batch.bat is provided at the referenced answer.

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

\newread\reader
\newcount\TotalFiles    
\newcount\UsedFiles

\makeatletter
\newcommand\IterateImages[4]{%
% #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}
    \advance\TotalFiles1\relax
    \ifnum\TotalFiles<#3\relax\else\ifnum\TotalFiles>#4\relax\else
      \advance\UsedFiles1\relax
      \section*{\filename}
      \begin{center}
            \includegraphics[scale=0.1]{"#1\filename@base"}
      \end{center}
      \endgraf
    \fi\fi
    \repeat
\closein\reader
}
\makeatother



\begin{document}
% ./ also works
\IterateImages{./}{png}{2}{6}

\section*{Summary}
There is(are) \the\TotalFiles\ file(s) in total.\\
Of them, \the\UsedFiles\ were employed.
\end{document}

enter image description here

  • 3
    His question is based on my answer here (click) – kiss my armpit Aug 05 '14 at 18:24
  • Sorry for not providing credits @ Please don't touch. Still not completely sure how if statements work in latex but I got it working anyway. – BigChief Aug 05 '14 at 19:16
  • 1
    @BigChief If you have access to Knuth's "The TeXbook," p. 209-210 gives a good description of all the different "if" contexts and how they are used: \ifnum \ifdim \ifodd \ifvmode \ifhmode \ifmmode \ifinner \if \ifcat \ifx \ifvoid \ifeof \iftrue \iffalse \ifcase. – Steven B. Segletes Aug 05 '14 at 19:35
  • hey thanks i was looking for something like that, syntax looks not that intuitive, maybe because original tex is quite old? – BigChief Aug 05 '14 at 19:49