# Page number in Fibonacci style

How to display the page number in Fibonacci. Example: Normal page number 1, 2, 3, 4, 5, 6, 7, 8,...

Fibonnacci page number style: 1, 1, 2, 3, 5, 8, 13, ...

• Welcome to TeX.SX! This looks very much like tex.stackexchange.com/q/164984/15925, and may well get closed as a duplicate of that. In any case we usually ask that you show what you have tried and include some sample code. If you don't have success modifying the code there, then please update your question saying what is giving you difficulties. – Andrew Swann Sep 19 '14 at 13:08
• ...........why? – meawoppl Sep 20 '14 at 1:03

Package fibnum helps:

\usepackage{fibnum}
\renewcommand*{\thepage}{\fibnum{\value{page}}}


Full example:

\documentclass{article}
\usepackage{fibnum}
\renewcommand*{\thepage}{\fibnum{\value{page}}}
\begin{document}
\newcommand*{\test}{%
\begin{tabular}{ll}
Page value: & \the\value{page}\\
\texttt{\textbackslash thepage}: & \thepage
\end{tabular}%
\newpage
}
\test
\test
\test
\test
\test
\test
\test
\test
\test
\end{document}


If the number of pages is large (>> 46), then \fibnumPreCalc should be used to get a more efficient \fibnum. \fibnum must calculate the numbers each time, unless the values are calculated in advance via \fibnumPreCalc. Example:

\documentclass{article}

\usepackage{fibnum}
\renewcommand*{\thepage}{\fibnum{\value{page}}}

\usepackage{atbegshi}
\AtBeginShipout{%
\fibnumPreCalc{\value{page}}%
}

\begin{document}
\newcommand*{\test}{%
\begin{tabular}{ll}
Page value: & \the\value{page}\\
\texttt{\textbackslash thepage}: & \thepage
\end{tabular}%
\newpage
}
\test
\test
\test
\test
\test
\test
\test
\test
\setcounter{page}{200}
\test
\end{document}


Table in last page:

The package fibnum calculates and stores the first 46 Fibonacci numbers, because these numbers also fit into a TeX count register. However the numbers are not limited by this, as the previous example has shown. Above this value package fignum switches its calculation method by using the expandable operations for big integers of package bigintcalc.

Two solutions.

# With Binet's formula

\documentclass{article}
\usepackage[paperwidth=3cm,paperheight=4cm,textheight=2cm]{geometry}
\usepackage{xparse}

\ExplSyntaxOn
\fp_const:Nn \c_rudstep_phi_fp { (sqrt(5)+1)/2 } % the golden ratio

\DeclareExpandableDocumentCommand{\fibonacci}{m}
{
\fp_eval:n
{
round( ( ( \c_rudstep_phi_fp)^(#1) - (1 - \c_rudstep_phi_fp)^(#1) ) / sqrt(5) , 0 )
}
}

\ExplSyntaxOff

\renewcommand{\thepage}{\fibonacci{\value{page}}}

\begin{document}

a\newpage a\newpage a\newpage a\newpage a\newpage a\newpage
a\newpage a\newpage a\newpage a\newpage a\newpage a\newpage

\end{document}


Note: the rounding operation guarantees a good result only for 68 pages. Indeed \fibonacci{68} returns 72723460248141, which is correct; on the other hand \fibonacci{69} returns 117669030460995, while the correct value is 117669030460994. No number returned thereafter is correct.

# With the recursive formula

\documentclass{article}
\usepackage[paperwidth=3cm,paperheight=4cm,textheight=2cm]{geometry}
\usepackage{atbegshi}

\newcounter{twoback}
\newcounter{oneback}
\setcounter{twoback}{0}
\setcounter{oneback}{1}

\AtBeginShipout{%
\setcounter{page}{\numexpr\value{twoback}+\value{oneback}-1}% page is stepped later
\setcounter{twoback}{\value{oneback}}%
\setcounter{oneback}{\numexpr\value{page}+1}%
}

\begin{document}

a\newpage a\newpage a\newpage a\newpage a\newpage a\newpage
a\newpage a\newpage a\newpage a\newpage a\newpage a\newpage

\end{document}


Note: This method will produce the correct number until the Fibonacci number is less than 231, that is in the range of TeX integers. Thus only 46 pages are supported.

This limitation can be overcome by using jfbu's package xint, with basically the same method that doesn't compute anew the number, but just applies the recursive definition.

\documentclass{article}
\usepackage[paperwidth=3cm,paperheight=4cm,textheight=2cm]{geometry}
\usepackage{xint,atbegshi}

\newcommand\twoback{0}
\newcommand\oneback{1}

\AtBeginShipout{%
\global\let\thepage\temp
\global\let\twoback\oneback
\global\let\oneback\temp
}
\AtBeginDocument{\def\thepage{1}}% initialize, just to be on the safe side

\begin{document}

a\newpage a\newpage a\newpage a\newpage a\newpage a\newpage
a\newpage a\newpage a\newpage a\newpage a\newpage a\newpage

\end{document}


# Output of both

Note that the setting with geometry is not relevant.

As of September 22, 2014, a new package is available: bnumexpr, that allows for a more natural notation. So instead of xint one can load bnumexpr and change the code above into

\usepackage{bnumexpr,atbegshi}

\newcommand\twoback{0}
\newcommand\oneback{1}

\AtBeginShipout{%
\edef\temp{\thebnumexpr\twoback+\oneback\relax}%
\global\let\thepage\temp
\global\let\twoback\oneback
\global\let\oneback\temp
}
% initialize, just to be on the safe side; the second is easier ;-)
%\AtBeginDocument{\edef\thepage{\thebnumexpr\twoback+\oneback\relax}}
\AtBeginDocument{\def\thepage{1}}

• You really like the LaTeX3 FPU, don't you :-) – Joseph Wright Sep 19 '14 at 13:59
• @JosephWright Yes, of course. ;-) I added the limitations. – egreg Sep 19 '14 at 14:02
• Perhaps we need to ship a big-integer module for expl3 to handle these cases :-) – Joseph Wright Sep 19 '14 at 14:14
• It is a honor for xintexpr to relay l3fp beyond 16 digits of precision... for people not otherwise needing \xintexpr it is enough to only load xint rather than xintexpr and then \oodef\temp{\xintiiAdd{\twoback}{\oneback}} rather than \fdef\temp{\xintthe\xintexpr\twoback+\oneback\relax}. For people who wonder: \oodef and \fdef are wrappers around \def, defined in package xint, they apply some type of expansion to the argument of the macro, in the case at hand it achieves the same result as if using \edef, with a minuscule gain in time. – user4686 Sep 19 '14 at 15:10
• @jfbu Thanks for the suggestion. Your packages are indeed amazing. – egreg Sep 19 '14 at 15:15