2

Let's say I want to produce the same effect as in How do you switch numbers in a chapter?

but with Roman numerals. So I would have

  • Chapter I
  • Chapter II
  • Chapter III+IV
  • Chapter VI

instead of the arabic version in that post.

In that post, Chapter 3+4 is produced by the following code:

\def\thechapter{\arabic{chapter}+\the\numexpr\value{chapter}+1\relax}

I wonder if there is a similar way to produce Chapter III+IV. If I just do

\def\thechapter{\Roman{chapter}+\Roman{\the\numexpr\value{chapter}+1\relax}}

it will return an error. (The same if I remove \the.)

3 Answers3

6

This answer really answers the specific question of why the error occurs and how to fix it. It doesn't address the issues answered in the linked question. See egreg's answer for that kind of solution or use the solution here as part of the solution given in the linked question.

The \Roman command (and similar commands) expect a counter as an argument, not a number, but your \numexpr yields a number, hence the error you receive:

Missing number, treated as zero.
<to be read again> 
\c@4

So if you want to do this simply by using the same method, you need to use the internal command \@Roman which expects a number.

\documentclass{book}

\makeatletter \def\thechapter{\Roman{chapter}+@Roman{\numexpr\value{chapter}+1\relax}} \makeatother \begin{document} \setcounter{chapter}{2} \chapter{A chapter} \end{document}

If you don't want to use the low level command \@Roman you can load the calc package, and use a temporary counter to achieve the same effect:

\documentclass{book}
\newcounter{tmpchap}
\usepackage{calc}
\renewcommand\thechapter{\protect\setcounter{tmpchap}{\value{chapter}+1}\Roman{chapter}+\Roman{tmpchap}}
\begin{document}
\setcounter{chapter}{2}
\chapter{A chapter}
\end{document}

output of code

Alan Munn
  • 218,180
  • Thanks! I find that the plethora of data types in tex always confuses me. There are counters (chapter), numbers (\value, \numexpr), strings (\Roman{counter})...Actually, if I remove \the from your code, there will be an error, but I don't understand why. Doesn't \numexpr already return a number? I thought \the converts a number to an arabic string. – Yifeng Huang Mar 26 '22 at 19:20
  • @YifengHuang If you want to learn TeX, TeXbook is a good resource. (or TeX by topic.) (or just learn expl3 and ignore the low level details entirely.) \numexpr is documented in a different place (etex manual), however. – user202729 Mar 26 '22 at 20:37
  • @YifengHuang Although I do not get an error removing \the, so you probably made some mistake somewhere... – user202729 Mar 26 '22 at 20:43
  • @YifengHuang As noted, the \the is not actually needed here, so I've removed it. \the is a TeX primitive that does many things, much more than can be explained in a comment, but not surprisingly we have a whole question on it: The \the command – Alan Munn Mar 27 '22 at 14:56
4

Alan Munn’s answer is fine, but there is a better method.

\ExplSyntaxOn
\NewDocumentCommand{\doublechapter}{O{#2}m}
 {
  \renewcommand{\thechapter}
   {
    \int_to_Roman:n {\value{chapter}}+\int_to_Roman:n {\value{chapter}+1}
   }
   \chapter[#1]{#2}
   \stepcounter{chapter}
   \renewcommand{\thechapter}{\Roman{chapter}}
 }
\ExplSyntaxOff

It is still possible to do \doublechapter[Short]{Long}

Full example

I use openany just to avoid blank pages and geometry just to make smaller pictures.

\documentclass[openany]{book}
\usepackage[a6paper]{geometry}

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

\ExplSyntaxOn \NewDocumentCommand{\doublechapter}{O{#2}m} { \renewcommand{\thechapter} { \int_to_Roman:n {\value{chapter}}+\int_to_Roman:n {\value{chapter}+1} } \chapter[#1]{#2} \stepcounter{chapter} \renewcommand{\thechapter}{\Roman{chapter}} } \ExplSyntaxOff

\begin{document}

\chapter{Title one}

\chapter{Title two}

\doublechapter{Title for the double chapter}\label{doublechapter}

\chapter{Title five}

\ref{doublechapter}

\end{document}

enter image description here

egreg
  • 1,121,712
1

After reading this answer, I found a solution myself.

\renewcommand{\thechapter}{\Roman(chapter}+\uppercase\expandafter{\romannumeral\numexpr\value{chapter}+1\relax\relax}}

To explain according to my understanding, chapter is a counter (as an argument type), and \Roman converts a counter to a string. On the other hand, \numexpr ... \relax is a number, so we need \romannumeral...\relax to convert it to a string. This produces the lower-case Roman numeral, and applying \uppercase\expandafter{ ... } makes it upper-case.