ToC 中的罗马数字:\settowidth 中的错误

ToC 中的罗马数字:\settowidth 中的错误

我有一份文档,我试图将目录中章节的阿拉伯数字替换为罗马数字。即

代替

Chapter 1  First chapter
Chapter 2  Second Chapter

我想了

Chapter I:  First chapter
Chapter II: Second Chapter

我尝试了以下方法来获得这个

\documentclass[12pt,a4paper]{report} 
\usepackage[titles]{tocloft}
%=================Content=======================
\newlength\mylength 
\newcommand*{\Romannumeral}[1]{\uppercase\expandafter{\romannumeral#1}}
\renewcommand\cftchappresnum{\chaptername~\Romannumeral} 
\renewcommand\cftchapaftersnum{:} 
\settowidth{\mylength}{\cftchappresnum\cftchapaftersnum\space} 
\addtolength\cftchapnumwidth{\mylength} 
%=============Set Chapter in Page==============
\usepackage{titlesec} 
\titleformat{\chapter}[display]{\normalfont\Large\bfseries\centering}{\chaptername\space\Roman{chapter}}{20pt}{\uppercase}
\begin{document}
\tableofcontents
\chapter{First}
first chapter
\section{first section}
first section
\section{second section} 
second section
\subsection{first}
\chapter{Second}
second chapter
\section{first section}  
first section
 \end{document} 

当我编译这个时出现以下错误

    ! Missing number, treated as zero.
<to be read again> 
                   :
l.8 ...th}{\cftchappresnum\cftchapaftersnum\space}

A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

当我手动将宽度设置为

\renewcommand\cftchapnumwidth{6em}

它编译并正确显示输出,但由于章节名称的大小变化,它看起来不太好。那么,我该怎么做呢?

答案1

\renewcommand\cftchappresnum{\chaptername~\Romannumeral}

是错误的,因为\Romannumeral看到的\numberline{1}是等语句,而不是章节编号本身。

\renewcommand{\thechapter}{\Roman{chapter}}如果定义了,整个事情就变得简单多了。

\documentclass[12pt,a4paper]{report} 
\usepackage[titles]{tocloft}
%=================Content=======================
\newlength\mylength 
\newcommand*{\Romannumeral}[1]{\uppercase\expandafter{\romannumeral#1}}
\renewcommand\cftchappresnum{\chaptername~} 
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand\cftchapaftersnum{:} 
\settowidth{\mylength}{\cftchappresnum\cftchapaftersnum\space} 
\addtolength\cftchapnumwidth{\mylength} 
%=============Set Chapter in Page==============
\usepackage{titlesec} 

\titleformat{\chapter}[display]{\normalfont\Large\bfseries\centering}{\chaptername\space\Roman{chapter}}{20pt}{\uppercase}
\begin{document}
\tableofcontents
\chapter{First}
first chapter
\section{first section}
first section
\section{second section} 
second section
\subsection{first}
\chapter{Second}
second chapter
\section{first section}  
first section
 \end{document} 

相关内容