5

I would like to use a full section title for:

  • the section title itself (of course)
  • the table of contents
  • the running heads

But, I would like a short name to be used with \nameref{}, because otherwise the reference is too long to work into the surrounding text effectively. (The reader finding the section is not a problem because the sections are in alphabetical order and the short version will be the beginning of the section title.) nameref uses the short title if one is provided, but then the short title ends up in the table of contents as well, which I don't want. Is there a way to get the table of contents to use the long titles, leaving the short title for nameref? Alternatively, if there's a way to specify a different title than the section title itself for nameref to use, that would work as well.

I found many other questions and tips about using a short version in page headers and a long version in the table of contents, but nothing that involved \nameref{}.

The page header is not a problem as I'm using memoir and can easily define the page header separately (as the same as the full title, in this case).

MWE:

\documentclass[twocolumn]{memoir}

\usepackage{hyperref}
\headnameref

% headers
\copypagestyle{maudcyclo}{plain}
\createmark{section}{both}{shownumber}{\sectionname}{}
\nouppercaseheads
\makeevenhead{maudcyclo}{\oldstylenums{\thepage}}{\scshape\leftmark}{}
\makeoddhead{maudcyclo}{}{\scshape\rightmark}{\oldstylenums{\thepage}}
\makeevenfoot{maudcyclo}{}{}{}
\makeoddfoot{maudcyclo}{}{}{}

\makeevenhead{plain}{\oldstylenums{\thepage}}{\leftmark}{}
\makeoddhead{plain}{}{\rightmark}{\oldstylenums{\thepage}}
\makeevenfoot{plain}{}{}{}
\makeoddfoot{plain}{}{}{}

% don't number chapters and below
\setcounter{secnumdepth}{-1}

\pagestyle{maudcyclo} % use header style

\begin{document}
\tableofcontents
\clearpage

\section[Section One][S1]{Section One}
\label{sec1}
Here is some text for section one.
\clearpage

\section[Section Two][S2]{Section Two}
Here I will reference \nameref{sec1}.

\end{document}

The above MWE works properly except for the fact that it places S1 and S2 in the header, whereas I want Section One and Section Two there.

5
  • Could you provide a minimal working example (MWE)? Aug 27, 2014 at 14:42
  • 3
    i'm not a memoir user, but a possible approach can be posited from the manual. two optional arguments are provided for the sectioning commands: [*toc name*][*head name*]{*full name*}, and \nameref can be set to use \headnameref (sec.16.2, p.303). so you could repeat the full name for the toc name, and insert your short name as the head name. Aug 27, 2014 at 15:44
  • @Barbara: That works, except it uses the short name in the header since I've now used the header argument that I was intending to make the same as the main title. (I did look in the manual but didn't see that part.) I would be willing to go with this if I have to, but I would rather get it working right. If there is a way to redefine \leftmark or \rightmark to use the full title or TOC title instead of the header title (which seems like an odd thing to do), that would fix it.
    – scorchgeek
    Aug 27, 2014 at 16:22
  • @Sean: Done. I had thought it wouldn't be useful for this problem, but I read the page you linked and that makes sense.
    – scorchgeek
    Aug 27, 2014 at 16:24
  • since the author of memoir observed that it was necessary to define \nameref within the class (on account of conflicts with the "usual" packages), it seems that your situation is a reasonable candidate for a feature/option request. Aug 27, 2014 at 16:35

2 Answers 2

3

Two applications of \patchcmd (from the etoolbox package) are sufficent:

