3

EDIT2: Sorry for this real time updates, but I am really going crazy. I added at the bottom the new code that is compiling correctly with showhexapagesStepOne

I edited the question trying to give more cartesian organization to the whole endeavour. Changed also the code provided with something closer to correctness (I hope).

I would like to use a standard book document and change the numbering of pages inside fancyhdr. In particular I want the pages to be printed in hex style 0xABCD. I have tried almost everything (i am not posting all the code attempts here because it would be embarassing), namely:

  1. pythonTex (just after immediatepython): stuck at trying to pass a LaTex counter before the counter really could be updated into the document. I renounced after 2 days.
  2. hex package (very old by Eric Domenjoud) but it is still using documentstyle and I got stuck in converting a old /usename directive
  3. tried also binhex package but same problem: counter expansion. Now I am trying to make it work with fmtcount package.

My preamble contain this fancyheader part before the document begins (just on top of it there are few experiments with the counters):

\newcounter{hexapages}


\newcommand\showhexapagesStepOne{%
\setcounter{hexapages}{\thepage}%
\thehexapages%
}

\newcommand\showhexapagesStepTwo{% If I make showhexapagesStepOne work, this will work as well
\setcounter{hexapages}{\thepage}%
\HEXADecimal(hexapages)% or maybe thehexapages ???
}


