我想使用一些自动交叉引用(我hyperref
在我定制的整个文档中使用)来写类似这样的内容:
We see in \autoref{partname} that
打印 :
We see in part I that
问题是我重新定义了 TOC 的部分名称,以下是我的问题的 MWE:
\documentclass[12pt,a4paper]{book}
% Ecrire en français
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{xspace} % Pour les espaces automatiques
% Part name in TOC
\usepackage{tocloft}
\renewcommand\cftpartpresnum{\hfill}
\renewcommand\cftpartleader{\hfill}
\renewcommand{\thepart}{Partie \Roman{part} --\hspace{-.4cm}}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\part{First Part}\label{first}
\chapter{Intro}
We will deal with \autoref{first}
We will deal with \ref{first}
When I add ponctuation : \autoref{first}. It is not beautiful.
\end{document}
给我 :
我只想将大写字母 P 改为非大写字母,取消破折号,然后使用后面的空格。由于我可以创建命令,所以我需要回答这个精确的问题:
例如,我如何获取零件编号
I
?我尝试使用,\getrefnumber
但结果显示“Partie I -”
我是否被这个问题困扰了?或者有没有办法同时获得目录和自动参考系统?
提前致谢。
答案1
不要\hspace
在\the...
命令中使用——它会破坏引用(格式)
\documentclass[12pt,a4paper]{book}
% Ecrire en français
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{xspace} % Pour les espaces automatiques
% Part name in TOC
\usepackage{tocloft}
\renewcommand\cftpartpresnum{\hfill}
\renewcommand\cftpartleader{\hfill}
\renewcommand{\thepart}{Partie \Roman{part}}% --\hspace{-0.4cm}}
\usepackage{hyperref}
\begin{document}
\part{First Part}\label{first}
\chapter{Intro}
We will deal with \autoref{first}
We will deal with \ref{first}
When I add ponctuation : \autoref{first}. It is beautiful!
When I add ponctuation : \autoref{second}. It is not beautiful.
% Now bad:
\renewcommand{\thepart}{Partie \Roman{part} --\hspace{-0.4cm}}
\part{Second Part}\label{second}
Foo
\end{document}
更新——解决--
问题!
\documentclass[12pt,a4paper]{book}
% Ecrire en français
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage{xspace} % Pour les espaces automatiques
\usepackage{xpatch}
\newcommand{\partnamedivider}{\textendash}
\makeatletter
% Must be done here, before tocloft 'screws' up the definition of \@part
\xpatchcmd{\@part}{%
\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
\addcontentsline{toc}{part}{\thepart\ \partnamedivider\ #1}
}{\typeout{Success!!!!!}}{\typeout{Failed!!!!!}}
\makeatother
% Part name in TOC
\usepackage{tocloft}
\renewcommand\cftpartpresnum{\hfill}
\renewcommand\cftpartleader{\hfill}
\renewcommand{\thepart}{Partie \Roman{part}}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\part{First Part}\label{first}
\chapter{Intro}
We will deal with \autoref{first}
We will deal with \ref{first}
When I add ponctuation : \autoref{first}. It is beautiful!
When I add ponctuation : \autoref{second}. It is not beautiful.
% Now bad:
\renewcommand{\thepart}{Partie \Roman{part} --\hspace{-0.4cm}}
\part{Second Part}\label{second}
Foo
\end{document}
答案2
你应该使用cleveref
,它更强大。我使用了我的其他答案
\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[titles]{tocloft}
\usepackage{xpatch}
%%% patches to \@part and \@chapter must
%%% be done before loading hyperref
\makeatletter
\patchcmd{\@part}
{\thepart\hspace{1em}}
{\protect\partnumberline{\thepart}}
{}{}
\patchcmd{\@chapter}
{\numberline}
{\chapternumberline}
{}{}
\makeatother
\usepackage{hyperref}
\usepackage[french,capitalize,nameinlink]{cleveref}
\newcommand\partnumberline[1]{\textsc{Partie} #1 -- }
\newcommand\chapternumberline[1]{\textsc{\chaptername} #1 -- }
\renewcommand\cftpartafterpnum{\par\nobreak\bigskip}
\renewcommand\cftdotsep{2}
\renewcommand{\cftpartleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
\begin{document}
\tableofcontents
\part{First Part}\label{first}
\chapter{Intro}
We will deal with \cref{first}
We will deal with \ref{first}
When I add ponctuation : \cref{first}. It is not beautiful.
\end{document}
答案3
我找到了一种方法:
\hyperref[<part label>]{Partie \Roman{part}}
正在执行该任务,那么我只需为其重新定义一个新命令即可。是的,这并不优雅,因为\autoref
文本和\MyCommand
部分中都会有。
如果还有更好的,请告诉我:)