我想创建一个名为假设的新部分类型标题。标题也必须出现在目录中。到目前为止,我已经设法创建了标题,但我找不到让它出现在目录中的方法。我希望它像目录中的部分一样出现:章节号.标题计数器号,例如:1.2 假设标题 1 另外,我创建了一个新的标签格式,并希望章节号也出现。请参阅附件 MWE 并请提供建议:
\documentclass[a4paper,12pt]{report}
\usepackage[english]{babel}
\newcounter{assumption}[section]
\newenvironment{assumption}[1][\unskip]{\large\refstepcounter{assumption}\medskip
\noindent{\textbf{Assumption~\thechapter.\theassumption. {\large}#1} \\} \addcontentsline{toc}{assumption} \rmfamily}{\medskip}
\newcommand{\assumptionlabel}[2]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{}}}
\hypertarget{#1}{#2}}
\begin{document}
\tableofcontents
\chapter{Assumptions Chapter}
\assumption[Assumption Header]
\label{assumptionlabel:some-assumption}
Some assumption text.
\assumption[Assumption Header]
\label{assumptionlabel:some-assumption2}
Some assumption text with reference to Assumption \ref{assumptionlabel:some-assumption}.\\
Instead, I would like:\\
Some assumption text with reference to Assumption 1.1
\end{document}
答案1
线路
\addcontentsline{toc}{assumption}
不起作用,因为assumption
没有已知的ToC
条目类型section
等,即还没有\l@assumption
宏,但这可以通过以下方式解决
\makeatletter
\let\l@assumption\l@section
\makeatother
以便使用相关的\l@section
代替。
现在
\addcontentsline{toc}{assumption}{\protect\numberline{\theassumption}#1}
将在目录中提供相应级别的假设编号和标题section
。
现在讨论标签问题:重新定义\@currentlabel
以根据需要制作交叉引用格式,无需明确的\hypertarget
- 语句 -hyperref
足够复杂,可以在有正确标签的情况下“保证”正确的超级锚点。
\documentclass[a4paper,12pt]{report}
\usepackage[english]{babel}
\usepackage{hyperref}
\newcommand{\assumptionname}{Assumption}
\newcounter{assumption}[section]
\makeatletter
\let\toclevel@assumption\toclevel@section% Providing bookmark support
\let\l@assumption\l@section
\newenvironment{assumption}[1][\unskip]{%
\large\refstepcounter{assumption}%
% Specific label format
\protected@edef\@currentlabel{\csname p@assumption\endcsname\assumptionname\ \thechapter.\csname theassumption\endcsname}%
\medskip%
\noindent\textbf{\assumptionname~\thechapter.\theassumption.\ \large #1}
\addcontentsline{toc}{assumption}{\protect\numberline{\thechapter.\theassumption}#1}%
\medskip% % Empty line here!!!
\parindent0em
\rmfamily%
}{\medskip}%
\makeatother
\begin{document}
\tableofcontents
\chapter{Assumptions Chapter}
\begin{assumption}[Assumption Header]
\label{assumptionlabel:some-assumption}%
Some assumption text. \ref{assumptionlabel:some-assumption2}
\end{assumption}
\clearpage
\begin{assumption}[Assumption Header]
\label{assumptionlabel:some-assumption2}
Some assumption text with reference to \ref{assumptionlabel:some-assumption}.
\end{assumption}
\end{document}