\ref 和 \nameref 带有字母枚举项

\ref 和 \nameref 带有字母枚举项

有没有办法保留枚举列表(A)(二)等,并有参考文献提供完整的背景,如1.1.1(一)

在此处输入图片描述

章节和子章节引用按我想要的方式显示1.11.1.1并且名称引用也有效,“第一的”“和..”. 枚举项引用显示为二十八但我想1.1.1(ab)。并且枚举项名称引用是错误的(“还..”),尽管这也许是故意的。因此,枚举字母项引用有两个问题:它不包含其上下文;并且,它不遵守枚举字母显示特性。对于我的使用,引用的自定义命令很好,但希望该命令不需要子部分和项标签,例如,而不是编写冗长且容易出错的代码:

\myuglyitemref{subsec:first-and}{itm:faab}

是否有某种方法可以定义引用,以便我可以写:

\myawesomeitemref{itm:faab}

显示1.1.1(ab)

\documentclass{report}
\usepackage{alphalph}
\usepackage{enumitem}
\usepackage{nameref}
\makeatletter
\def\enumalphalphcnt#1{\expandafter\@enumalphalphcnt\csname c@#1\endcsname}
\def\@enumalphalphcnt#1{\alphalph{#1}}
\makeatother
\AddEnumerateCounter{\enumalphalphcnt}{\@enumalphalphcnt}{aa}
\renewcommand{\labelenumi}{(\enumalphalphcnt{enumi})}
% my (ugly) item reference
\newcommand{\myuglyitemref}[2]{\ref{#1}\labelenumi}
\begin{document}
\chapter{SOMETHING}The chapter.
\section{First}\label{sec:first}
Section items show with (ab) but referenced with 1.1(ab)
\begin{enumerate}\setcounter{enumi}{27}
\item {First 28} \label{itm:fab} 28th element
\end{enumerate}
\subsection{And...}\label{subsec:first-and}
Subsection items show with (a) but referenced with 1.1.1(a)
\begin{enumerate}\setcounter{enumi}{27}
\item {And 28} \label{itm:faab} 28th element
\end{enumerate}
Correct: \ref{sec:first} and \ref{subsec:first-and}
Correct: ``\nameref{sec:first}'' and ``\nameref{subsec:first-and}''
Incorrect: \ref{itm:fab} and \ref{itm:faab} - should be 1.1(ab) and 1.1.1(ab)
Incorrect: ``\nameref{itm:faab}''
Ugly: \myuglyitemref{subsec:first-and}{itm:faab}

答案1

我的回答仅涉及枚举项的交叉引用,其中交叉引用“数字”的出现取决于项是在节级还是子节级出现。(附言:如果帖子包含以下内容,则此网站“工作”效果最佳只有一个主要问题一次。这就是为什么我甚至没有尝试解决您提出的另一个主要问题,即如何\nameref在枚举项目的“标题”上使用。)

该解决方案通过修改低级 LaTeX 宏 、 和 来实现\theenumi\labelenumi观察修改宏的语句中条件\p@enumi的使用。\ifnum\p@enumi

在下面的例子中,hyperref包中加载了 merley 来突出显示排版文本的哪些部分是指令生成的实际交叉引用\ref

在此处输入图片描述

\documentclass{report}
\usepackage{alphalph}

\renewcommand\theenumi{(\alphalph{\value{enumi}})}
\renewcommand\labelenumi{\theenumi}
\makeatletter
\renewcommand\p@enumi{\ifnum\value{subsection}=0{\thesection}\else{\thesubsection}\fi}
\makeatother

\usepackage[colorlinks]{hyperref} %% just for this example

\begin{document}
\chapter{SOMETHING}
The chapter.

\section{First section}\label{sec:first}

Section-level enumerated items: Labels should show as ``(ab)'' and should be cross-referenced as ``1.1(ab)''.

\begin{enumerate}
\setcounter{enumi}{27}
\item 28th element \label{itm:fab} 
\end{enumerate}

\subsection{First subsection}\label{subsec:first-and}
Section-level enumerated items: Labels should show as ``(ba)'' and should be cross-referenced as ``1.1.1(ba)''.

\begin{enumerate}
\setcounter{enumi}{52}
\item 53rd element \label{itm:faab} 
\end{enumerate}

\subsection{Second subsection}

Correct: Section \ref{sec:first} and subsection \ref{subsec:first-and} \dots

\medskip\noindent
Now also correct: Items \ref{itm:fab} and \ref{itm:faab} \dots

\end{document}

答案2

这是一个部分解决方案。

一些日常事务:

代替

\renewcommand{\labelenumi}{(\enumalphalphcnt{enumi})}

\setlist[enumerate,1]{label=(\enumalphalphcnt*)}

或者

\setlist[enumerate,1]{label=(\enumalphalphcnt*), ref={\thesubsection(\enumalphalphcnt*)}}

请注意,你也可以说

\begin{enumerate}[start=27] 

\begin{enumerate}[resume]

但这还不正确,因为0当引用不在章节或小节内时,您不希望出现 s......

相关内容