# Compiling a document to input more than 100 image files

What I want to achieve is to compile a document of pictures in .jpg format into a pdf file. The problem is that I would not want to input all the files one by one. The files follow a pattern in there name:

MSD 2013-10-15 00-20-27-72
MSD 2013-10-15 00-20-32-86

How can this be achieved in an algorithmic manner? The files were created as snapshots of a few powerpoint presentations using PrtSc then converted to .jpg format. If you can notice, the only difference between the files would be the creation time.

Note upon creation, the image files should cover all the page width and page height.

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{MSD 2013-10-15 00-20-27-72} % should cover entire page 1
\includegraphics{MSD 2013-10-15 00-20-32-86} % should cover entire page 2
.
.
.
\end{document}

Using the answer below, pdflatex shouts out:

! Dimension too large.
\pr@ship@end ...fi \endgroup {\dimen@ \ht \pr@box
\ifdim \dimen@ <\z@ \dimen...
l.17 \end{document}

I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

! Dimension too large.
\pr@ship@end ...men@ \pr@bb@iv \dimen@ii =\dimen@
\global \pdfvorigin \dimen...
l.17 \end{document}

I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

! Dimension too large.

l.17 \end{document}

I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

! Dimension too large.

l.17 \end{document}

I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

! Dimension too large.
\pr@endbox ... \@undefined \voffset =-\ht \pr@box
\hoffset =\z@ \fi \c@page ...
l.17 \end{document}

I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

[1
! Huge page cannot be shipped out.
\pr@endbox ...y \pr@markerbox }\else \box \pr@box
l.17 \end{document}

The page just created is more than 18 feet tall or
more than 18 feet wide, so I suspect something went wrong.

The following box has been deleted: []

Note each image file must be read from a folder.

• Is there any pattern (like constant time gap) between two successive pictures taken so as to generate them or they are randomly taken ones (in this case one needs to read all *.jpg file names and store to reuse them i suppose)? – texenthusiast Oct 16 '13 at 19:38
• @texenthusiast The only pattern that I see would be the second each snapshot was taken. That is, the second snapshot was taken a few seconds after the first. So the reading would be made on the basis of incremented time. – azetina Oct 16 '13 at 19:49
• If the timestamps separated at random, the simplest strategy is to create a list and then adding \printimage{ and } around the items; give a suitable definition of \printimage. Nesting three cycles that test, for every timestamp, the existence of the file with that name seems really a sledgehammer. – egreg Oct 16 '13 at 19:57
• @egreg Would it take to much time to compile using the three cycle test? Would it be possible that the first run save to a .aux file then the second run compile the document. – azetina Oct 16 '13 at 20:02
• The "Dimension too large" error might be due to missing information in the JPG files about resolution and dimensions. Sometimes it happens with JPG's. – egreg Oct 16 '13 at 21:02

Adapt the following. It works only on Windows. Compile it with pdflatex -shell-escape filename.tex. The same answer has been posted here.

## Code

% filename.tex
\documentclass{article}
\usepackage[a6paper,margin=15mm]{geometry}
\usepackage{graphicx}
\edef\subdir{"Sub Dir/"}

\graphicspath{{\subdir}}
\immediate\write18{cmd /c dir /b \subdir\space *.png > imagelist.txt}

\begin{document}
\makeatletter
\begingroup
\endlinechar=-1\relax
\loop
\filename@parse{\data}\noindent
\includegraphics[scale=.5,ext=.\filename@ext,width=\textwidth,height=\textheight,keepaspectratio]{"\filename@base"}\newpage
\repeat
\endgroup
\makeatother
\end{document}

## Output

To prove that I am NOT lying...

• pdflatex complains by saying ! Dimension too large. – azetina Oct 16 '13 at 20:28
• The file simply does not compile. How is the data read? I do not see no ^^M. Where is this located? – azetina Oct 16 '13 at 20:37
• I am running your code as is. It creates the file imagelist.txt but complains afterwards. – azetina Oct 16 '13 at 20:40
• Why does it print the filename beside each image? – azetina Oct 18 '13 at 0:10
• You don't have to do it. I suppose that with your code its a good start for me. Here it goes then :-) – azetina Nov 27 '13 at 1:33

I've often done something like the following. Create (and save) a file, say myphotos.tex, with the required preamble:

\documentclass{article}
\usepackage{graphicx}
\begin{document}

At the command line (under windows) issue the following commands

for %x in (*.jpg) do echo \includegraphics{%x}\clearpage >> myphotos.tex
echo \end{document} >> myphotos.tex

Other operating systems can do things like this, although the syntax will differ. To get the photos expanded to fill the entire page, there are options to the \includegraphics command that will scale the pictures. For example, use

\noindent\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]

instead of \includegraphics in the above.

This does the three loops; the arguments to \imageloop are the “common part”, then the starting minute and the ending minute for the search. If also the hour changes one needs another loop.

