Post by unknownThanks Ulrich - that is a very comprehensive answer which I shall do my
best to work through (by a process of trial-and-error) and understand.
I made a big big mistake - within the macro \NoSecNums
I used "\setcounter{secnumdepth}".
The resulting assignment will be global.
But it is to be restricted to the current scope.
Thus this needs to be changed to "\***@secnumdepth=-1\relax" .
Below the corrected code.
Please accept my humble apologies.
There are four user-macros, each of them taking an optional
argument and a mandatory argument.
With each of these macros, the mandatory argument is to be a
sectioning-command.
1. If the sectioning-command is wrapped to be the mandatory
argument of the macro \TOCnohrefs, then no hyperlink linking
from the corresponding entry within the table of contents
towards the sectioning-heading in the text is created.
2. If the sectioning-command is wrapped to be the mandatory
argument of the macro \TOCnopdfbookmarks, then in pdf-files
no bookmark linking towards the sectioning-heading in the
text is created.
3. If the sectioning-command is wrapped to be the mandatory
argument of the macro \TOCNoPageNums, then the entry within
the table of contents will not denote the page-number
where the corresponding sectioning-heading occurs.
4. If the sectioning-command is wrapped to be the mandatory
argument of the macro \NoSecNums, then the corresponding
sectioning-header is not numbered.
As said above, each of the macros \TOCnohrefs, \TOCnopdfbookmarks,
\TOCNoPageNums and \NoSecNums also processes an optional argument.
That optional argument is intended to consist of a set of tokens
that in turn will process a single trailing mandatory argument.
You can use this for combining either of the four macros with either
of the other ones.
E.g.,
\TOCnohrefs{\chapter{Nice chapter}}
will produce both a bookmark, a numbered chapter-heading, an entry
within the table of contents also denoting the number of the page
where the chapter-heading occurs.
But there will not be a hyperlink from the entry within the table of
contents towards the corresponding chapter-heading.
E.g.,
\TOCnohrefs[\NoSecNums]{\chapter{Nice chapter}}
will produce both a bookmark, a chapter-heading, an entry
within the table of contents also denoting the number of the page
where the chapter-heading occurs.
But there will not be a hyperlink from the entry within the table of
contents towards the corresponding chapter-heading.
Also, this time the chapter will not be numbered.
E.g.,
\NoSecNums[{\TOCnohrefs[\TOCNoPageNums]}]{\chapter{Nice chapter}}
will produce both a bookmark, a chapter-heading, an entry
within the table of contents.
But there will not be a hyperlink from the entry within the table of
contents towards the corresponding chapter-heading.
Also, this time the chapter will not be numbered.
The entry within the table of contents does not denote the number
of the page where the chapter-heading occurs.
This nesting of optional arguments might be cumbersome.
The macro \settoclinks[<comma-separated key-list>]{<sectioning-command>}
will do that for you.
The keys are similar to the names of the macros above:
TOCnohrefs, TOCnopdfbookmarks, TOCNoPageNums, NoSecNums .
Ulrich
\documentclass{report}
\usepackage{tocloft}
\usepackage{keyval}
\usepackage{hyperref}
\makeatletter
\newcommand\TOCnopdfbookmarks[2][\@firstofone]{%
\begingroup
\let\WriteBookmarks\relax
%\***@bookmarksdepth=-1
#1{#2}%
\endgroup
}%
\newcommand\TOCnohrefs[2][\@firstofone]{%
\addtocontents{toc}{\string\begin{NoHyper}}%
#1{#2}%
\addtocontents{toc}{\string\end{NoHyper}}%
}%
\newcommand\NoSecNums[2][\@firstofone]{%
\begingroup
\***@secnumdepth=-1\relax
#1{#2}%
\endgroup
}%
\newcommand\TOCNoPageNums[2][\@firstofone]{%
\addtocontents{toc}{\begingroup\SetCftStuff}%
#1{#2}%
\addtocontents{toc}{\endgroup}%
}%
\DeclareRobustCommand\SetCftStuff{%
\renewcommand{\cftdotsep}{\cftnodots}%
\cftpagenumbersoff{chapter}%
\cftpagenumbersoff{section}%
\cftpagenumbersoff{subsection}%
\cftpagenumbersoff{subsubsection}%
}%
%% Comma-List/KeyVal-"interface" :
%%
%% \settoclinks[<comma-separated key-list>]{<sectioning-command>}
%%
\newcommand\settoclinks[2][]{%
\begingroup
\def\@settoclinks{\@firstofone}%
\setkeys{toclinks}{#1}%
\expandafter\endgroup\@settoclinks{#2}%
}%
\begingroup
\@temptokena{\endgroup}%
\def\toclinkskeydefiner#1{%
\@temptokena\expandafter{\the\@temptokena
\***@key{toclinks}{#1}{\novalueerror{#1}}%
\@namedef{***@toclinks@#***@default\expandafter}%
\expandafter{%
\expandafter\@settoclinksdef
\expandafter{\csname#1\endcsname}}%
}%
}%
\toclinkskeydefiner{TOCnohrefs}%
\toclinkskeydefiner{TOCnopdfbookmarks}%
\toclinkskeydefiner{NoSecNums}%
\toclinkskeydefiner{TOCNoPageNums}%
\the\@temptokena
\newcommand\novalueerror[1]{%
\PackageError{settoclinks}{The key #1 does not take values}{\@ehc}%
\csname ***@toclinks@#***@default\endcsname
}%
\newcommand\@settoclinksdef[1]{%
\expandafter\def\expandafter\@settoclinks\expandafter{%
\expandafter#1\expandafter[\expandafter{\@settoclinks}]}%
}%
\makeatother
\begin{document}
% Nest optional arguments by wrapping them
% into a pair of curly braces.
\tableofcontents
\chapter{First Chapter: Link in toc and bookmark}
\newpage
\TOCnohrefs{\chapter{Second Chapter: No link in toc but bookmark}}
\newpage
\TOCnopdfbookmarks{\chapter{Third Chapter: No bookmark but link in toc}}
\newpage
\TOCnohrefs[\TOCnopdfbookmarks]%
{\chapter{Fourth Chapter: No Link in toc and no bookmark}}
\newpage
\TOCnopdfbookmarks[\TOCnohrefs]%
{\chapter{Fifth Chapter: No Link in toc and no bookmark}}
\newpage
\NoSecNums{\chapter{Sixth Chapter: Link in toc and no sectioning-number}}
\newpage
\NoSecNums[\TOCnohrefs]%
{\chapter{Seventh Chapter: No link in toc and
no sectioning-number}}
\newpage
\NoSecNums[\TOCnopdfbookmarks]%
{\chapter{Eighth Chapter: No bookmark and no
sectioning-number}}
\newpage
\NoSecNums[{\TOCnohrefs[\TOCnopdfbookmarks]}]%
{\chapter{Ninth Chapter: No link and
no bookmark and
no sectioning-number}}
\newpage
\TOCNoPageNums[{\NoSecNums[{\TOCnohrefs[\TOCnopdfbookmarks]}]}]%
{\chapter{Tenth Chapter: No link and
no bookmark and
no sectioning-number
and no page-number}}
\newpage
\chapter{Eleventh Chapter: Link in toc and bookmark}
\newpage
\settoclinks{\chapter{Twelfth Chapter: Link in toc and bookmark}}
\newpage
\settoclinks[TOCnohrefs]%
{\chapter{Thirteenth Chapter: No link in toc but bookmark}}
\newpage
\settoclinks[TOCnopdfbookmarks]%
{\chapter{Fourteenth Chapter: No bookmark but link in toc}}
\newpage
\settoclinks[TOCnohrefs, TOCnopdfbookmarks]%
{\chapter{Fifteenth Chapter: No Link in toc and no bookmark}}
\newpage
\settoclinks[TOCnopdfbookmarks, TOCnohrefs]%
{\chapter{Sixteenth Chapter: No Link in toc and no bookmark}}
\newpage
\settoclinks[NoSecNums]%
{\chapter{Seventeenth Chapter: Link in toc and
no sectioning-number}}
\newpage
\settoclinks[NoSecNums, TOCnohrefs]%
{\chapter{Eighteenth Chapter: No link in toc and
no sectioning-number}}
\newpage
\settoclinks[NoSecNums, TOCnopdfbookmarks]%
{\chapter{Nineteenth Chapter: No bookmark and no
sectioning-number}}
\newpage
\settoclinks[NoSecNums, TOCnohrefs, TOCnopdfbookmarks]%
{\chapter{Twentieth Chapter: No link and
no bookmark and
no sectioning-number}}
\newpage
\settoclinks[TOCNoPageNums, NoSecNums, TOCnohrefs, TOCnopdfbookmarks]%
{\chapter{Twenty-first Chapter: No link and
no bookmark and
no sectioning-number
and no page-number}}
\newpage
\settoclinks[]%
{\chapter{Twenty-second Chapter: Link in toc and bookmark}}
\newpage
\end{document}