1

There are various possibilities to reference a section \section{The First Section} \label{first}:

  • \ref{first} returns its number (e.g. "1").
  • \autoref{first} returns its level and number (e.g. "section 1").
  • \nameref{first} returns its name (I"The First Section").

But how do I get only the word "section" (or "chapter", or "item" – depending on the target's lever and type)? That is, I'd like to get

In this section, ...

from

\section{The First Section}
\label{first}
In this \typeref{first}, ... % whatever the command is called

and

In this chapter, ...

from

\chapter{The First Section}
\label{first}
In this \typeref{first}, ...

This is the reason I cannot use cleveref:

\documentclass[]{scrreprt}

\usepackage{scrjura}
\usepackage{cleveref}

\begin{document}
    \begin{contract}
        \Clause{title={First}, preskip=0em, postskip=0em}\label{first}
        
        \Clause{title={Second}, preskip=0em, postskip=0em}
        \ref{first}
    \end{contract}
\end{document}
4
  • 3
    Use the command \namecref from the cleveref package.
    – Gaussler
    Jul 7, 2021 at 10:57
  • Ah, in my case I had to drop cleveref because it's incompatible with some other package. Jul 7, 2021 at 11:01
  • Are you sure that it’s incompatible? Usually, the problem with cleveref is that you have to load it after all other reference packages, including hyperref and varioref. If these packages are loaded in the right order, everything usually works out fine.
    – Gaussler
    Jul 7, 2021 at 11:22
  • I've added an example with scrjura which doesn't work with cleveref Jul 7, 2021 at 12:31

2 Answers 2

2

When the hyperref package is loaded, the autoref-feature is available.

With the autoref-feature the type/level of reference is concluded from the name of the destination for hyperlinks that is stored in the cross-referencing-data.

You can copy that mechanism and in the copy eliminate those things that besides the name of the sectioning-level deliver spaces and \the⟨counter⟩ of the referenced counter at the time of placing the cross-referencing-label.

(The mapping from the name of the destination to the phrase that occurs in the text is done by these \...autorefname- and \...refname-macros.)

In the example I added a mechanism using an \if-switch for having the first letter of the textual phrase as a capital letter if this is desired. This might yield unexpected effects in case \sectioninglevelref is used within moving arguments.

Apply \nextsectioninglevelreffirstuppercase only right before \sectioninglevelref. This way \sectioninglevelref has a fair chance to reset the switch before some \autoref-command might interfere.

\documentclass[]{scrreprt}

\usepackage{scrjura}
\usepackage{hyperref}

\makeatletter
\DeclareRobustCommand*{\sectioninglevelref}{%
  \leavevmode
  \@ifstar{\HyRef@sectioninglevelref\@gobbletwo}{\HyRef@sectioninglevelref\hyper@@link}%
}
\newcommand*\HyRef@sectioninglevelref[2]{%
  \begingroup
  \Hy@safe@activestrue
  \expandafter\HyRef@sectioninglevelsetref\csname r@#2\endcsname{#2}{#1}%
  \endgroup
}
\newcommand*\HyRef@sectioninglevelsetref[3]{% link command, csname, refname
  \HyRef@ShowKeysRef{#2}%
  \ifcase 0\ifx#1\relax 1\fi\ifx#1\Hy@varioref@undefined 1\fi\relax
    \edef\HyRef@thisref{%
      \expandafter\@fourthoffive#1\@empty\@empty\@empty
    }%
    \expandafter\HyRef@testsectioninglevelreftype\HyRef@thisref.\\%
    \Hy@safe@activesfalse
    #3{%
      \expandafter\@fifthoffive#1\@empty\@empty\@empty
    }{%
      \expandafter\@fourthoffive#1\@empty\@empty\@empty
    }{%
      \HyRef@currentHtag
      %%%%% \expandafter\@firstoffive#1\@empty\@empty\@empty
      \null
    }%
  \else
    \protect\G@refundefinedtrue
    \nfss@text{\reset@font\bfseries ??}%
    \@latex@warning{%
      Reference `#2' on page \thepage\space undefined%
    }%
  \fi
  \ifnextsectioninglevelreffirstuppercase\global\nextsectioninglevelreffirstuppercasefalse\fi
}%
\@ifdefinable\HyRef@testsectioninglevelreftype{%
  \def\HyRef@testsectioninglevelreftype#1.#2\\{%
    \ltx@IfUndefined{#1autorefname}{%
      \ltx@IfUndefined{#1name}{%
        \HyRef@StripStar#1\\*\\\@nil{#1}%
        \ltx@IfUndefined{\HyRef@name autorefname}{%
        \ltx@IfUndefined{\HyRef@name name}{%
          \def\HyRef@currentHtag{}%
          \Hy@Warning{No autoref name for `#1'}%
        }{%
          \edef\HyRef@currentHtag{%
            \expandafter\noexpand\csname\HyRef@name name\endcsname
            %%%%%\noexpand~%
          }%
        }%
      }{%
        \edef\HyRef@currentHtag{%
          \expandafter\noexpand
          \csname\HyRef@name autorefname\endcsname
            %%%%%\noexpand~%
          }%
        }%
      }{%
        \edef\HyRef@currentHtag{%
          \expandafter\noexpand\csname#1name\endcsname
          %%%%%\noexpand~%
        }%
      }%
    }{%
      \edef\HyRef@currentHtag{%
        \expandafter\noexpand\csname#1autorefname\endcsname
        %%%%%\noexpand~%
      }%
    }%
  }%
}%
\newif\ifnextsectioninglevelreffirstuppercase
\global\nextsectioninglevelreffirstuppercasefalse
\newcommand\nextsectioninglevelreffirstuppercase{\global\nextsectioninglevelreffirstuppercasetrue}%
\makeatother