\setlength{\headheight}{15pt}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\renewcommand{\sectionmark}[1]{ \markright{#1} }

\fancyhf{}
%\fancyhead[LE]{{\thepage}} %obviously compiles
%\fancyhead[RO]{{\thepage}} %obviously compiles
\fancyhead[LE]{{\showhexapagesStepOne}} % does not compile
\fancyhead[RO]{{\showhexapagesStepOne}} % does not compile
\fancyhead[RE]{\textit{ \nouppercase{\leftmark}} }
\fancyhead[LO]{\textit{ \nouppercase{\rightmark}} }
\fancyheadoffset[LE]{14mm} 

The current error is: (foreword.tex ! Missing number, treated as zero. i l.9

[I think Latex is trying to apply the conversion to something that is numbered in roman style...so I should differentiate between frontmatter and mainmatter....]

What seems to fail is trying to pass any counter (predefined as \thepage or custom made as \thehexapages) into any placeholder of fancy header.

The first attemtps with PythonTex were intended to create a macro that was doing the translation of any number into its hex conversion, but failed when the argument became counters.

How should I do it? Why fancyheader takes \thepage like a charm and refuses whatever else? If a try HEXADecimalnum{459} it prints correctly "1CB". So, it is not the fmtcount package giving issues.

Start of EDIT2: The new code is:

\newcounter{hexapages}

\newcommand\showhexapagesStepOne{%
\setcounter{hexapages}{\thepage}%
\thehexapages%
}

\newcommand\showhexapagesStepTwo{%
\setcounter{hexapages}{\thepage}%
\HEXADecimal(hexapages)% or maybe thehexapages ???
}


\setlength{\headheight}{15pt}

\pagestyle{fancyplain}
\fancyhf{}


\fancypagestyle{plain}{ %
  \fancyhf{} % remove everything
  \renewcommand{\headrulewidth}{0pt} % remove lines as well
  \renewcommand{\footrulewidth}{0pt}
}

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\renewcommand{\sectionmark}[1]{ \markright{#1} }
%\fancyhead[LE]{{\thepage}}
%\fancyhead[RO]{{\thepage}}
\fancyhead[LE]{{\showhexapagesStepOne}}
\fancyhead[RO]{{\showhexapagesStepOne}}
\fancyhead[RE]{\textit{ \nouppercase{\leftmark}} }
\fancyhead[LO]{\textit{ \nouppercase{\rightmark}} }
\fancyheadoffset[LE]{14mm}% slightly less than 0.25in
\fancyheadoffset[RO]{14mm}%
\renewcommand{\headrulewidth}{0pt} % remove lines as well
\renewcommand{\footrulewidth}{0pt}

% and after all the preamble definitions.....


\frontmatter 
\pagestyle{plain}
\include{dedication}
\include{foreword}
\include{preface}
\include{acknowledgement}

\tableofcontents

\include{acronym}



\mainmatter%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\renewcommand{\sectionmark}[1]{ \markright{#1} }
%\fancyhead[LE]{{\thepage}}
%\fancyhead[RO]{{\thepage}}
\fancyhead[LE]{{\showhexapagesStepTwo}}
\fancyhead[RO]{{\showhexapagesStepTwo}}
\fancyhead[RE]{\textit{ \nouppercase{\leftmark}} }
\fancyhead[LO]{\textit{ \nouppercase{\rightmark}} }
\fancyheadoffset[LE]{14mm}% slightly less than 0.25in
\fancyheadoffset[RO]{14mm}%
\renewcommand{\headrulewidth}{0pt} % remove lines as well
\renewcommand{\footrulewidth}{0pt}

This allows me to remove any heading and numbering in the frontmatter and leaves the fancy style only for the mainmatter. THe point is: now it prints arabic numbers correctly in the LE and RO angles using showhexapagesStepOne. When I switch to the StepTwo (the one containing the conversion) I get the error:

! You can't use `\relax' after \the.
<recently read> \c@(

Any clue?

3 Answers 3

2

Here's a fairly general way to print the page number in hexadecimal format, with fully expandable commands.

\documentclass{article}

\usepackage[a6paper]{geometry} % just to show several pages
\usepackage{lipsum} % filler text

\usepackage{fancyhdr}

\fancyhf{}
\fancyfoot[C]{\texttt{\thepage}}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\printhex}{O{4}m}
 {% #1 = number of digits, #2 = integer
  0x
  \prg_replicate:nn { #1 - \tl_count:e { \int_to_Hex:n { #2 } } } { 0 }
  \int_to_Hex:n { #2 }
 }
\cs_generate_variant:Nn \tl_count:n { e }

\ExplSyntaxOff

\renewcommand{\thepage}{\printhex{\value{page}}}

\begin{document}

\raggedright % don't bother with overfull boxes

\lipsum

\clearpage

\setcounter{page}{6700}

\lipsum

\end{document}

In the picture I show the two pages before and after \clearpage, in order to show that page 6700 is correctly shown as 0x1A2C.

enter image description here

1
2

Ganzo! I was just succeeding in posting the answer to this life draining issue while Niranjan commented. I found a solution with fmtcount package but he did not. Niranjan used binhex. How is that possible? I gave up on that! So, I tried again starting from my latest solution (the one I was writing the answer with) including his great hint on binhex. Actually binhex is fundamentally better because it pads to the required alignment in a matter of seconds! And in fact here below the solutionw ith both the packages. I will use binhex as proposed in the comment. Really thanks!

%----------------------------------------------------------------------------------------
%   HEADERS & FOOTERS
%----------------------------------------------------------------------------------------


% hexadecimal page numbers
  
\newcommand\showhexapagesStepTwo{%
%0x\HEXADecimal{hexapages} % this can work but it does not pad to any alignment
0x\nhex{4}{\thepage}
}


\setlength{\headheight}{15pt}

\pagestyle{fancyplain}
\fancyhf{}


\fancypagestyle{plain}{ %
  \fancyhf{} % remove everything
  \renewcommand{\headrulewidth}{0pt} % remove lines as well
  \renewcommand{\footrulewidth}{0pt}
}

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\renewcommand{\sectionmark}[1]{ \markright{#1} }
\fancyhead[LE]{{\showhexapagesStepTwo}}
\fancyhead[RO]{{\showhexapagesStepTwo}}
\fancyhead[RE]{\textit{ \nouppercase{\leftmark}} }
\fancyhead[LO]{\textit{ \nouppercase{\rightmark}} }
\fancyheadoffset[LE]{14mm}% slightly less than 0.25in
\fancyheadoffset[RO]{14mm}%
\renewcommand{\headrulewidth}{0pt} % remove lines as well
\renewcommand{\footrulewidth}{0pt}

There is not even need for any proxy counter (\thepage fits perfectly once the fancyhdr directives are placed after document begins). I don't really know what I was doing wrong. Probably in the many many attempts I meesed up everything and lost sight on the solution. Anyway this is the only question on this aspect that I found on SO! or SE! so I am posting the solution for future use.

A final note: the real issue was mainly connected to the usage of nested `thepagebefore\begin{document}`. I think special attention is necessary in such a case becaue the macro should not be expanded at definition but only when there is a real page to number underneath (so in the mainmatter usually).

Regards Alex

1
  • Most welcome! As explained in my answer too, I don't think the real issue was with where you had used \thepage. The real issue was you were using \thepage in \frontmatter where it means a letter and not a number. So the trick was to use \pagenumbering{arabic}. Try going through my shorter examples first to understand these things conceptually. Your actual large code might confuse you.
    – Niranjan
    Apr 7 at 14:26
1

The \hex command needs a number as its argument. When you use something like \hex{\thepage}, you should keep in mind that \thepage is sensitive to context. In \frontmatter it doesn't print numbers. Instead, it prints letters. To see what I mean, try this:

\documentclass{book}

\begin{document}
\frontmatter
\verb|\thepage| prints \thepage.

\mainmatter
\verb|\thepage| prints \thepage.
\end{document}

So something like \hex{\thepage} will obviously give you a Missing number error when \frontmatter is active, because, well, a number is actually missing! :)

So the solution can be much simpler:

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}
\input{binhex}
\pagestyle{fancy}

\begin{document}
\frontmatter
\pagenumbering{arabic}% \thepage will produce numbers.
\cfoot{\hex{\thepage}}
\lipsum[1-50]
\end{document}

I don't know much about the hex style of numbering, but I can see A on page 10. So I suppose it is working.

You must log in to answer this question.

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