I commented the “real” printing function, replacing it with a test. The loops are quite fast on my machine, but, with \IfFileExists and the printing of the images, processing will be rather slow.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
% #1 = fixed part
% #2 = starting minute
% #3 = ending minute
\NewDocumentCommand{\imageloop}{mmm}
{
\azet_imageloop:nnn { #1 } { #2 } { #3 }
}
\cs_new_protected:Npn \azet_imageloop:nnn #1 #2 #3
{
\int_step_inline:nnnn { #2 } { 1 } { #3 }
{
\azet_imageloop_seconds:nn { #1 } { ##1 }
}
}
\cs_new_protected:Npn \azet_imageloop_seconds:nn #1 #2
{
\int_step_inline:nnnn { 0 } { 1 } { 59 }
{
\azet_imageloop_hundredths:nnn { #1 } { #2 } { ##1 }
}
}
\cs_new_protected:Npn \azet_imageloop_hundredths:nnn #1 #2 #3
{
\int_step_inline:nnnn { 0 } { 1 } { 99 }
{
\azet_imageloop_print:nnnn { #1 } { #2 } { #3 } { ##1 }
}
}

\cs_new:Npn \azet_imageloop_two:n #1
{
\int_compare:nT { #1 < 10 } { 0 } #1
}

%\cs_new_protected:Npn \azet_imageloop_print:nnnn #1 #2 #3 #4
% {
%  \IfFileExists
%   {
%    #1-\azet_imageloop_two:n{#2}-\azet_imageloop_two:n{#3}-\azet_imageloop_two:n{#4}.jpg
%   }
%   {
%    \clearpage
%    \vspace*{\fill}
%    \includegraphics
%     {
%      #1-\azet_imageloop_two:n{#2}-\azet_imageloop_two:n{#3}-\azet_imageloop_two:n{#4}.jpg
%     }
%    \vfill
%   }
%   {} % nothing if the file doesn't exist
% }

%%% just for testing
\cs_new_protected:Npn \azet_imageloop_print:nnnn #1 #2 #3 #4
{
\par
#1-\azet_imageloop_two:n{#2}-\azet_imageloop_two:n{#3}-\azet_imageloop_two:n{#4}.jpg
}

\ExplSyntaxOff
\begin{document}
\imageloop{MSD 2013-10-15 00}{20}{20}
\end{document}

Here are the first few lines:

• This created a document with 131 pages and the three counters just seem to be increasing. How is this reading all the jpg files in the folder and then inputing them for compilation? Thanks for the input. – azetina Oct 16 '13 at 20:57
• @azetina You can see a commented definition for \azet_imageloop_print:nnnn which is what you'll really be using. The definition used for the sample is just for testing correctedness of the tentative file names to be passed to IfFileExists. (I fixed an error in the commented code.) – egreg Oct 16 '13 at 20:59
• It now produces 44 pages with no image output in the pdf file. Weird. – azetina Oct 16 '13 at 21:13
• @azetina Of course, spaces in file names must be removed. – egreg Oct 16 '13 at 21:22
• @azetina Yes, spaces in file names and TeX don't go along well. – egreg Oct 16 '13 at 21:25

I would use a script to generate a .tex file with just

\doit{MSD 2013-10-15 00-20-27-72}
\doit{MSD 2013-10-15 00-20-32-86}

(I know how to do it in *nix, not in Windows). Than use a file with in the preamble:

\newcommand\doit[1]{\includegraphics[width=0.99\linewidth]{#1}\vfill\eject}

or something similar.

The error you see seems more a problem of a missing width (maybe the jpgs have a very big pixel count?)

\def \SD #1-#2-#3-#4-#5-#6#7{\includegraphics[width=0.99\linewidth]
{MSD #1-#2-#3-#4-#5-#6#7.jpg}\vfill\newpage}
% Each "-" will match a hyphen "-" in the filename
\catcodeM=0 % M is now conveninetly synonym to \
\input{imagelist.tex} % MSD is like \SD so each line of this file now calls \SD.
\catcodeM=11 % clean up of M.

If each line of the file imagelist.txt contains also the suffix .jpg, then you should replace "SD #1-#2-#3-#4-#5-#6#7" by "SD#1.jpg".

I know, this is not a TeX-Solution, but I would use a small Python-Script. The advantage would be, that it would be easily adjustable, if you need to browse through different directories or want to include further features (i.e. read some description for each picture from a textfile and add it as a caption).

Put the following in a file picmaker.py (and make it executable, if you are not using Windows).

#!/usr/bin/python
# -*- coding: utf-8 -*-

import glob
import subprocess

texfile = ['''\\documentclass{article}
\\usepackage{graphicx}
\\begin{document}''']

for pic in glob.glob('*.jpg'):
texfile.append('\\includegraphics{{{0}}}'.format(pic[:-4]))

texfile.append('\\end{document}')

finalfile = "\n".join(texfile)
with open('tmp.tex', 'w') as f:
f.write(finalfile)

subprocess.call(['pdflatex', 'tmp.tex'])

Execute it via python picmaker.py.

If you need further adjustments, such as textwidth, you can easily add them.

Note that \ has to be replaced by \\, { by {{ and } by }}.

Note also, that '*.jpg' is case-sensitive.