3

Is there a way to disable nesting in hyperrefs? Here is an example:

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}

\begin{document}

\tableofcontents
\newpage

\section{One}
\label{sec:one}
\newpage

\section{\nameref{sec:one} Two}

\end{document}

The hyperref of the second section in the table of contents will consist of two hyperrefs one for "One" that leads to the first section and one for "One Two" that leads to the second section where "One" lays over the link for "One Two".

But I would like to have only one link there that leads to the second section if clicks anywhere on "One Two" in the TOC entry for the second section. So, I would like hyperrefs not to be nested into others.

4

More recent releases of the hyperref-bundle and the nameref-package provide so-called "starred" variants of referencing-commands that do produce textual phrases but do not produce hyperlinks.

Within your \section-command you can use the starred variant of \nameref.

But there are some pitfalls:

Be aware that with page-style "headings" arguments of sectioning-macros also go into page-headers while everything within the page-header gets "uppercased" after expansion which includes names of labels occurring within the arguments of sectioning-macros—a circumstance which in turn might lead to undefined-reference-errors.

This example does exhibit the problem:

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}
\pagestyle{headings}

\begin{document}

\tableofcontents
\newpage

\section{One}
\label{sec:one}
\newpage

\section{\nameref*{sec:one} Two}

\end{document}

With this example, you will never get rid of the message about undefined references. The reason is that within page-headers due to uppercase it is attempted to refer to a label "SEC:ONE" while it should be "sec:one".

You might get the idea of using referencing-labels whose names consist of uppercase-letters only. If you do so, referencing works but unlike the other things within the page-headers the result of referencing will not be in uppercase letters within the page-headers:

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}
\pagestyle{headings}

\begin{document}

\tableofcontents
\newpage

\section{One}
\label{SEC:ONE}
\newpage

\section{\nameref*{SEC:ONE} Two}

\end{document}

Therefore I suggest using the expandable referencing-commands of the refcount-package within the arguments of sectioning-commands.

Solution 1:

In case you don't like hyperlinks at all:

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}
\usepackage{refcount}

\makeatletter
\newcommand\questionmarks{\texorpdfstring{\nfss@text{\reset@font\bfseries??}}{??}}
\makeatother

\pagestyle{headings}

\begin{document}

\tableofcontents
\newpage

\section{One}
\label{sec:one}
\newpage

\section[\getrefbykeydefault{sec:one}{name}{\questionmarks} Two]%<- This goes to toc and pdf-bookmarks (and page-headers).
        {\nameref*{sec:one} Two} %<- This goes to main text.

\end{document}

Solution 2:

In case you wish hyperlinks in the main text:

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}
\usepackage{refcount}

\makeatletter
\newcommand\questionmarks{\texorpdfstring{\nfss@text{\reset@font\bfseries??}}{??}}
\makeatother

\pagestyle{headings}

\begin{document}

\tableofcontents
\newpage

\section{One}
\label{sec:one}
\newpage

\section[\getrefbykeydefault{sec:one}{name}{\questionmarks} Two]%<- This goes to toc and pdf-bookmarks (and page-headers).
        {\texorpdfstring{\nameref}{\nameref*}{sec:one} Two} %<- This goes to main text.

\end{document}
  • Unfortunately, I can't follow what the problem is. Is it a general problem or only when using nameref*? Maybe you can change the document so it shows the problem? – Daniel Dec 9 '18 at 16:12
  • 1
    @Daniel It is a general problem that occurs when using \pagestyle{headings} and \nameref within arguments of sectioning-commands: That page-style does print headings consisting of the sectioning-title in uppercased letters. For one thing the letters forming names of referencing-labels will get uppercased, too, which might cause "undefined references"-errors. For another thing, if you use label-names with uppercase-letters, the results of referencing will not be uppercased. Solution: expandable referencing-macros of the refcount-package. – Ulrich Diez Dec 9 '18 at 16:18
  • Okay, I think I got it, thanks. I still have to figure out what other trouble looms since I am not only using nameref for headings. – Daniel Dec 9 '18 at 16:26
4

Use the starred \nameref:

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}

\begin{document}

\tableofcontents
\newpage

\section{One}
\label{sec:one}
\newpage

\section{\nameref*{sec:one} Two}

\end{document}
  • Works great. I cannot add stars to all my namerefs. Do you happen to know how to replace the nameref command with the nameref* command? – Daniel Dec 9 '18 at 15:38
  • Yes, but it would more sensible to do a search and replace - it can't take really long. – Ulrike Fischer Dec 9 '18 at 15:43
  • Yes, but unfortunately, I cannot do a search and replace since I am using LyX. (And apparently, it does not currently support the starred commands. Though I will suggest it to the developers.) – Daniel Dec 9 '18 at 15:46
  • @UlrikeFischer Okay, I might have understood why replacing one with the other command is not a good idea. It will not only get rid of nested hyperlinks but also non-nested hyperlinks which is undesirable. I guess I could use LyX's functionality of inserting just the name of a label and then build my own nameref* inset. – Daniel Dec 9 '18 at 16:09

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.