我想做与此回答的问题中相同的事情:
给出以下要添加的代码:
\usepackage{chngcntr}
\counterwithin{chapter}{part}
\makeatletter
\renewcommand{\thechapter}{\@arabic\c@chapter}
\renewcommand{\p@chapter}{\thepart.}
\makeatother
但有一点不同:
是否可以添加部分“部分”后交叉引用里面的章节“部分”?
我的意思是可以设置后缀吗?
结果应为:
Comparison between \autoref{part_1_chapter_1} and \autoref{part_2_chapter_1}
应该看起来像:
第一部分第 1 章与第二部分第 1 章的比较
更新:
我希望能够引用任何命令,如章节、部分、小节、小子节,并具有 autoref 的正常输出,之后应该添加来自此引用的部分。
例如:
第 1 章/第 1 部分或第 2 节/第 1 部分
答案1
这里有一个方法(类似于 Mico 的答案),使用而不是 来zref
存储零件信息。\zlabel
\label
该解决方案存储锚点和所引用计数器的类型。它检索计数器名称,将名称更改为首字母大写,并将此新名称与零件信息一起输入。
如果使用了零件计数器或没有使用 进行引用zlabel
,则\pref
命令将恢复为\Cref
。
输出的精确格式可以在 内配置\partreferenceformat
。
\documentclass{report}
\usepackage{etoolbox}
\usepackage[user,counter,hyperref]{zref}
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \str_head:n {x,o,V}
\cs_generate_variant:Nn \str_tail:n {x,o,V}
\cs_generate_variant:Nn \str_uppercase:n {x,o,V}
\cs_generate_variant:Nn \str_set:Nn {Nx}
\newcommand{\FirstUpcase}[1]{%
\str_set:Nx \l_tmpa_str {#1}%
\str_uppercase:x {\str_head:N \l_tmpa_str}\str_tail:V {\l_tmpa_str}%
}
\ExplSyntaxOff
\makeatletter
\newif\if@hyperrefloaded
\AtEndPreamble{%
\@ifpackageloaded{hyperref}{%
\global\@hyperrefloadedtrue
}{
\global\@hyperrefloadedfalse
}
}
\zref@newprop{partinfo}{\thepart}
\zref@addprop{main}{partinfo}
\newcommand{\partreferenceformat}[1]{%
\FirstUpcase{\zref@extract{#1}{counter}}\ \zref@extract{#1}{default}/Part \zref@extract{#1}{partinfo}%
}
\newcommand{\pref}[1]{%
\begingroup
\edef\temp@@a{part}%
\zref@ifrefundefined{#1}{%
\Cref{#1}%
}{%
% Checking whether we have a reference to the part counter -> use the usual \Cref for this
\edef\temp@@b{\zref@extract{#1}{counter}}%
\ifx\temp@@b\temp@@a
\Cref{#1}%
\else
\if@hyperrefloaded
\hyperlink{\zref@extract{#1}{anchor}}{\partreferenceformat{#1}}%
\else
\partreferenceformat{#1}%
\fi
\fi
}%
\endgroup
}
\makeatother
\usepackage{blindtext}
\begin{document}
\tableofcontents
\part{AAA}
\chapter{Uno} \zlabel{chap:uno}
\section{Foo section} \zlabel{sec:foo}
\part{BBB} \label{part:bbb}
\chapter{Due} \zlabel{chap:due}
\blindtext[5]
\clearpage
A comparison between \pref{chap:uno} and \pref{chap:due} or \pref{sec:foo} \dots
On the other hand \pref{part:bbb} produces something different!
\end{document}
旧解决方案
该命令\chappartref
检索标签并检查它是否是属于章节计数器的标签,否则它将返回\ref
。
\documentclass{report}
\usepackage[user,counter,hyperref]{zref}
\usepackage[colorlinks]{hyperref}
\makeatletter
\zref@newprop{partinfo}{\thepart}
\zref@addprop{main}{partinfo}
\newcommand{\chappartref}[1]{%
\begingroup
\edef\temp@@a{chapter}%
\zref@ifrefundefined{#1}{%
\ref{#1}%
}{%
\edef\temp@@b{\zref@extract{#1}{counter}}%
\ifx\temp@@b\temp@@a
\hyperlink{\zref@extract{#1}{anchor}}{Chapter \zref@extract{#1}{default}/Part \zref@extract{#1}{partinfo}}%%
\else
\ref{#1}%
\fi
}%
\endgroup
}
\makeatother
\begin{document}
\part{AAA}
\chapter{Uno} \zlabel{chap:uno}
\part{BBB} \label{part:bbb}
\chapter{Due} \zlabel{chap:due}
\clearpage
A comparison between \chappartref{chap:uno} and \chappartref{chap:due} \dots
On the other hand \chappartref{part:bbb} produces something different!
\end{document}
下次更新部分柜台有德语翻译
\documentclass{report}
\usepackage{etoolbox}
\usepackage{amsmath}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{letltxmacro}
\usepackage[user,counter,hyperref]{zref}
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink,ngerman]{cleveref}
\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g_zaehlernamen_deutsch_prop
\cs_generate_variant:Nn \str_head:n {x,o,V}
\cs_generate_variant:Nn \str_tail:n {x,o,V}
\cs_generate_variant:Nn \str_uppercase:n {x,o,V}
\cs_generate_variant:Nn \str_set:Nn {Nx}
\cs_generate_variant:Nn \prop_item:Nn {Nx}
\cs_generate_variant:Nn \prop_gput:Nnn {Nxx}
\newcommand{\FirstUpcase}[1]{%
\str_set:Nx \l_tmpa_str {#1}%
\str_uppercase:x {\str_head:N \l_tmpa_str}\str_tail:V {\l_tmpa_str}%
}
\NewDocumentCommand{\StoreCounterNameMap}{+m}{%
\seq_set_from_clist:Nn \l_tmpa_seq {#1}
\seq_map_inline:Nn \l_tmpa_seq {
\seq_set_split:Nnn \l_tmpb_seq {/} {##1}
\prop_gput:Nxx \g_zaehlernamen_deutsch_prop { \seq_item:Nn \l_tmpb_seq {1}} { \seq_item:Nn \l_tmpb_seq {2}}
}
}
\cs_new:Npn \GetNameFromCounter #1{%
\prop_item:Nx \g_zaehlernamen_deutsch_prop {#1}
}
\ExplSyntaxOff
\makeatletter
\newif\if@hyperrefloaded
\AtEndPreamble{%
\@ifpackageloaded{hyperref}{%
\global\@hyperrefloadedtrue
}{
\global\@hyperrefloadedfalse
}
}
\zref@newprop{partinfo}{\thepart}
\zref@newprop{equationinfo}{\theequation}
\zref@addprops{main}{partinfo,equationinfo}
\newcommand{\partreferenceformat}[1]{%
\GetNameFromCounter{\zref@extract{#1}{counter}}\ \zref@extract{#1}{default}/\GetNameFromCounter{part} \zref@extract{#1}{partinfo}%
}
\newcommand{\pref}[1]{%
\begingroup
\edef\temp@@a{part}%
\zref@ifrefundefined{#1}{%
\Cref{#1}%
}{%
\edef\temp@@b{\zref@extract{#1}{counter}}%
\ifx\temp@@b\temp@@a
\Cref{#1}%
\else
\if@hyperrefloaded
\hyperlink{\zref@extract{#1}{anchor}}{\partreferenceformat{#1}}%
\else
\partreferenceformat{#1}%
\fi
\fi
}%
\endgroup
}
%Special command for zequationlabel
\newcommand{\zequationlabel}[1]{%
\label{#1}%
\IfRefUndefinedExpandable{#1}{}{%
\zref@setcurrent{anchor}{\getrefbykeydefault{#1}{anchor}\relax}%
}%
\zref@setcurrent{counter}{equation}%
\zref@setcurrent{default}{\getrefnumber{#1}}%
\zlabel{#1}%
}
\makeatother
\usepackage{blindtext}
\StoreCounterNameMap{
part/Teil,
chapter/Kapitel,
section/Abschnitt,
subsection/Unterabschnitt,
subsubsection/Unterunterabschnitt, % ;-)
paragraph/Paragraf,
subparagraph/Kleiner Paragraf,
figure/Abbildung,
table/Tabelle,
equation/Gleichung
}
\begin{document}
\tableofcontents
\part{AAA}
\chapter{Uno} \zlabel{chap:uno}
\begin{align}
E&=mc^{2} \zequationlabel{Einstein} \\%
E^{2} &= m^{2} c^{4} + p^{2} c^{2} \zequationlabel{Einstein2}
\end{align}
\section{Foo section} \zlabel{sec:foo}
\part{BBB} \label{part:bbb}
\chapter{Due} \zlabel{chap:due}
\blindtext[5]
\begin{align}
E&=mc^{2} \zequationlabel{Einstein3}%
\end{align}
\getrefbykeydefault{Einstein}{anchor}
\clearpage
Hier ist ein kleiner Vergleich zwischen \pref{chap:uno} und \pref{chap:due}, oder zum Beispiel \pref{sec:foo}, bzw. \pref{Einstein} bzw. \pref{Einstein2}\dots
Wird nicht \verb!\zlabel! verwendet, dann liefert \verb!\pref! ganz einfach den cleveref-Namen, z.B: \pref{part:bbb}.
Und weil es so schön ist: \pref{Einstein3}
\end{document}
答案2
像这样吗?
请注意,该\mychref
宏接受两个参数:这里,它们是部分和章节的标签,但实际上它们可以是任何东西。
\documentclass{report} % use a document class that provides "\chapter" macro
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
% Custom macro for composite cross-references
% Input ordering: part label, chapter label
% Output ordering: "Chapter" <number> "slash" "Part" <number>
\newcommand\mychref[2]{\Cref{#2}/\Cref{#1}}
\begin{document}
\part{AAA} \label{part:aaa}
\chapter{Uno} \label{chap:uno}
\part{BBB} \label{part:bbb}
\chapter{Due} \label{chap:due}
\clearpage
A comparison between \mychref{part:aaa}{chap:uno} and \mychref{part:bbb}{chap:due} \dots
\end{document}