This is another question about protection and expansion, that I still don't understand well… (and I guess this is my problem here). I read especially How do I "unprotect" an argument?, and many others.
I'm trying to create automatic and contextual labels for titles (See below for details if needed). If I write eg \myref{My_title_here}
, the link to the label is eg “A.II.3.d My title here”, and points to the 4th subsection of the 3rd section of the 2nd chapter in the 1st part. A.II.3.d
is the “title number”, and is the result of \getrefnumber {#1}
(here #1
is My_title_here
), and I want to get it in \templabel
, but:
%\edef \templabel {\getrefnumber {#1}} doesn't work
What I want is to get the result (the expansion) of \getrefnumber {#1}
(eg A.II.3.d
) in \templabel
. Again, the code is at the end if needed.
Workaround
\edef
didn't work, so I finally tried \protected@edef
(Are \protected\edef
and \protected@edef
the same?):
\protected@edef \templabel {\getrefnumber {#1}}
%\show \templabel->A.\protect \textlatin {I}.1.
I finally found this to remove \protect
:
\expandafter \expandafter \expandafter \def \expandafter \expandafter \expandafter \templabel \expandafter \expandafter \expandafter {\getrefnumber {#1}}
%\show \templabel ->A.\textlatin {I}.1.
I looked at the definition of \textlatin
and noticed that it really looks like \textbf
. It's a formatting command. To remove the \textlatin
command, I use the following:
\def \textlatin #1{#1}
Questions
Finally it works, but:
Can you explain briefly what's the problem here, with protection and expansion? Why can't I use
\getrefnumber {#1}
directly?I would like to know is there is a better solution, or more general, or at least, if the solution covers all cases here: how to get directly
A.I.1
instead ofA.\protect \textlatin {I}.1
?Does this solution with
\expandafter
works in each case? Which commands may I encounter here, in\getrefnumber {#1}
:\textlatin
,\textgreek
(because I use\greek
?) and what else?The way I deal with the \textlatin command is it the better one? Are there any drawbacks with this solution? (I don't understand the definition of
\textlatin
, which is like\textbf
)And if I use
-
instead of.
(A-II-3-4)? Does the-
will be considered as an arithmetic operation, eg during expansion?
Feel free give advice on code, I'm learning.
The context, just in case
I use the following numbering:
\def \thepart {\Alph {part}}
\def \thechapter {\Roman {chapter}}
\def \thesection {\arabic {section}}
\def \thesubsection {\alph {subsection}}
\def \thesubsubsection {\greek {\value{subsubsection}}}
\setcounter {secnumdepth} {3} % Paragraphs and subparagraphs unnumbered
I create “informative” numbering for the titles (Source: (Re)define \ref for chapter that includes the part number):
\def \p@chapter {\thepart.}
\def \p@section {\thepart.\thechapter.}
\def \p@subsection {\thepart.\thechapter.\thesection.}
\def \p@subsubsection {\thepart.\thechapter.\thesection.\thesubsection.}
Then I create my label command that I will patch to sectionning commands (Sources:
— Sectioning command that automatically creates label,
— Changing the output of \ref depending on the position of the corresponding \label,
— How to change the appearance of \ref depending on where it's called relative to the \label,
— Automatic creations of labels).
\newcommand {\myref }[1]{%
\@ifundefined {r@#1} {??} {\begingroup
%\edef \templabel {\getrefnumber {#1}} doesn't work
To get A.I.2.b
in \templabel
, this is the problem
Then I will split the string with xstrings:
\StrCut \templabel {.} \part@number \part@tail
\StrCut \part@tail {.} \chapter@number \chapter@tail
% …
\StrCut \paragraph@tail {.} \subparagraph@number \subparagraph@tail
and create the contextual link (the link, his number is eg “A-III-2-b The title here” but if the link is in A-III, it will be only “2-b The title here”; it's contextual):
\edef \templink {\getrefbykeydefault {#1} {anchor} {}}
\IfStrEq \thepart \part@number
{\IfStrEq \thechapter \chapter@number
%…
{\textcolor{red}{X} \hyperlink \templink \part@tail}}%
{\textcolor{red}{X} \hyperlink \templink \templabel}%
\endgroup}}
The MWE
\documentclass[greek]{book} % THE GREEK OPTION IS THE CAUSE
\usepackage {hyperref}
\usepackage {babel}
\makeatletter
\def \p@chapter {\thepart.}
\def \p@section {\thepart.\thechapter.}
\def \p@subsection {\thepart.\thechapter.\thesection.}
\def \p@subsubsection {\thepart.\thechapter.\thesection.\thesubsection.}
\newcommand {\myref}[1]{%
\@ifundefined {r@#1} {??} {\begingroup
\edef \temp {\getrefnumber {#1}}
% \show \temp->A.\textlatin {I}.1.
\endgroup }}
\makeatother
\begin{document}
\mainmatter
\part{Hi :\\introduction}\label{Hi}
\chapter{Chapter}\label{Chapter}
See \myref{Test} below.
\section{Test}\label{Test}
\enddocument
nameref
,cleveref
, .... – jon Nov 24 '16 at 5:30\documentclass
and end with\end{document}
. – Werner Nov 24 '16 at 5:39\getrefnumber {#1}
is a command fromnameref
, which I use. I read the documentation ofcleveref
, quickly, and others too, but didn't find anything. I prefer not to use too many packages, but if there is one… I'm still interested in the answer here. – user73438 Nov 24 '16 at 5:40