11

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, ...

DDS
  • 8,816
  • 4
  • 9
  • 36
rudstep
  • 111
  • 1
    Welcome to TeX.SX! This looks very much like http://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 01:03

2 Answers2

16

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:

Result 200

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.

Heiko Oberdiek
  • 271,626
8

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{%
  \oodef\temp{\xintiiAdd{\twoback}{\oneback}}%         
  \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.

enter image description here


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}}
egreg
  • 1,121,712
  • 2
    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
  • 1
    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. –  Sep 19 '14 at 15:10
  • @jfbu Thanks for the suggestion. Your packages are indeed amazing. – egreg Sep 19 '14 at 15:15
  • thanks... in my previous comment I mistyped \xintexpr where your original and correct \xintiiexpr was meant, sorry. I did some additional work on \xintexpr in June, and your comment encourages me to work towards releasing it sometime soon. –  Sep 19 '14 at 15:20
  • @jfbu I was serious about a 'bigint' module for expl3: I might take a look at your code if you've sorted expressions (we have single big ints but not expressions, and the latter would be nice for symmetry). Note of course the like most FPUs l3fp doesn't claim or aim to go beyond a certain number of places of precision. – Joseph Wright Sep 19 '14 at 15:25
  • To be clear, I'm thinking of expressions of the same form as allowed by \numexpr (and so \int_eval:n), not any further 'stuff'. – Joseph Wright Sep 19 '14 at 15:32
  • @JosephWright currently \xintiiexpr works with big ints, +, -, * and maps therein / to the Euclidean division (which truncates; rounding could be chosen instead). But it has more stuff than \numexpr: it knows ^ for power, it has max and min also gcd and lcm for multiple arguments, it knows sqrt for truncated square root... it knows boolean operation (development version uses && and || notation like in l3fp). Although only macros from xint are needed, \xintiiexpr is in package xintexpr which loads all the extra stuff in xintfrac dealing with fractions. –  Sep 19 '14 at 15:40
  • @jfbu Yes, I'd seen that xintexpr covers many things. What I was trying to get at is that for an expl3 implementation, expressions would be nice but would presumably be set up to match what can be done by (most obviously) \numexpr. That means on +, -, * and / as 'native' operations, with / rounding and a separate \bigint_div_truncate:n. [I'm thinking we should look at this for expl3 as the idea is we are aiming to cover what people commonly need in programming, and I need big integers for siunitx :-)] – Joseph Wright Sep 19 '14 at 15:44
  • @JosephWright (side note: development version has some new style when expandably branching which will make the code more readable.) Does bigint module for expl3 want as Heiko's bigintcalc or my xint deal with arbitrarily big integers, or say, up to 64 or 96 digits? –  Sep 19 '14 at 15:46
  • @jfbu I added a mention and code for the new bnumexpr package – egreg Sep 22 '14 at 16:00
  • @egreg after some procrastination I am adding l3bigint option to package bnumexpr. Somehow I got afraid it would be complicated to install l3bigint but I was wrong. –  Sep 22 '14 at 19:29