Discussion:
LyX/LaTeX help sought - how to suppress one hyperlink in a PDF
(too old to reply)
unknown
2013-08-24 16:42:25 UTC
Permalink
Hello

Short version: For my document I need every entry in my TOC to result in
internal hyperlinks (chapters, sections, etc.) when LyX compiles its PDF
output, except for one TOC entry which I don't want to have a hyperlink for.

Question: Please can someone help me with some ERT (for LyX v2.0.2 under
linux) or some LaTeX code to make it work?

background: In LyX I have a document written using "report" class and
the TOC correctly shows all chapters and Appendices. I managed to add a
"separator" word (which says "Appendices") at the end of the chapter
listing in the TOC, just before the Appendices (A,B,C...) are listed,
by inserting this bit of code in the body immediately after the last
chapter and just before the start of the appendices:

\chapter*{Appendices}
\thispagestyle{empty}
\addtocontents{toc}{\cftpagenumbersoff{chapter}}
\addcontentsline{toc}{chapter}{Appendices}
\addtocontents{toc}{\cftpagenumberson{chapter}}

(My LaTeX preamble, in LyX, is set to use the tocloft package and LyX
natively uses hyperref).

By design, the separator word does not have a page number associated in
the TOC when I compile a PDF, but that separator word is hyperlinked in
the resultant PDF to the last page of the last chapter. No good!

I want it to not hyperlink ideally, or to hyperlink to the first page of
the first appendix as a second-best.

So what do I need to add to the ERT or in the preamble to prevent the
separator word from linking elsewhere with tin the compiled PDF please?

I have read several dozen web pages and I have tried (and failed) to
read the hyperref description. Not being a programmer, I wasn't able to
fathom it, so if someone could provide some guidance please, I would be
very grateful.

Thanks in advance for any help you can offer.
Ulrich D i e z
2013-08-25 00:44:59 UTC
Permalink
Short version: For my document I need every entry in my TOC to result in
internal hyperlinks (chapters, sections, etc.) when LyX compiles its PDF
output, except for one TOC entry which I don't want to have a hyperlink for.
Question: Please can someone help me with some ERT (for LyX v2.0.2 under
linux) or some LaTeX code to make it work?
I think your "short version" covers a much broader scope of cases
than is covered by the "background-explanation".

Some LaTeX code using hyperref's NoHyper-environment.

Ulrich

\documentclass{report}
\usepackage{tocloft}
\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
\setcounter{secnumdepth}{-1}%
#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}%
}%
\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

\end{document}
Ulrich D i e z
2013-08-25 14:21:10 UTC
Permalink
Somebody (who claimed not to be the "unknown" person that
started this thread) asked via e-mail for comma-list/key(val)-syntax.

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
\setcounter{secnumdepth}{-1}%
#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}
unknown
2013-08-25 18:45:25 UTC
Permalink
Post by Ulrich D i e z
Short version: For my document I need every entry in my TOC to result
in internal hyperlinks (chapters, sections, etc.) when LyX compiles
its PDF output, except for one TOC entry which I don't want to have a
hyperlink for.
Question: Please can someone help me with some ERT (for LyX v2.0.2
under linux) or some LaTeX code to make it work?
Some LaTeX code using hyperref's NoHyper-environment.
Thanks 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.
Ulrich D i e z
2013-08-25 21:57:39 UTC
Permalink
Post by unknown
Thanks 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}
unknown
2013-08-25 22:26:26 UTC
Permalink
Post by Ulrich D i e z
Post by unknown
Thanks 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}".
Below the corrected code.
Please accept my humble apologies.
Crikey - many thanks indeed for putting so much effort into this for
me! It is very much appreciated.

Earlier today I compiled your example without fault into a PDF and I was
able to start to understand what you have done. I shall embrace all of
these comments and your kind correction and I think I should be able to
get this puzzle solved within no time.

Again, let me thank you very much for helping me Ulrich.

Richard

Loading...