7

I have lots of images I need to display in a PDF (no text). I was hoping to make something like a contact sheet used for photos. Is there an easy way of doing this without having to do it all manually, i.e. can I set up, say, a 20 by 10 grid and then just list the filenames? If I have to go through and begin and end environments every few lines it's going to take way too long. Ideally, I'd like it to wrap onto the next page.

  • 1
    As long as the file names are predictive, this is quite easy. There are several packages that provide loops etc. You may want to look at etoolbox, plus you might need to know about \IfFileExists{<file>}{yes}{no}, – daleif Jul 24 '15 at 14:10
  • Welcome to TeX.SX! You should give us more information about the code – user31729 Jul 24 '15 at 14:13
  • @ChristianHupfer I'm not sure what information you want. I am asking for the best way of doing this so clearly I haven't coded this yet. – danm Jul 24 '15 at 14:16
  • @daleif I have a list of the file names. I'm not trying to create code that finds the right files or anything like that. My question is how to display this easily without having to modify my list line-by-line. – danm Jul 24 '15 at 14:21
  • Make the list into a comma separated list, then use etoolbox tools to loop over that list and include each one of them. The file test if useful if you make an error in making the list. Then use that construction as a starting point for how ever you wanted it to look like. – daleif Jul 24 '15 at 14:23
10

Reading filenames from another file can be tricky as soon as there are special characters in the file name, such as _ etc.

This uses \readline, which changes the catcodes accordingly and stores the read file name to a list, here called \mylist.

To display the graphics, it's 'sufficient' to define a list processor, basically doing \includegraphics. ( I added wrapping around after three images and used pretty alignment within table)

\documentclass{article}

\usepackage{etoolbox}
\usepackage{graphicx}

\listxadd{\mylist}{}
\newread\readhandle

\newcommand{\readfromfiletolist}[1]{%
  \IfFileExists{#1}{%
    \begingroup
    \openin\readhandle=#1% open the file
    \endlinechar-1  % Drop the endline characters
    \loop
    \readline\readhandle to \linefromfile %read a complete line
    \listxadd{\mylist}{\linefromfile}% add expanded!
    \unless\ifeof\readhandle
    \repeat
    \endgroup
    \closein\readhandle% Close the file
  }{}%
}


\newcounter{piccounter}

\newcommand{\displayimage}[1]{%
  \stepcounter{piccounter}%
  \begin{tabular}[t]{l}
  \includegraphics[width=3cm]{"#1"} \tabularnewline
  #1  
  \end{tabular}
  \ifnum\value{piccounter} = 3  % Wrap after 3 pictures
  \par
  \setcounter{piccounter}{0}%
  \fi%
}


\begin{document}
\readfromfiletolist{filelist.txt}
\begin{center}
\huge \textbf{Spot the wrong image!}
\end{center}

\forlistloop{\displayimage}{\mylist}
\end{document}

enter image description here

Here's the content of filelist.text

a_1.jpg
a_2.jpg
a_3.jpg
a_4.jpg
a_5.jpg
a_6.jpg
brontosaurs.png
a_7.jpg

The brontosaurs.png file is taken from OpenClipart, while the nice beelike duke is taken from our ever friendly user Paulo Cereda!

Please note that _ is displayed as a superscript dot, due to the catcode change. As long as the file name should not displayed itself, this is no issue, in my point of view.

  • Would it be more or less the same as locally redefine verbatiminput? That already have a lot of facilities in play and it should the be a matter of redoing its write command (not sure if it will work) – daleif Jul 25 '15 at 5:40
  • @daleif: Yes, I think you're right, but sometimes I am doing the hard way, just to learn how things work. – user31729 Jul 25 '15 at 5:42
  • 1
    \includegraphics[width=3cm]{"#1"} just in case of blank space in file name – touhami Jul 25 '15 at 5:49
  • @touhami: Never used spaces in file names, I don't like them, but I will change my code, thanks for the hint – user31729 Jul 25 '15 at 6:00
  • 1
    \begin{tabular}[t]{c} instead of \begin{tabular}[t]{l} then ;-) l stands for left justified, c for centered, I leave it to you to figure out the correct letter for right justifying;-) – user31729 Jul 27 '15 at 9:34
3

Something like this may work (untested)

\usepackage{etoolbox,graphicx}
\newcommand\HandlePic[1]{%
   \includegraphics[width=3cm]{#1}
   \par
 }

 \forcsvlist\HandlePic{A,B,C}

Each item in the list is given one by one to the handler.


Just an addition to Christians solution. Here is a POC using the verbatim package to misuse \verbatiminput

\documentclass[a4paper]{article}
\usepackage{verbatim}
\usepackage{graphicx}
\makeatletter 
\newcommand\test[1]{
  \begingroup
  \def\verbatim@processline{%
    \includegraphics{\the\verbatim@line}%
    \par
  }
  \def\verbatim@input##1##2{%
   \IfFileExists {##2}{%
    \verbatim@readfile{\@filef@und}
    \endgroup\@doendpe}%
   {\typeout {No file ##2.}\endgroup}}
  \verbatiminput{#1}
  \endgroup
}
\makeatother

\begin{document}

\test{t.txt}

\end{document}
  • Is there a way to have the list in a file and read it in? There are over a hundred file names! – danm Jul 24 '15 at 14:41
  • How are the images named? If it is just a common name and a number we don't even need the list. Plus how is the list of filenames formatted? – daleif Jul 24 '15 at 14:42
  • Also, I really need to format these images as I think this will take up more than a page and tell me that the dimensions were exceeded. – danm Jul 24 '15 at 14:42
  • @danm: Reading it to a list is possible,with some work to do, assuming no spaces are in the name – user31729 Jul 24 '15 at 14:43
  • Why, here the image is scaled to 3cms and is placed on a line of its own, so it changes page without problems – daleif Jul 24 '15 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.