\makeatletter 
\newcommand\@AbsClauseautorefname{\ifnextsectioninglevelreffirstuppercase P\else p\fi aragraph}%
%\newcommand\@AbsClausename{\ifnextsectioninglevelreffirstuppercase P\else p\fi aragraph}%
\makeatother

\begin{document}
    \begin{contract}
        \Clause{title={First}, preskip=0em, postskip=0em}\label{first}
        
        \Clause{title={Second}, preskip=0em, postskip=0em}

        \noindent \verb|\ref{first}|: \ref{first}

        \noindent \verb|\autoref{first}|: \autoref{first}

        \noindent \verb|\sectioninglevelref{first}|: \sectioninglevelref{first}

        \noindent \verb|\sectioninglevelref*{first}|: \sectioninglevelref*{first}

        \noindent \verb|\hyperref[first]{the first \sectioninglevelref*{first}}|:
        \hyperref[first]{the first \sectioninglevelref*{first}}
        
        \noindent 
        \verb|\nextsectioninglevelreffirstuppercase|\\
        \phantom{(6) }\verb|\hyperref[first]{the first \sectioninglevelref*{first}}|:
        \nextsectioninglevelreffirstuppercase  
        \hyperref[first]{the first \sectioninglevelref*{first}}

        \noindent \verb|\hyperref[first]{the first \sectioninglevelref*{first}}|:
        \hyperref[first]{the first \sectioninglevelref*{first}}

    \end{contract}
\end{document}

enter image description here

2

You could use zref to retrieve the counter:

\documentclass[]{scrreprt}

\usepackage{hyperref}
\usepackage{zref-counter,zref-user}
\makeatletter
\newcommand\mynameref[1]{\csname \zref@extractdefault{#1}{counter}{XX}autorefname\endcsname}
\newcommand\enumiautorefname{Enumerate item}
\makeatother
\begin{document}
\chapter{Chap}
\section{abc}\zlabel{sec}

\begin{enumerate}
\item \zlabel{item}
\end{enumerate}

\mynameref{sec}
\mynameref{item}
\end{document}

enter image description here

You must log in to answer this question.

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