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.




            }{The corresponding PDF file does not exist.}%




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.

enter image description here

  • 7
    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. Jun 20, 2012 at 3:36
  • 7
    I think Yiannis Lazarides is the person you are looking for :)
    – Ryan Reich
    Jun 20, 2012 at 8:16
  • Look at pdfpages package and its layout options. Jun 22, 2012 at 23:23
  • @Forgiver Sorry, but I don't understand your comment: what thing do you want to float? Jun 23, 2012 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, 2012 at 17:15

2 Answers 2


Ok, there is my answer.

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.


\lstset{ %
    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

  \repeat\for{pag}\by{0} % We advance manually the counter in the loop body
    \advance\pag by 1
    \ifnum\pag>\lastpdfpage\hfill\par% If odd number of pages
    \else% If even number of pages, output the last one
       \hfill\hbox{}\par % Next pair of images
    \advance\pag by 1

    And more text follows\dots

An this is the result: enter image description here

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

\title{Lorem Ipsum}
% This inserts paragraphs 1 to 35 of ``Lorem Ipsum'' which produces 7 pages

And you can download the resulting pdf here

  • 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, 2012 at 16:02
  • 1
    Yes, plain tex and latex define \null as \hbox{}
    – JLDiaz
    Jul 6, 2012 at 16:19

tcolorbox provides command \tcbinputlisting which does all the work for you.

Let's supose that you have the file Lipsum.tex (taken from JLDiaz answer) already compiled. If you want to include into another document the code and the result, use \tcbinputlisting with options listing file=Lipsum and listing and comment. listing includes tex source and comment the pdf result.

In case you want to get similar results but with LaTeX code into the main document it's also possible to do it. Some examples are explained in section "Option Keys for Processing and Full Document Examples" from tcolorbox documentation.

Next code shows a solution with external files.




enhanced jigsaw, breakable, pad at break*=2mm, height fixed for=first and middle,
lower separated=false,
leftlower=0pt, rightlower=0pt, middle=0pt,
colframe=red!50!black, colback=yellow!10!white,
listing and comment,
listing file={Lipsum},
listing options={style=tcblatex,texcsstyle=*\color{red!70!black}},
pdf comment,
comment style={raster columns=3, colframe=blue, drop fuzzy shadow},



The result looks like

enter image description here


You must log in to answer this question.

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