# \nameref only shows reference as a red rectangle, no name in it

I'm trying to use \usepackage{hyperref} to reference in the text a section by name. The section to be referenced is

\section{whatever}\label{sec:marker}


However, when I place in the text

\nameref{sec:marker}


I get nothing but a narrow red rectangle without a text in it (I expected the text 'whatever' to appear there). Is there anything else I should do to fix this problem?

Here is a MWE of the problem I have:

\documentclass[onecolumn,twoside]{revtex4}
%\usepackage{float}
%\usepackage{graphicx}
%\usepackage{sansmath}
%\usepackage{amsmath}
%\usepackage{tikz}
%\pdfpagewidth 8.32in
%\pdfpageheight 11in
%\setlength{\topmargin}{-20mm}
%\setlength{\columnsep}{10mm}
%\setlength{\oddsidemargin}{-2mm}
%\setlength{\evensidemargin}{-2mm}
%\setlength{\textwidth}{170mm}
%\usepackage{anttor}
%\bibpunct{(}{)}{;}{s}{,}{,}
%\usepackage{fancyhdr}
%\usepackage{longtable}
%\usepackage{pgothic}
%\usepackage[T1]{fontenc}
%\usepackage{eufrak}
%\usepackage{oldgerm}
%\usepackage[normalem]{ulem}
%\usepackage{cancel}
%\usepackage{wasysym}
\usepackage{hyperref}

\begin{document}

\section*{FirstSection}

In \nameref{sec:marker} it is stated that ...

\section*{SecondSection} \label{sec:marker}

\end{document}

-
Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Personally, I have often solved my own problems in the process of reducing the amount of code actually required to reproduce the problem. –  Peter Grill Feb 16 '12 at 4:06
A tip: If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the "code" button ({}). –  Peter Grill Feb 16 '12 at 4:06
I'm unable to reproduce the problem you describe having encountered, i.e., I get a nice little red rectangle with "whatever" inside it. Please provide a full, compilable MWE that generates the (unwanted) behavior. –  Mico Feb 16 '12 at 4:20
The problem apparently is in revtex4 –  ganzewoort Feb 16 '12 at 4:58
Thanks for posting a working example. As it's not exactly "minimal", I've taken the liberty of commenting out all statements in the preamble except the \usepackage{hyperref} instruction. That way, casual readers will pick up right away that the problem is due to some interaction between the revtex document class and the hyperref package. –  Mico Feb 16 '12 at 14:34

Yes, revtex4 plays some tricks on hyperref. That is, revtex4 redefines the sectional macros that hyperref is tapping into, leading to the "lost \nameref".

To fix this, you need the xpatch package and patch the correct sectional macros: \@ssect@ltx and \@sect@ltx, each of which are called depending on whether you use the starred variant (or not) of the sectional command.

Here's a minimal working example:

\documentclass{revtex4}% http://ctan.org/pkg/revtex4
\usepackage{xpatch}% http://ctan.org/pkg/xpatch
\makeatletter
\xpatchcmd{\@ssect@ltx}{\@xsect}{\edef\@currentlabelname{#8}\@xsect}{}{}% Patch \<section>*
\xpatchcmd{\@sect@ltx}{\@xsect}{\edef\@currentlabelname{#8}\@xsect}{}{}% Patch \<section>
\makeatother
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\begin{document}
\section{FirstSection} \label{sec:marker1}
In \nameref{sec:marker2} it is stated that \ldots

\section*{SecondSection} \label{sec:marker2}
You can also see in \nameref{sec:marker1} that \ldots
\end{document}


\nameref relies on the macro \@currentlabelname that contains the title used in the sectional unit. This title is stored in argument #8 with a call to either \@ssect@ltx or \@sect@ltx. So, the patch restores this definition at an appropriate place within the macros.

-
It might be worth mentioning that this patch works (and is needed!) for the revtex4-1 document class as well. –  Mico Feb 16 '12 at 14:29
Thanks @Werner. Unfortunately, I'm having trouble installing xpatch package. Should I open a new question to address this problem or I should continue discussing it here? –  ganzewoort Feb 16 '12 at 20:07
We can try and hash it out here. If not, it's best to post a ollow-up question. If you're using TeX Live, you should use, from the command line, tlmgr install xpatch, or better yet, tlmgr update --self --all --reinstall-forcibly-removed. If you're running MiKTeX, you need to use the MiKTeX Package Manager. This is better than installing the packages yourself. The package managers put stuff in the right location, and update the appropriate filename databases (FNDB). –  Werner Feb 16 '12 at 20:11
I'm using MikTeX but no repository I've tried so far seems to carry xpatch. Do you know of any that would? –  ganzewoort Feb 16 '12 at 20:16
The repositories available in MiKTeX do not list the one you pointed to: ctan.org/pkg/xpatch. How can I reach it through MikTeX? –  ganzewoort Feb 16 '12 at 20:41