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}

\protect\mathbf? – Henri Menke Dec 15 '22 at 20:31