Wow ! Right on the spot ! Thanks very much, cyberSingularity !
- Main error = not all my PDF documents had 2 pages, whereas
sometimes the command
\IncludeMyPDFThatBugs
called for the first,
and sometimes it called for the second page.
- Many missing closing braces:
{\FirstLanguageSecondLanguage{first language}{second language}}
had
some missing closing braces.
{\FirstLanguageSecondLanguage{\textbf{first}\textbf{language}}{\textbf{second}\textbf{language}}}
had some missing closing braces.
- Elsewhere, in my main code, I had
\hyperlink{name-target}{\FirstLanguageSecondLanguage{}{}}
with some missing closing braces.
- Some missing closing braces at the end of the last argument of another of my "home-made" command, not related to \includepdf.
TextWrangler / View / Balance or Balance and fold can help in looking for the "missing closing braces". But, be sure to close TeXShop when doing that.
Here is my final code without bug: \IncludeMyPDF
%!TEX TS-program = pdflatex
\PassOptionsToPackage{table,x11names,svgnames}{xcolor}
\documentclass[10pt,twoside,openright,usletter]{book}
\usepackage{lipsum}
\usepackage{pdfpages}
\usepackage{hyperref}
\usepackage{minitoc}
\usepackage{url}
\usepackage{fancyhdr}
\hypersetup{
backref=true,
pagebackref=true,
hyperindex=true,
colorlinks=true,
breaklinks=true,
urlcolor= blue,
linkcolor= purple,
bookmarks=true,
pdftoolbar=true,
pdfmenubar=true,
bookmarksopen=true,
pdftitle={title of book},
pdfauthor={author of book},
pdfsubject={subject of book}
}
%---------------------------- begin selection of language
% 2 languages, here = second language selected
\mtcselectlanguage{english} % for minitoc
\newcommand{\FirstLanguageSecondLanguage}[2]{#2}
%---------------------------- begin macro for including a PDF document
% includepdf syntax:
% addtotoc={⟨page number⟩,⟨section⟩, ⟨level⟩,⟨heading⟩,⟨label⟩}
% addtolist={⟨page number⟩,⟨type⟩,⟨heading⟩,⟨label⟩}
%
% \IncludeMyPDF
% {1} % page number to be included
% {0.9} % scale
% {true} % landscape = true or false
% {false} % turn = true or false
% {subsection,2} % level in TOC: section, subsection, subsubsection + level 1,2,3
% {TitleTOC} % heading for TOC / list
% {Label} % label: label-toc-#7, label-list-#7, #7-target for hyperlinks
% {table} % addtolist = table or figure
% {mindmaps.pdf} % file
\newcommand{\IncludeMyPDF}[9]{%
\newpage\hypertarget{#7-target}
{\includepdf[pages={#1},nup=1x1,
scale=#2,landscape=#3,turn=#4,
pagecommand={\thispagestyle{fancy}},
addtotoc={#1,#5,#6,label-toc-#7},
addtolist={#1,#8,#6,label-list-#7}]
{#9}}}
%---------------------------- end macro for including a PDF document
\title{Title of book}
\author{Author of book}
\date{\today}
\begin{document}
\frontmatter
\maketitle
\cleardoublepage
\dominitoc
\dominilof
\tableofcontents
\mainmatter
\cleardoublepage
\chapter{Chapter one}
\minitoc
\cleardoublepage
\section{Section one Chapter one}
\lipsum
\IncludeMyPDF
{2}
{1}
{false}
{false}
{subsection,2}
{\FirstLanguageSecondLanguage{Nam dui ligula, fringilla}{Hamlet speaks this on his entry to Act 3 scene 1}}
{table-mind-one}
{table}
{mindmaps.pdf}
\IncludeMyPDF
{1}
{0.9}
{false}
{true}
{subsection,2}
{\FirstLanguageSecondLanguage{Nam dui ligula, fringilla a}{Hamlet speaks this on his entry to Act 3 scene 1}}
{figure-mind-one}
{figure}
{mindmaps.pdf}
\chapter{Chapter two}
\minitoc
\cleardoublepage
\section{Section one Chapter two}
\lipsum
\section{Section two Chapter two}
\lipsum
\IncludeMyPDF
{1}
{0.9}
{false}
{true}
{subsection,2}
{\FirstLanguageSecondLanguage{Nam dui ligula, fringilla}{To be, or not to be, that is the question}}
{figure-mind-two}
{figure}
{mindmaps.pdf}
\IncludeMyPDF
{1}
{0.9}
{false}
{true}
{subsection,2}
{\FirstLanguageSecondLanguage{Nam dui ligula, fringilla}{To be, or not to be, that is the question}}
{table-mind-two}
{table}
{mindmaps.pdf}
\IncludeMyPDF
{2}
{0.9}
{false}
{true}
{subsection,2}
{\FirstLanguageSecondLanguage{Nam dui ligula, fringilla}{To be, or not to be, that is the question}}
{nrp-algo}
{figure}
{mindmaps.pdf}
\cleardoublepage
\listoftables
\addcontentsline{toc}{chapter}{List of Tables}
\cleardoublepage
\listoffigures
\addcontentsline{toc}{chapter}{List of Figures}
\cleardoublepage
\end{document}
Best Answer
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 isThe 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 withEt voilà.