\patchcmd{\H@old@sectm@m}
  {\csname #1mark\endcsname{#8}}
  {\csname #1mark\endcsname{#9}}
  {}{}
\patchcmd{\H@old@sectm@m}
  {\fi #7}
  {\fi #9}
  {}{}

Running heads will be the full section title while \nameref maintains its originary functionality:

title

And the TOC entries have the long form too:

contents

And for the sake of completeness, here is the complete code:

\documentclass[twocolumn]{memoir}

\usepackage{hyperref}
\headnameref

% headers
\copypagestyle{maudcyclo}{plain}
\createmark{section}{both}{shownumber}{\sectionname}{}
\nouppercaseheads
\makeevenhead{maudcyclo}{\oldstylenums{\thepage}}{\scshape\leftmark}{}
\makeoddhead{maudcyclo}{}{\scshape\rightmark}{\oldstylenums{\thepage}}
\makeevenfoot{maudcyclo}{}{}{}
\makeoddfoot{maudcyclo}{}{}{}

\makeevenhead{plain}{\oldstylenums{\thepage}}{\leftmark}{}
\makeoddhead{plain}{}{\rightmark}{\oldstylenums{\thepage}}
\makeevenfoot{plain}{}{}{}
\makeoddfoot{plain}{}{}{}

% don't number chapters and below
\setcounter{secnumdepth}{-1}

\pagestyle{maudcyclo} % use header style

\usepackage{etoolbox}

\makeatletter
\patchcmd{\H@old@sectm@m}
  {\csname #1mark\endcsname{#8}}
  {\csname #1mark\endcsname{#9}}
  {}{}
\patchcmd{\H@old@sectm@m}
  {\fi #7}
  {\fi #9}
  {}{}
\makeatother

\begin{document}
\tableofcontents
\clearpage

\section[Section One][S1]{Section One (long title)}
\label{sec1}
Here is some text for section one.
\clearpage

\section[Section Two][S2]{Section Two (long title)}
Here I will reference \nameref{sec1}.
\end{document}
3
  • I'm trying to implement this but it's not working for me. Is this dependent on using the memoir class? I'm not using that class, and it'll be great if your solution can work with any class.
    – Felipe
    Aug 8, 2019 at 0:49
  • @Felipe - Unfortunately latexdef H@old@sectm@m returns 'undefined'. Seems to me that it is a deprecated internal hyperref macro. I'll try to figure out how to implement the solution in the current version.
    – Ruben
    Aug 8, 2019 at 21:49
  • @Felipe - I have not tested it right now. What I think at the moment is that the \NR@sect macro of the nameref module from hyperref fulfills the same purpose as \H@old@sectm@m. Try \patchcmd{\NR@sect}...
    – Ruben
    Aug 8, 2019 at 22:12
0

If you want a different reference specifically for \nameref, then define a new name using the following macro:

\makeatletter
\newcommand{\namerefname}[1]{\edef\@currentlabelname{#1}}% New \nameref name
\makeatother

Here's your example showing its usage:

enter image description here

\documentclass[twocolumn]{memoir}

\usepackage{hyperref}
\headnameref

% headers
\copypagestyle{maudcyclo}{plain}
\createmark{section}{both}{shownumber}{\sectionname}{}
\nouppercaseheads
\makeevenhead{maudcyclo}{\oldstylenums{\thepage}}{\scshape\leftmark}{}
\makeoddhead{maudcyclo}{}{\scshape\rightmark}{\oldstylenums{\thepage}}
\makeevenfoot{maudcyclo}{}{}{}
\makeoddfoot{maudcyclo}{}{}{}

\makeevenhead{plain}{\oldstylenums{\thepage}}{\leftmark}{}
\makeoddhead{plain}{}{\rightmark}{\oldstylenums{\thepage}}
\makeevenfoot{plain}{}{}{}
\makeoddfoot{plain}{}{}{}

% don't number chapters and below
\setcounter{secnumdepth}{-1}

\pagestyle{maudcyclo} % use header style

\makeatletter
\newcommand{\namerefname}[1]{\edef\@currentlabelname{#1}}%
\makeatother

\begin{document}
\tableofcontents

\section[Section ONE]{Section One}
\namerefname{S1}\label{sec1}
Here is some text for \nameref{sec1}.

\section[Section TWO]{Section Two}
\label{sec2}
Here is some text for \nameref{sec2}.

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .