3

I am using the tocloft package to change the style of my ToC. I would like to typeset the chapter numbers as roman numerals. However, for the ToC I would to typeset them using the romanbars package.

This package provides a command which takes its input (a roman numeral) and formats it with horizontal bars on the base- and top-line which I find pleasing to the eye. The command takes a single argument. However, the tocloft command only provides commands to change the formatting of the (chapter) number using non-argument command (like \bfseries).

My MWE is:

\documentclass{scrbook}

\renewcommand{\thechapter}{\Roman{chapter}}

\usepackage[usedvipsnames]{xcolor}
\usepackage{tocloft}
\usepackage{romanbar}

\renewcommand{\cftchappresnum}{\color{red}\Romanbar}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\chapter{First Chapter}
\lipsum[2]

\chapter{Second Chapter}
\lipsum[2]

\chapter{Third Chapter}
\lipsum[2]

\end{document}

which produces:

example

as you can see, the first letter of each roman numeral is formatted properly using the command while any subsequent letter is formatted using the normal font.

Is it possible to wrap the \Romanbar command somehow so that it reads the entire following numeral? Or otherwise configure tocloft to achieve the desired formatting?

1
  • I don't know well enough tocloft to be useful, but would you consider doing that with titletoc?
    – Bernard
    Commented Feb 13, 2018 at 20:15

2 Answers 2

2

A little hacky solution:

\documentclass{scrbook}

\renewcommand{\thechapter}{\Roman{chapter}}

\usepackage[usedvipsnames]{xcolor}
\usepackage{tocloft}
\usepackage{romanbar}

\newsavebox{\tocnr}
\renewcommand{\cftchappresnum}{\color{red}\begin{lrbox}{\tocnr}}
\renewcommand{\cftchapaftersnum}{\end{lrbox}\expandafter\Romanbar\expandafter{\usebox{\tocnr}}\relax}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\chapter{First Chapter}
\lipsum[2]

\chapter{Second Chapter}
\lipsum[2]

\chapter{Third Chapter}
\lipsum[2]

\end{document}

enter image description here

2

You could patch \addchaptertocentry:

\documentclass{scrbook}

\renewcommand{\thechapter}{\Roman{chapter}}

\usepackage[usedvipsnames]{xcolor}
\usepackage{romanbar}
\RedeclareSectionCommand[
  tocentrynumberformat=\def\autodot{}\textcolor{red}
]{chapter}

\usepackage{xpatch}
\xpatchcmd{\addchaptertocentry}
  {\addtocentrydefault{chapter}{#1}{#2}}
  {\ifstr{#1}{}{\addtocentrydefault{chapter}{#1}{#2}}
    {\addtocentrydefault{chapter}{\protect\Romanbar{#1}}{#2}}%
  }{}{\PatchFailed}

\usepackage{lipsum}

\begin{document}
\tableofcontents

\chapter{First Chapter}
\lipsum[2]
\chapter{Second Chapter}
\lipsum[2]
\chapter{Third Chapter}
\lipsum[2]
\end{document}

enter image description here

Note that I have used the KOMA-Script command \RedeclareSectionCommand to change the color of the chapter number in TOC. The usage of package tocloft with a KOMA-Script class is not recommended. But if you really want to use package tocloft then add packagexpatch and the patch of \addchaptertocentry to your MWE.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .