# How to automatically arrange an unknown number of images?

I have several compilable LaTeX input files and their corresponding PDF files. Each PDF file contains any number of pages. I want to import each LaTeX input file with \lstinputlisting followed by its PDF pages as images. I have to consider

• the number of pages for each PDF file on the fly,
• the available spaces for the images

to avoid getting trimmed/cropped output.

The following is my MWE. Determining the number of PDF pages per file on the fly has been solved. The remaining task is to automatically arrange the imported PDF pages.

\documentclass[12pt]{book}
\usepackage[a4paper,margin=25mm,showframe=false]{geometry}
\usepackage{graphicx}
\usepackage{multido}
\usepackage{etoolbox,ifpdf,ifxetex}

\ifxetex
\newcount\pdflastximagepages
\def\pdfximage#1{\pdflastximagepages=\XeTeXpdfpagecount"#1"\relax}
\fi

\newcommand\Directory{}

\newcommand{\InsertOutput}[1]{%
\IfFileExists{\Directory/#1.pdf}{%
\pdfximage{\Directory/#1.pdf}%
\multido{\i=1+1}{\the\pdflastximagepages}
{\includegraphics[page=\i]{\Directory/#1}}
}{The corresponding PDF file does not exist.}%
}

\begin{document}

\renewcommand\Directory{Contents}
\InsertOutput{Article}

\end{document}


Pages in a single PDF file have the same dimension. I might need scaling factor to arrange the images. Each PDF file might need different scaling factor but the same factor will be used for the pages in a single PDF file. I prefer no more than 2 images in one row.

I skipped the usage of \lstinputlisting code for the sake of simplicity.

Using pdfpages package is not my option because I need the images floats and fill the available space.

## The real scenario

I want to write a tutorial on LaTeX showing the code followed its output. As LTXexample from showexpl does not work well for a compilable LaTeX input file, I have to use \lstinputlisting and \includegraphics. The problem is to arrange the images following the imported code.

I want to produce a tutorial like below. FOR EXAMPLE, the page n contains a source code and 2m images, the page n+1 may contain the remaining images and some sentences as the corresponding comments, with m and n are integers.

-
Beauty is in the eye of the beholder. Could you provide a specimen of a beautifully arranged page? Can the included PDF files have different page sizes? One of the most important factors for arrangement will be the relation between the size of the included PDF and the page they are to be arranged on. –  Stephan Lehmke Jun 20 '12 at 3:36
I think Yiannis Lazarides is the person you are looking for :) –  Ryan Reich Jun 20 '12 at 8:16
Look at pdfpages package and its layout options. –  Paul Gaborit Jun 22 '12 at 23:23
@Forgiver Sorry, but I don't understand your comment: what thing do you want to float? –  Paul Gaborit Jun 23 '12 at 9:38
The code in your question solves the problem, doesn't it? You only have to add [width=0.5\linewidth] or a similar option to each \includegraphics, so that you can have several pages per row. Or perhaps you are asking about how to find the appropiate value instead of 0.5? –  JLDiaz Jul 4 '12 at 17:15

The idea is to make a loop which uses \includegraphics to output each page of the pdf, at 45% of the line width (assuming that you want two pages per row, which seems sensible). The tricky part is to consider the case of an odd number of pages. In this case, the last row contains a single page, so it would be nicer if that page is centered.

The following code does that.

\documentclass[12pt]{book}
\usepackage[a4paper,margin=25mm,showframe=false]{geometry}
\usepackage{graphicx}
\usepackage{listings}
\input{repeat}

\lstset{ %
frame=single,
language=tex,                    % the language of the code
numbers=left,                    % where to put the line-numbers
numberstyle=\scriptsize,         % the size of the fonts that are used for the line-numbers
basicstyle=\ttfamily\scriptsize, % font and size of the code
showspaces=false,                % show spaces adding particular underscores
showstringspaces=false,          % underline spaces within strings
showtabs=false,                  % show tabs within strings adding particular underscores
tabsize=4,                       % sets default tabsize to 4 spaces
breaklines=false,                % sets automatic line breaking
breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
}

\def\IncludeOutput#1{
\parskip=2mm
\def\examplename{#1}
\pdfximage{\Directory/\examplename.pdf}%
\edef\lastpdfpage{\the\pdflastximagepages}
\repeat\for{pag}\by{0} % We advance manually the counter in the loop body
\until{\ifnum\pag>\lastpdfpage}
\do{
\noindent\hfill\fbox{%
\includegraphics[width=0.45\textwidth,page=\number\pag]{\Directory/\examplename.pdf}}%
\hfill%
\ifnum\pag>\lastpdfpage\hfill\par% If odd number of pages
\else% If even number of pages, output the last one
\fbox{%
\includegraphics[width=.45\textwidth,page=\number\pag]{\Directory/\examplename.pdf}}%
\hfill\hbox{}\par % Next pair of images
\fi%
}
}

\begin{document}
\def\Directory{Examples}
\lstinputlisting{\Directory/Lipsum.tex}
\IncludeOutput{Lipsum}
And more text follows\dots
\end{document}


An this is the result:

In case the text is unreadable, the content of Lipsum.tex is:

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\title{Lorem Ipsum}
\maketitle
% This inserts paragraphs 1 to 35 of Lorem Ipsum'' which produces 7 pages
\lipsum[1-35]
\end{document}


The idea to have the two images "evenly spaced between the margins" is to have in each line \hfill[PAGE1]\hfill[PAGE2]\hfill. Those hfills expand to the same amount and then the pages are centered and evenly spaced. However, this require an empty box at the end of the third \hfill. Without it, \par would discard that space and the two pages will be "pushed" to the right margin. Try it :-) –  JLDiaz Jul 6 '12 at 16:02