# How to put non-return function and return function in \newcommand?

During my work, I came across a need to insert pages from PDF document (which i used \usepackage{pdfpages} and \includepdf[option]{doc.pdf}) and I wanted to add these pages into \tableofcontents with the section number increase according. I did it with

\stepcounter{section}


However, when I tried to park these functions into a \newcommand it returns an error.

Define Command:

\newcommand{\addtosection}
{
\stepcounter{#1}
\the\value{section}
\hspace{10pt}
}


Execution of Command:

\addcontentsline{toc}{section}{\addtosection <text>}


Error:

   ! Missing \endcsname inserted. <to be read again>
\csname\endcsname
l.24 ...pcounter{section} \addtosection Part Four}
The control sequence marked <to be read again> should not appear between \csname and \endcsname.
[1
{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}


What I have figured

I realised that when I put \stepcounter{section} into \addcontentsline{toc}{section}{\stepcounter{section} <text>}, it will give an error saying

Missing \endcsname inserted. <to be read again> \csname\endcsname


Simplified code of my application

\documentclass[11pt]{article}

{%\stepcounter{#1}
\the\value{section}%
\hspace{10pt}%
}

\begin{document}

\tableofcontents

{---------------------------}

\section{Part One} % 1 Part One

\section*{Part Two}
\stepcounter{section}
\addcontentsline{toc}{section}{\thesection \hspace{7pt} Part Two} % 2 Part Two

\addcontentsline{toc}{section}{\addtosection Part Three} % 3 Part Three

\section{Part Four}

\end{document}


   Error: % (on line 3 Part Three)

! Missing \endcsname inserted. <to be read again>
\csname\endcsname
l.24 ...pcounter{section} \addtosection Part Four}
The control sequence marked <to be read again> should not appear between \csname and \endcsname.
[1
{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}

• Welcome! You are trying to increment the section counter in the .toc file which is not what you want.... – cfr Oct 29 '15 at 2:27

I would use (do use, in fact) a custom command. For example,

\includepdftoc[<options>]{<title>}{<filename>}


will increment the section counter, add <title> to the ToC as a section and include <filename.pdf>. The number used in the ToC will be the first page of the PDF. If the optional argument is used, it will be passed to \includepdf.

\documentclass[11pt]{article}
\usepackage{xparse,pdfpages}
\makeatletter
\NewDocumentCommand\includepdftoc { O {} m m }{%
\stepcounter{section}%
\def\addfirsttotoc{% from latex.ltx (modified)
\protect\numberline{\csname thesection\endcsname}%
#2}}%
\includepdf[pagecommand={\makedynstyle},#1]{#3}%
}
\makeatother
\begin{document}
\tableofcontents
\bigskip

\hrule\bigskip

\section{Part One} % 1 Part One
\section*{Part Two}
\includepdftoc{Part Two}{example-image-a} % 2 Part Two
\includepdftoc[pages={-}]{Part Three}{forest-prooftrees-egs} % 3 Part Three
\section{Part Four}
\end{document}


• doesn't \thesection already returns the current section number? why do you need to do \protect\numberline{\csname thesection\endcsname}? how does \numberline, \csname help? – Hoejhl Oct 29 '15 at 5:26
• also, I realised that you define a lot of function in the \NewDocumentCommand. what is the need for \def\makedynstyle{\addfirsttotoc\global\let\addfirsttotoc\relax}? – Hoejhl Oct 29 '15 at 5:26
• Where can I learn about \global, \let, \relax, etc for better understanding? – Hoejhl Oct 29 '15 at 5:34
• The \makedynstyle ensures that the section is added to the ToC only on the first page of the included PDF. You don't want it added 10 times, say, for a 10 page PDF. The \protect is necessary, I think, because you are writing this to the .toc and that makes things complicated in terms of when things are expanded etc. \numberline is just for consistency with the standard class so that the entries added this way should hopefully get the standard formatting without manually imposing it. Makes it easier to keep things consistent. For\global,\let, \relax, probably the TeX Book. – cfr Oct 29 '15 at 12:49
• can't \thesection be executed separately from \includepdf? – Hoejhl Oct 29 '15 at 13:04