# using PDF bookmarks to refer to pages in \includegraphics

If I have a multi-page PDF file with one figure on each page, I can use the `\includegraphics[page=123]{foo}` command to refer to individual figures (see this question).

However, if I have lots of figures, it gets difficult to try to keep track of the page numbers. (For example, I use OmniGraffle to prepare my illustrations and export everything as one multi-page PDF file; if I re-organise the figures in OmniGraffle, I have to manually fix all page numbers in the Latex code.)

Could I somehow use PDF bookmarks instead of page numbers in `\includegraphics`?

-
Unfortunately all PDF anchors and other annotations in included PDFs are removed. I think we have a similar question already. –  Martin Scharrer Sep 24 '11 at 6:22
@MartinScharrer: Removing bookmarks in included PDFs is perfectly fine for me. I would just like to use the bookmarks to select which page to include. That is, to write something like `\includegraphics[bookmark=bar]{foo}` instead of `\includegraphics[page=2]{foo}`. This is certainly doable by invoking an external program from Latex, but I was wondering if there is a more direct approach. –  Jukka Suomela Sep 24 '11 at 7:41
I'm not sure if `pdftex` or `luatex` can access these bookmarks. You need this kind of lower level support, otherwise you won't have a chance on the higher level. –  Martin Scharrer Sep 24 '11 at 7:48
I can confirm that `pdftex` at least does not have access to the bookmarks. I don't know about `luatex`. –  Lev Bishop Sep 25 '11 at 1:30

Here's a command that implements user946850's idea. You need `pdftk` and GNU `sed`:

``````\$ pdftk input_file.pdf dump_data | sed -n '/BookmarkTitle/{y/1234567890/ABCDEFGHIJ/;H};
/BookmarkPageNumber/H;
\${g;s/BookmarkTitle: \([^\n]*\)\nBookmarkPageNumber: \([^\n]*\)/\\newcommand{fig\1}{\2}/g;
s/ //g;p}' > file_bookmarks.tex
``````

This turns a bookmark such as "1 First figure" (being a bookmark on page 18) into a line in the output:

``````\newcommand{\figAFirstFigure}{18}
``````

I prepend `\fig` for uniqueness, and I eliminate spaces, and convert digits into A-J because they are not easily used in TeX commands. Other special characters will be a problem too: better stick to alphabetic characters only for the bookmarks. This could be worked around using `\csname` if necessary.

-

From the LaTeX Wikibook section on including graphics, this does not seem to be possible. Have you considered a solution like the following:

``````\newcommand{\definepage}[1]{%
\stepcounter{myexternalpagenum}\edef#1{\arabic{myexternalpagenum}}}
\newcounter{myexternalpagenum}
\definepage{\macroforpageone}
\definepage{\macroforpagetwo}
%...
\definepage{\macroforlastpage}
%...
\includegraphics[page=\macroforpageonehundredtwentythree]{foo}
``````

This would at least spare you from editing the document in many places, it suffices to update the order of the `\definepage` commands. Perhaps there is a PDF tool that can output a list of bookmarks which you then could convert into a list of `\definepage` calls.

-
I've done almost exactly this, but in my case the graphics pages were all generated via tex (and various scripts and makefiles) so it was easy to autogenerate the list of definitions without needing to parse the bookmarks out of the pdf. –  Lev Bishop Sep 25 '11 at 1:32
What you're linking to is not the "documentation of `\includegraphics`, I'd refer to it as the "LaTeX Wikibook section on including graphics". A documentation of a package (the package most commonly providing the command `\includegraphics` is `graphicx`) can usually be found on CTAN; however, for `graphicx` I don't seem to find one right away. –  doncherry Sep 29 '11 at 16:45
Sure. Edited the answer. -- Have you tried `texdoc graphicx`? –  krlmlr Oct 28 '11 at 13:52