1

I have used the StylishArticle class several times, but I recently thought it would be a good idea to reference sections based on their names instead of using sections numbers or pageref.

So I defined \newcommand{\appref}[1]{Appendix:\nameref{#1}} for use with \subsection* (not numbered), and tested it with a normal article class. Seems to be working fine.

However when I use it with StylishArticle, it doesn't seem to work so well, and I get a warning:

Package hyperref Warning: Suppressing empty link on input line 86.

This link will get you to an Overleaf document (read-only) so you can see for yourself. I tried to keep the example small, but I don't think it's minimal; I need help identifying what's causing this issue. Any help greatly appreciated!


Following @egreg suggestion, here is a much shorter MWE:

\documentclass[12pt]{article}
\usepackage{titlesec}
\usepackage[unicode=true]{hyperref}
\newcommand{\appref}[1]{Appendix:\nameref{#1}}

\begin{document}

    \section{A normal section}
    Reference to \appref{app}.

    \phantomsection
    \section*{Appendices}
    \label{app}
    \addcontentsline{toc}{section}{Appendices}

\end{document}
  • @Sh3ljohn The life time of the link target can be different of the life time of the question. Therefore, it is better to include the short document in the question text. – Heiko Oberdiek Oct 2 '16 at 14:48
  • @HeikoOberdiek Please see the latest edit, sorry about the length, but that's as short as it gets if I should include the class-file. – Jonathan H Oct 2 '16 at 14:51
  • I can easily make up a non working example with the article class, by just loading titlesec. – egreg Oct 2 '16 at 14:52
  • @egreg Very sorry, but I don't know how to do that. :/ – Jonathan H Oct 2 '16 at 14:53
  • 1
    @Sh3ljohn \documentclass{article}\usepackage{titlesec}\usepackage{hyperref}, then the definition of \appref and the same body as your test document. – egreg Oct 2 '16 at 15:04
2

From hyperref's README:

titlesec

nameref supports titlesec, but hyperref does not (unsolved is the anchor setting, missing with unnumbered section, perhaps problems with page breaks with numbered ones).

| improve this answer | |
  • Right, so there is no way I can link the names of my unnumbered sections then? – Jonathan H Oct 2 '16 at 15:16
  • @Sh3ljohn Does the answer of the duplicate, found by you, works for you? – Heiko Oberdiek Oct 2 '16 at 16:46
  • Sorry for being so demanding, but although I understand that titlesec is causing the issue, and I understand that @egreg defined new commands for subsubsections, I am not sure how to implement this in the class-file linked in the OP.. I can see a titleformat definition in there for (sub(sub))sections, but there is also a special definition for \titleformat{name=\section,numberless} and I don't know how to adapt the answer from the duplicate to that case. – Jonathan H Oct 2 '16 at 17:40
1

As in @egreg's answer, the following definitions did it for me, although this implied using \appsubsec{name}{app:label} instead of the usual \subsection*{name}\label{app:label}.

\makeatletter
\newcommand{\appsec}[2]{%
  \phantomsection
  \refstepcounter{section}%
  \section*{#1}%
  \addtocounter{section}{-1}%
  \def\@currentlabelname{App: #1}%
  \def\@currentlabel{\thesection}%
  \label{#2}%
  \addcontentsline{toc}{section}{#1}%
}
\newcommand{\appsubsec}[2]{%
  \refstepcounter{subsection}%
  \subsection*{#1}%
  \addtocounter{subsection}{-1}%
  \def\@currentlabelname{App: #1}%
  \def\@currentlabel{\thesubsection}%
  \label{#2}%
}
\newcommand{\appsubsubsec}[2]{%
  \refstepcounter{subsubsection}%
  \subsubsection*{#1}%
  \addtocounter{subsubsection}{-1}%
  \def\@currentlabelname{App: #1}%
  \def\@currentlabel{\thesubsubsection}%
  \label{#2}%
}
\makeatother

These commands make sure that only appsec (the equivalent of section*) is entered into the table of contents, so that you only see one entry Appendices (which should be defined in your document to mark the beginning of appendices), and not each appendix listed as a normal subsection.

Note that these commands require a label (the second input is not optional), and that calling \nameref{app:label} in your document will yield a link with text App: name.

| improve this answer | |
  • Thank you! And I just want to throw this out there for anyone else; make sure you use \makeatletter and \makeatother or you won't get any effect. – user59034 Nov 5 '16 at 19:04

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.