# How do you print only the pages with figures on them?

A friend of mine recently completed his doctoral thesis. In order to save on toner, he printed the pages with figures on a color printer, but printed everything else on a black and white printer. In order to do this, he went through the document and printed out each page with a figure, by hand.

While he was careful, it seems extremely error prone, and I'd like to know if there is a way to generate a document containing only the pages with figures on them while retaining the formatting and pagination of the full document?

-
–  Martin Scharrer Jul 11 '11 at 20:49
@Martin, I'll have to look at it a little closer, but it seems like the solution removes either the text or the graphics from a page. What I'm looking for keeps the page intact, in its entirety, but only the pages with the figures on them without losing any of their counters. –  rcollyer Jul 11 '11 at 21:03

It might be easiest to do this sort of thing with the pdfpages package. It's not automatic, but you wouldn't have to print each page individually, and you could generate the list of pages from the list of figures.

Here's an example:

\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages={3-6, 17, 28, 29-31}]{yourdocument}
\end{document}


One thing to bear in mind when using this method is that the page numbers correspond to the number of the physical pages in yourdocument.pdf, not the page numbers in your document. This means that if you have n \frontmatter pages (which in a thesis, you would) then all of the relevant page numbers will be off by n. The easiest way around this is to simply delete all the frontmatter pages from the pdf document in order to generate the extracted page document, so that the first physical page corresponds to page 1 of the thesis.

Automating

Theoretically, it would be possible to automate the process slightly, although for something that is being done once or twice it is probably not worth it. For those who like the challenge, here are some ideas (based on the discussion in the comments).

1. Modify the \addtocontents code for figures to also write out a list of page numbers for subsequent use in the pdfpages document. This seems the most work for little gain in actual automation.
2. Use your favourite scripting language to to extract the lof entries from the .aux file, and parse out the page numbers. The script could then generate the entire pdfpages document. This would probably be the best route if you really needed to automate the process.
3. You're only doing this once or twice; do 2 above by hand and spend the rest of your time enjoying a beer.
-
I hadn't considered pdfpages, but it does make it relatively simple to do, especially if I work from the table of figures. –  rcollyer Apr 18 '11 at 19:14
@Alan Munn Could one modify \contentsline and pick up the page number from the .lof then pass it on onto a list etc. to automate it? –  Yiannis Lazarides Apr 18 '11 at 19:28
@rcollyer I'll be to see anyone comes up with a more automatic solution, but since this is something you only do once in a while, doing it automatically may not be worth the effort. –  Alan Munn Apr 18 '11 at 19:29
@Yiannis, I was thinking along those lines myself. –  rcollyer Apr 18 '11 at 19:33
@Alan, I've been told the same thing, but I'm lazy, and if an automated solution existed ... –  rcollyer Apr 18 '11 at 19:34

I gave a shot at this and came up with a solution. Probably not the best that can be made, but it works. The solution is based on @Alan Munn's answer.

It's a shell script that should work on all Unix systems with pdfLaTeX and pdfpages installed. You use it by giving it the file name of the TeX file (no .tex ending!) and the offset in pages (caused by front matter) as the second argument:

./only-figures.sh document 1


First we load the .aux and prepare to store it in a variable.

STR=$(cat$1.aux |\


We only care about the lines with figures.

 grep figure |\


And of these lines we only care about the number referring to unique pages.

 sed 's/.*}}{$$[1-9]*$$}}/\1/' | uniq |\


We then add the offset to the page numbers.

 awk '{print $0+'$2'}' |\


And finally pack them all up in a comma-separated list.

 sed ':a;N;$!ba;s/\n/,/g')  A template file is used for the new temporary document. cat template.tex | sed s/@@@/$STR/ > only-figures-tmp.tex


The template looks as following:

\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages={@@@}]{document}
\end{document}


We call pdfLaTeX on the temporary file, move the new PDF and cleans up.

pdflatex only-figures-tmp
mv -f only-figures-tmp.pdf \$1-only-figures.pdf
rm only-figures-tmp.*


The whole thing can also be found in this Gist

-