I'm trying to create a new command that will act like \nameref
but the output will manipulate the original name of what I'm referencing.
To be more specific, I'll give an example:
\documentclass{book}
\usepackage{hyperref}
\usepackage{xstring}
\makeatletter
\newcommand{\getnamereftext}[1]{%
\@ifundefined{r@#1}{}{%
\unexpanded\expandafter\expandafter\expandafter{%
\expandafter\expandafter\expandafter\@thirdoffive\csname r@#1\endcsname
}%
}%
}
\newcommand{\shortRef}[1]{%
\StrBefore{\getnamereftext{#1}}{:}[\myref]%
\IfBeginWith{\myref}{``}{\StrBetween{\myref}{``}{''}}{\myref}%
}
\begin{document}
\tableofcontents
\chapter{``First Part'': Second Part}
\label{ch:1}
\section{This section doesn't have parts}
\label{sc:1}
\section{section parts: it contains them}
\label{sc:2}
I want to be able to reference Section~``\shortRef{sc:2}'' and only show the first part.
But I also want it to be a link so it's clickable and pointing to the actual chapter/section.
I want \nameref{sc:2} to still display the full name.
And I want to remove any quotations from the first part if they are present.
So citing Chapter~``\shortRef{ch:1}'' wouldn't show double quotation marks.
\end{document}
I got the \getnamereftext
command from How to use \nameref with xstring package to check string length
I don't want to redefine \nameref
because I still want to be able to cite the chapter with its full name as shown in the example. I also don't want to use \chapter[short name]{long name}
because that will also make all \nameref
use the short name and, worst of all, my table of contents will also use the short name (this is very important, hence adding \tableofcontents
command to the example).
The code is also trying to remove quotation marks if present.
The missing part of the code is creating a link from the reference. I have no idea how to do this. Any help with this would be greatly appreciated.