4

I used to set section headings in uppercase with the sectsty package \MakeUppercase. But this gives problems when using references in section headings. So how does one do it properly?

Here is a minimal example:

\documentclass{article}

\usepackage{nameref}
\usepackage{sectsty}
\sectionfont{\MakeUppercase}

\begin{document}

\begin{description}
  \item [Keyword\label{key}] This is the description text.
\end{description}

\section{Here it does not work: \nameref{key}}

Here it works: \nameref{key}.

\end{document}
Daniel
  • 1,787

1 Answers1

3

You need an expandable version of \nameref.

\documentclass{article}

\usepackage{nameref}
\usepackage{sectsty}
\sectionfont{\MakeUppercase}

\makeatletter
\newcommand\enameref[1]{%
  \expandafter\ifx\csname r@#1\endcsname\relax
  ??\typeout{^^JLaTeX Warning: Reference #1 undefined on input line \the\inputlineno}%
  \else
    \expandafter\expandafter\expandafter\@thirdoffive\csname r@#1\endcsname
  \fi
}
\makeatother

\begin{document}

\begin{description}
  \item [Keyword\label{key}] This is the description text.
\end{description}

\section{Here it does not work: \enameref{key}}

Here it works: \nameref{key}.

\end{document}

enter image description here

egreg
  • 1,121,712
  • Sorry, I must somehow have overlooked your reply. Yes, that works. Now I wonder: why is this not the default definition of nameref? Also, how would I overwrite the default nameref command to use the expandable version instead. I tried to just remove the "e" in your newcommand but that didn't work. – Daniel Oct 07 '17 at 17:51