# imakeidx and \renewcommand{\thepage} incompatibility

I am writing a long document in LaTeX and I am using imakeidx for indexing contents. All works find. I'm compiling in xelatex and when I used the command:

\renewcommand{\thepage}{{\arabic{page}}\hfill{\roman{page}}}

Like this:

\chapter{{First chapter}}
\renewcommand{\thepage}{{\arabic{page}}\hfill{\roman{page}}}
\setcounter{page}{1}


Then the indexing stops working.

It costed me long time to figure out it was this the problem... It was good because I managed to clean the preamble of packages I'm not really using.

Does anyone could give me light on why there is incompatibility? Thank you in advance

EDITION TO HELP UNDERSTANDING The next code produces the INDEX

% !TEX encoding = UTF-8 Unicode
%!TEX program = xelatex
\documentclass{book} % \documentclass es lo primero que se pone en un documento de latex. Este comando me sirve para decirle a latex si lo que estoy haciendo es un libro, un reporte, un artículo, etc.

\usepackage[utf8]{inputenc}
\usepackage{imakeidx}
\usepackage{blindtext}

\author{Autor 1\\[1cm]{\small Boos: Boos2}}
\title{My document}
\date{\today}
\makeindex
\begin{document}
\maketitle
\chapter{Teoric}
\index{scientific}  \index{dynamic!system}  \index{economic!system}  \index{music}.
\blindtext
$$3+2=5$$

\printindex

\end{document}


The next code does NOT produce the INDEX

% !TEX encoding = UTF-8 Unicode
%!TEX program = xelatex
\documentclass{book} % \documentclass es lo primero que se pone en un documento de latex. Este comando me sirve para decirle a latex si lo que estoy haciendo es un libro, un reporte, un artículo, etc.

\usepackage[utf8]{inputenc}
\usepackage{imakeidx}
\usepackage{blindtext}

\author{Autor 1\\[1cm]{\small Boos: Boos2}}
\title{My document}
\date{\today}
\makeindex

\begin{document}
\maketitle

\chapter{Teoric}
\renewcommand{\thepage}{{\arabic{page}}\hfill{\roman{page}}}
\index{scientific}  \index{dynamic!system}  \index{economic!system}  \index{music}.
\blindtext
$$3+2=5$$

\printindex

\end{document}


The only difference is the line

\renewcommand{\thepage}{{\arabic{page}}\hfill{\roman{page}}}


The first code does not include both indoarabic and roman numbers in the pages but it can make the INDEX with \printindex. The second code does NOT print the INDEX with \printindex but it does include both indoarabic and roman numbers.

This is why I meant that stop working.

• "stops working" is impossible to debug, do you mean you got an error or that the page numbers were wrong or??? It is always best to provide an example that shows the problem. The definition is rather strange and quite likely to break most indexes and tables of contents, you are printing the number twice with infinite stretch glue every time you print the page number in any context????? Feb 2, 2021 at 16:49
• Did you try \tableofcontents? Anyway, how's the index supposed to manage this doubled page number? If you really want to print a double format page number, you should do it as part of the header/footer definitions, not redefining \thepage. Feb 2, 2021 at 16:51
• with your definition a text such as see the figure on page \pageref{zzz} will produce "see the figure on page 2______________________________ii" for any reference that uses a page number, similarly the table of contents will print every page number of every section twice, what is the intention of the definition? Feb 2, 2021 at 17:01
• Dear @DavidCarlisle Thank you for your comments, I have added the code in an edited question.
– Bpi
Feb 2, 2021 at 17:41
• Dear @egreg thank you, I will try that.
– Bpi
Feb 2, 2021 at 17:42

making \thepage print the page number twice would make \pageref (and similar constructs such as tables of contents and lists of tables) unusable.

Presumably the intention is to have the arabic and latin form in the page head (although this is also rather unusual) that can be specified directly without redefining \thepage

\documentclass{book} % \documentclass es lo primero que se pone en un documento de latex. Este comando me sirve para decirle a latex si lo que estoy haciendo es un libro, un reporte, un artículo, etc.

\usepackage[utf8]{inputenc}
\usepackage{imakeidx}
\usepackage{blindtext}

\author{Autor 1\\[1cm]{\small Boos: Boos2}}
\title{My document}
\date{\today}
\makeindex
\renewcommand{\thepage}{\arabic{page}}
\newcommand{\thepagetwice}{\arabic{page}\hfill\roman{page}}

\makeatletter
\makeatother
\begin{document}
\maketitle

\tableofcontents

\chapter{Teoric}

\index{scientific}  \index{dynamic!system}  \index{economic!system}  \index{music}.
\blindtext
$$3+2=5$$

\printindex

\end{document}


This has the duplicated number in the page head

and a usable index

• Dear @DavidCarlisle Thank you very much for your answer, I will study it carefully I'm sure this will help. Thank you again.
– Bpi
Feb 2, 2021 at 18:23

When \index does its job it writes out a line of the form

\indexentry{scientific|hyperpage}{5}


and the last braced group must contain a single number (in whatever supported format). In your case it will contain {5\hfill v} which is illegal.

This seems to work, but I believe you should reconsider the problem and solve it in a different way: you get scores of warnings.

Oh, the links won't work at all. But that's another story.

\documentclass{book}

\usepackage[utf8]{inputenc}
\usepackage{imakeidx}
\usepackage{xpatch}
\usepackage{blindtext}

\author{Autor 1\\[1cm]{\small Boos: Boos2}}
\title{My document}
\date{\today}

\renewcommand{\thepage}{\arabic{page}\hfill\roman{page}}

\makeatletter
\xpatchcmd\HyInd@org@wrindex{\thepage}{\arabic{page}}{}{}
\makeatother

\NewCommandCopy{\hyperpageoriginal}{\hyperpage}
\renewcommand{\hyperpage}[1]{%
\hyperpageoriginal{#1\ \romannumeral#1}%
}

\makeindex

\begin{document}

\maketitle

\tableofcontents

\chapter{Teoric}
\section{whatever}
\index{scientific}  \index{dynamic!system}  \index{economic!system}  \index{music}.
\blindtext
$$3+2=5$$

\printindex

\end{document}

• Dear @egreg thank you very much.
– Bpi
Feb 2, 2021 at 19:06
• I didn't manage to get it to work. I keep getting:  Undefined control sequence. l.19 \NewCommandCopy {\hyperpageoriginal}{\hyperpage} can't figure why.
– Bpi
Apr 27, 2021 at 22:15