Since I use unnumbered sections, I'd like to have a variation of \autoref
that uses names instead of numbers in the case of sectioning, but keeps its usual behaviour for other environments. I've come up with this naive piece of code, but I don't get it to work.
\documentclass{article}
\usepackage{xstring, hyperref, nameref}
\setcounter{secnumdepth}{-1}
\makeatletter
\newcommand{\KSautoref}[1]{
\edef\KS@RefType{\expandafter\@fourthoffive\csname r@#1\endcsname}
\newif\ifKSSectioning\KSSectioningfalse
\IfBeginWith{\KS@RefType}{chapter}{\KSSectioningtrue}{}
\IfBeginWith{\KS@RefType}{section}{\KSSectioningtrue}{}
\IfBeginWith{\KS@RefType}{subsection}{\KSSectioningtrue}{}
\IfBeginWith{\KS@RefType}{subsubsection}{\KSSectioningtrue}{}
\IfBeginWith{\KS@RefType}{paragraph}{\KSSectioningtrue}{}
\IfBeginWith{\KS@RefType}{subparagraph}{\KSSectioningtrue}{}
\ifKSSectioning\chaptername\nameref{#1}\else\autoref{#1}\fi
}
\makeatother
\begin{document}
\section{Test} \label{test}
See \KSautoref{test}.
\end{document}
When I try to compile this, I get the following error:
! Argument of \@fourthoffive has an extra }.
<inserted text>
\par
l.19 See \KSautoref{test}
.
?
Using \tracingall
I get this additional information:
{\edef}
{\expandafter}
{\csname}
\@fourthoffive #1#2#3#4#5->#4
#1<-\r@test
How can I get \r@test
to expand as well, so that it's not used as just the first argument?
If there is a package that provides the functionality, I'd rather use that, but still I'd like to know the bit about expansion...
\chapername
? and besides with numbering off, starting with sections, hyperref will (I think) always usesection*.<number>
so\nameref
sayssection
for things which are not necessarily sections. It is pretty hopeless if things are not numbered to use\label/\ref
(or variants). – user4686 Nov 6 '13 at 15:21\chaptername
as a placeholder for a custom command, but since I didn't get this far with the above code, I never saw the error. Apart from that, I do intend to use this custom command, as\nameref
just prints the name without the type as\autoref
does. – pazathoth Nov 12 '13 at 14:54