如何在 titletoc 中格式化 \contentslabel?

如何在 titletoc 中格式化 \contentslabel?

因此在制作目录时,\contentslabel会提供章节编号,并使用\thecontentslabel默认格式。问题是这个编号是阿拉伯数字,而我想使用罗马数字。Titletoc 给出了\contentslabel[<format>]重新格式化的方法,但它没有告诉我想要的格式。有什么方法可以将其重新格式化为罗马数字吗?

我的代码是:

\documentclass[oneside,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{titlesec}
\usepackage{titletoc}

\titleformat{\chapter}[display]{\bfseries\upshape\filcenter}{CHAPTER \ \Roman{chapter}}{1ex}{}
\titlespacing*{\chapter} {0pt}{-10pt}{40pt}

\titlecontents{chapter}[0pt]{\bfseries}%above code
{\MakeUppercase{\chaptername} \thecontentslabel \ }%numbered chapter%
{}%numberless chapter%
{\mdseries\titlerule*[0.75em]{.}\bfseries\thecontentspage}%for dots

\chapter{lorem}
\chapter{ipsum}

如您所见,我用罗马字体格式化了我的章节,但\thecontentslabel仍然给出阿拉伯语。我想要的格式:

CHAPTER I LOREM
CHAPTER II IPSUM

默认输出:

CHAPTER 1 LOREM
CHAPTER 2 IPSUM

我尝试过的一些格式(全部失败):

\titlecontents{chapter}[0pt]{\bfseries}%above code
{\MakeUppercase{\chaptername} \contentslabel[\Roman{contentslabel}] \ }%numbered chapter%
{}%numberless chapter%
{\mdseries\titlerule*[0.75em]{.}\bfseries\thecontentspage}%for dots

\titlecontents{chapter}[0pt]{\bfseries}%above code
{\MakeUppercase{\chaptername} \contentslabel[\Roman{\contentslabel}] \ }%numbered chapter%
{}%numberless chapter%
{\mdseries\titlerule*[0.75em]{.}\bfseries\thecontentspage}%for dots

\titlecontents{chapter}[0pt]{\bfseries}%above code
{\MakeUppercase{\chaptername} \Roman{\thecontentslabel} \ }%numbered chapter%
{}%numberless chapter%
{\mdseries\titlerule*[0.75em]{.}\bfseries\thecontentspage}%for dots

\titlecontents{chapter}[0pt]{\bfseries}%above code
{\MakeUppercase{\chaptername} \Roman{thecontentslabel} \ }%numbered chapter%
{}%numberless chapter%
{\mdseries\titlerule*[0.75em]{.}\bfseries\thecontentspage}%for dots

答案1

以下是基于 TeX 原语的解决方案\romannumeral

\documentclass[oneside,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{titlesec}
\usepackage{titletoc}

\titleformat{\chapter}[display]{\bfseries\upshape\filcenter}{CHAPTER \ \Roman{chapter}}{1ex}{}
\titlespacing*{\chapter} {0pt}{-10pt}{40pt}

\titlecontents{chapter}[0pt]{\bfseries}%above code
{\MakeUppercase{\chaptername\ \romannumeral \thecontentslabel}\enspace}%numbered chapter%
{}%numberless chapter%
{\mdseries\titlerule*[0.75em]{.}\bfseries\thecontentspage}%for dots

\begin{document}

\tableofcontents
\chapter{lorem}
\chapter{ipsum}%

\end{document} 

在此处输入图片描述

相关内容