TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to include pages from an external PDF file into my document, using the pdfpages package. The following works for me:



This is my document, and an included file starts on the next page.

\includepdf[pages=-,addtotoc={1,section,1,{A Title},sec.1},3,subsection,2,{A subsection},{s.1.1}]{LF426-2up.pdf}


But if the toc entry list was given passed as a macro instead, I get an error:

\newcommand{\mylist}{1,section,1,{A Title},s.1,3,subsection,2,{A subsection},{s.1.1}}
Runaway argument?
\END \fi \whiledo {\AM@page =\AM@toc@page }{\ifx \AM@toclist \empty \else \ETC.
! Paragraph ended before
\AM@parse@toclisti was complete.
<to be read again>


How (if possible) can I pass \mylist correctly to the addtotoc option of \includepdf?

share|improve this question
up vote 8 down vote accepted
% Put this in the preamble

% this can go everywhere (notice the `\unexpanded` to avoid possible problems)
\newcommand{\mylist}{\unexpanded{1,section,1,{A Title},s.1,3,%
  subsection,2,{A subsection},{s.1.1}}}

% With this you include the PDF file

The reason is that the option addtotoc expects a comma separated list of values and not a macro expanding to that.

A couple of words about \begingroup\edef\x{\endgroup

This has already been explained elsewhere, but for the sake of completeness, I'll repeat here. When LaTeX finds \eincludepdf it looks for a possible [ following and, if it finds one, gathers what's between [ and ] as an argument to replace #1 (if there's no [ then #1 is replaced by nothing). So, with \eincludepdf[pages=-,addtotoc={\mylist}] the replacement is


The fun begins: TeX executes \begingroup, thus entering in a "semisimple group", and proceeds to do the \edef\x. First of all it expands everything it finds in the following pair of braces:

  • \endgroup is not expandable, so it remains untouched;
  • \noexpand is expandable, its expansion is empty and the following token becomes unexpandable;
  • [pages=-,addtotoc={ are all unexpandable;
  • \mylist is expanded to \unexpanded{...}, and also \unexpanded is expanded, which gives ... not subject to further expansion (it might contain text with typesetting directives such as \textbf, so we protect against its expansion);
  • }] are not expandable.

Now the meaning of \x is assigned: \endgroup\includepdf[...] and \x is expanded! This closes the group (so removing the meaning of \x) and TeX is confronted with

\includepdf[pages=-,addtotoc={<contents of \mylist>}]{otherfile.pdf}

Et voilà.

share|improve this answer
Works wonderfully. Thanks! – LianTze Lim Mar 31 '12 at 16:48
I'm sure you've explained expansion and grouping umpteenth times elsewhere, but could you do me a favor and elaborate why a { can come between \begingroup and \endgroup in your definition of \eincludepdf? I'm still puzzled after half a day. – LianTze Lim Apr 1 '12 at 8:52
Thank you so much for the remark about expansions. – LianTze Lim Apr 1 '12 at 11:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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