我正在使用scrbook
以及enumitem
进行练习和cref
交叉引用。在我的样式文件中,我设置了以下新列表
% Rename enumerate from item to exercise
\newlist{exercise}{enumerate}{75}
\setlist[exercise]{label*=\arabic*.}
% adjust cref for label names
\crefname{exercisei}{exercise}{exercise}
\Crefname{exercisei}{Exercise}{exercise}
在我的主文档中,我使用\input
章节。我有一个问题和一个疑问。问题是引用嵌套的exercises
。请考虑以下示例
\begin{exercise}
\item
question 1
\begin{exercise}[label = (\alph*), ref=\theenumi{} (\alph*)]
\item
\label{ch21a}
sub question a
\item
From \cref{ch21a}, we have ...
\end{exercise}
\end{exercise}
它不会说出练习 1 (a) 或 (b) 或当时的任何字母,而是产生
但非嵌套可以exercise
正常工作。我猜我在定义时遗漏了某些内容,newlist
但现在我确定那是什么。
这是我的问题。我的问题是,如果我跨章节交叉引用,我如何让引用显示为exercise 10, chapter 1
?也就是说,如果我在第 2 章练习 10 中引用,我希望读者知道链接将带他们到第一章。目前,如果我在另一章中引用,它只会显示为exercise 10
。这能实现吗?
梅威瑟:
\documentclass{scrbook}
\usepackage{enumitem}
\usepackage[pdfencoding = auto, psdextra, bookmarksdepth = 4]{hyperref}
\usepackage[noabbrev]{cleveref}
% Rename enumerate from item to exercise
\newlist{exercise}{enumerate}{75}
\setlist[exercise]{label*=\arabic*.}
% adjust cref for label names
\crefname{exercisei}{exercise}{exercise}
\Crefname{exercisei}{Exercise}{exercise}
\begin{document}
\chapter{one}
\begin{exercise}
\item
q1
\begin{exercise}[label = (\alph*), ref = \theenumi{} (\alph*)]
\item
\label{ch11a}
\item
\Cref{ch11a}
\end{exercise}
\end{exercise}
\chapter{two}
\begin{exercise}
\item
\Cref{ch11a}
\end{exercise}
\end{document}
答案1
使用你的代码你会收到类似这样的警告
LaTeX Warning: cref reference format for label type `exerciseii'
undefined on input line 24.
因此您需要提供定义exerciseii
;此外,您需要在前面添加exercisei
计数器以便交叉引用,而不是enumi
;类似
ref=\arabic{exercisei}(\alph*)
完整示例:
\documentclass{scrbook}
\usepackage{enumitem}
\usepackage[pdfencoding = auto, psdextra, bookmarksdepth = 4]{hyperref}
\usepackage[noabbrev]{cleveref}
% Rename enumerate from item to exercise
\newlist{exercise}{enumerate}{75}
\setlist[exercise]{label*=\arabic*.}
% adjust cref for label names
\crefname{exercisei}{exercise}{exercise}
\Crefname{exercisei}{Exercise}{exercise}
\crefname{exerciseii}{exercise}{exercise}
\Crefname{exerciseii}{Exercise}{exercise}
\begin{document}
\chapter{Test chapter}
\begin{exercise}
\item question 1
\begin{exercise}[label = (\alph*), ref=\arabic{exercisei}(\alph*)]
\item
\label{ch21a}
sub question a
\item
From \cref{ch21a}, we have ...
\end{exercise}
\end{exercise}
\end{document}
结果:
要将章节编号添加到交叉引用中,请标记章节并使用这些标签;例如
\chapter{Test chapter one}
\label{cha:testa}
\begin{exercise}
\item question 1
\begin{exercise}[label = (\alph*), ref=\arabic{exercisei}(\alph*)]
\item
\label{ch21a}
sub question a
\item
sub question b
\end{exercise}
\end{exercise}
\chapter{Test chapter one}
\label{cha:testb}
From \cref{ch21a}~\cref{cha:testa}, we have ...
您可以为此定义一个命令:
\newcommand\chapref[#2]{\cref{#1}, \cref{#2},}
进而
\chapref{ch21a}{cha:testa}