0

The following MCE shows that pdfpages' addtotoc option works well, even with basic math but, if the commented line is uncommented, shows that it doesn't work with less basic math (here \mathbf, though this command works well in usual ToC), the compilation failing with the error:

! Undefined control sequence.
\GenericError  ...                                
                                                    #4  \errhelp \@err@     ...
l.11   }]{example-image.pdf}

Adding \protect before \mathbf{R}^{n} doesn't help.

Do you understand what is going on and how to get around it?

\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\tableofcontents
\includepdf[
  pages=-,
  addtotoc={
    1,section,1,{No math},ex-1
    , 1,section,1,{Basic math: $R^{n}$},ex-2
    % , 1,section,1,{Not so basic math: $\mathbf{R}^{n}$},ex-3
  }]{example-image.pdf}
\section{$\mathbf{R}^{n}$}
\end{document}
Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85

3 Answers3

2

This is one possibility.

With unicode-math you can use \symbf{<characters>} which works for both Greek and Latin letters.

Compile with xelatex or lualatex.

bold math symbols

\documentclass{article}

\usepackage{unicode-math}% added <<<<<<<< \setmathfont{XITSMath-Regular.otf}% added <<<<<<<<

\usepackage{pdfpages} \begin{document} \tableofcontents \includepdf[ pages=-, addtotoc={ 1,section,1,{No math},ex-1 , 1,section,1,{Basic math: $R^{n}$},ex-2 , 1,section,1,{Not so basic math: $\symbf{R}^{n}$},ex-3 %changed <<<<< }]{example-image.pdf} \section{$\mathbf{R}^{n}$} \end{document}

a

Or

\documentclass{article}

\usepackage{fontspec} % added <<<< \usepackage{unicode-math}% added <<<<

\usepackage{pdfpages} \begin{document} \tableofcontents \includepdf[ pages=-, addtotoc={ 1,section,1,{No math},ex-1 , 1,section,1,{Basic math: $R^{n}$},ex-2 , 1,section,1,{Not so basic math: $\mathbf{R}^{n}$},ex-3 }]{example-image.pdf} \section{$\mathbf{R}^{n}$} \end{document}

Simon Dispa
  • 39,141
2

pdfpages uses \edef a bit to often ...

\documentclass{article}
\usepackage{pdfpages}
\makeatletter
\define@key{pdfpages}{addtotoc}{\protected@edef\AM@toclist{#1,}}%replace \edef
\makeatother

\begin{document} \tableofcontents
\includepdf[ pages=-, addtotoc={ %1,section,1,{No math},ex-1 %, 1,section,1,{Basic math: $R^{n}$},ex-2 1,section,1,{Not so basic math: $\mathbf{R}^{n}$},ex-3 }]{example-image.pdf} \section{$\mathbf{R}^{n}$} \end{document}

Ulrike Fischer
  • 327,261
0

Finally, I found a solution. As shown by the following MCE, it is enough (and maybe necessary for commands with arguments: see \textit) to prevent the expansion of the culprit commands with \noexpand:

\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\tableofcontents
\includepdf[
  pages=-,
  addtotoc={
    1,section,1,{No math},ex-1
    , 1,section,1,{Basic math: $R^{n}$},ex-2
    , 1,section,1,{Not so basic math: {$\noexpand\mathbf{R}^{n}$}},ex-3
    , 1,section,1,{Not so basic command: \noexpand\textit{Foo}},ex-4
  }]{example-image.pdf}
\section{$\mathbf{R}^{n}$}
\end{document}
Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85