6

Short version:

How can I solve the problem of using hyperref links when expanded by MakeUpperCase?

Long version: If I try to redefine:

\renewcommand{\descriptionlabel}[1]{\hspace*{\labelsep}%
\small\spacedallcaps{#1}}

where:

\DeclareRobustCommand{\spacedallcaps}[1]{\sffamily%
\textssc{\MakeTextUppercase{#1}}}%

So when I want to use a description:

\begin{description}
\item[{\hyperref[cap:first]{first chapter}}] Bla bla
\end{description}

The link doesn't work because hyperref is looking for CAP:FIRST.

How can I fix it (yeah, I could redefine all the lowercase label to uppercase, but I guess there could be something else that works.

Edit:

I am not always using hyperref within the description, but I would prefer to keep the MakeUpperCase, rather than rewriting every description item. I mean, if I really have to do it, I can, (as I could redefine the labels), but I am still interested in a more general solution (if it exists).

Pierpaolo
  • 786
  • What is your documentclass? There is a known conflict between hyperref and memoir with \MakeTextUpperCase. – musarithmia Jan 29 '15 at 02:54
  • Komascript+classicthesis – Pierpaolo Jan 29 '15 at 03:20
  • It might be the same underlying problem. It's discussed in the memoir manual and in this question by me: http://tex.stackexchange.com/questions/219134/how-to-apply-memoir-manuals-fix-to-allow-maketextuppercase-in-toc-with-hyperre – musarithmia Jan 29 '15 at 03:55
  • Tell us a little more about your usage of the description environment. Do you always have a \hyperref in there? Also, why not just use \hyperref[..]{FIRST CHAPTER}? How extensive is this usage? Don't try to build a castle when all you need is a doll house. – Werner Jan 29 '15 at 03:58
  • I am not always using hyperref within the description, but I would prefer to keep the MakeUpperCase, rather than rewriting every description item. I mean, if I really have to do it, I can, (as I could redefine the labels), but I am still interested in a more general solution (if it exists). – Pierpaolo Jan 29 '15 at 04:46

1 Answers1

5

Under memoir, the following works:

enter image description here

\documentclass{memoir}
\usepackage{hyperref}
\newif\ifhyperrefindesclabel

\makeatletter
\renewcommand{\descriptionlabel}[1]{%
  \let\oldhyperref\hyperref% Store \hyperref in \oldhyperref
  \hyperrefindesclabelfalse% No \hyperref in description
  \renewcommand{\hyperref}[2][]{% Update \hyperref to
    \gdef\h@optarg{##1}% ... store optional argument
    \gdef\h@manarg{##2}% ... store mandatory argument
    \global\hyperrefindesclabeltrue}% ... and mark as \hyperref-being-used
  \setbox1=\hbox{#1}% Evaluate \descriptionlabel argument
  \hspace*{\labelsep}% Insert space
  \small% Change font
  \ifhyperrefindesclabel% Was \hyperref used?
    \oldhyperref[\h@optarg]{\spacedallcaps\h@manarg}% Call \oldhyperref
  \else% ... no \hyperref was used
    \spacedallcaps{#1}% Re-set label using \spacedallcaps
  \fi
  }
\DeclareRobustCommand{\spacedallcaps}[1]{\sffamily%
  \textsc{\MakeTextUppercase{#1}}}%

\makeatother
\begin{document}

\section{First section}\label{sec:first}

\begin{description}
  \item[{\hyperref[sec:first]{first section}}] Bla bla

  \item[second section] Bla bla
\end{description}

\end{document}

The idea behind the enlarged \descriptionlabel definition is to set a flag (\ifhyperrefindesclabel) whenever you use \hyperref inside a description label. Depending on this condition, \hyperref is restored in a modified way to use \spacedallcaps in the second/mandatory argument only.

Werner
  • 603,163