引用某个部分有多种可能性\section{The First Section} \label{first}
:
\ref{first}
返回其数字(例如“1”)。\autoref{first}
返回其级别和编号(例如“第 1 部分”)。\nameref{first}
返回其名称(I“第一部分”)。
但是我如何才能只获取单词“部分”(或“章节”或“项目”——取决于目标的杠杆和类型)?也就是说,我想获得
在这个部分, ...
从
\section{The First Section}
\label{first}
In this \typeref{first}, ... % whatever the command is called
和
在这一章当中, ...
从
\chapter{The First Section}
\label{first}
In this \typeref{first}, ...
这就是我不能使用的原因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}
答案1
当 hyperref 包被加载时,autoref 功能可用。
利用自动引用功能,可以根据存储在交叉引用数据中的超链接目标名称推断出引用的类型/级别。
您可以复制该机制,并在副本中消除除了放置交叉引用标签时传递空格和引用计数器的名称之外的那些东西。\the⟨counter⟩
(从目标名称到文本中出现的短语的映射由这些\...autorefname
- 和\...refname
- 宏完成。)
在示例中,我使用\if
-switch 添加了一种机制,用于将文本短语的首字母设为大写字母(如果需要)。如果在\sectioninglevelref
移动参数中使用,这可能会产生意想不到的效果。
\nextsectioninglevelreffirstuppercase
仅在 之前应用\sectioninglevelref
。这种方式有相当大的机会在某些-command 可能干扰\sectioninglevelref
之前重置开关。\autoref
\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}
答案2
您可以使用 zref 来检索计数器:
\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}