56

How to number frames with an increasing number of digits of pi? Illustrative, I want the frame numbers to be like

  • 3 (on the first slide)

  • 3.1 (on the second slide)

  • 3.14 (on the third slide)

... and so on.

This is a question, which occupies me for quite some time; actually since a talk I gave on March 14th (also known as pi-day :).

My present workaround is, to insert the frame numbers by hand – which is of course quite annoying when changing the order of slides.

\documentclass[t]{beamer}

\usepackage[absolute,overlay]{textpos}
\setbeamertemplate{navigation symbols}{}

\newcommand{\pifoot}[1]{
    \begin{textblock*}{120mm}(0mm,84.3mm)
        \raggedleft #1
    \end{textblock*}
}

\begin{document}

    \begin{frame}
        \pifoot{3}
    \end{frame}

    \begin{frame}
        \pifoot{3.1}
    \end{frame}

    \begin{frame}
        \pifoot{3.14}
    \end{frame}

\end{document}

EDIT:

Based on Reds great answer I compiled an unpretentious solution. It abstains new counters in favour of the current page number.

\documentclass[t]{beamer}

\usepackage[absolute,overlay]{textpos}
\setbeamertemplate{navigation symbols}{}
\usepackage{xstring}

\setbeamertemplate{footline}{%
    \begin{picture}(54,12.5)(0,0)
        \put(0.9,0.52){%
            \begin{minipage}[b][12.5mm][c]{112.5mm}
                 \raggedleft
                 3\StrLeft{.141592653589793238462643383279502884197169399375105820974944592307816406286 208998628034825342117067982148086513282306647093844609550582231725359408128481}{\arabic{framenumber}}
            \end{minipage}
        }
    \end{picture}
}

\begin{document}
    \begin{frame}
    \end{frame}

    \begin{frame}
    \end{frame}
\end{document} 
2
  • 2
    Which template are you using? And is it the frame or slide numbers you want pi to appear?
    – percusse
    Sep 9, 2013 at 10:13
  • 1
    I use a custom-made template, therefore it is really nice that the answers below work independently of any template Sep 9, 2013 at 11:05

9 Answers 9

25

EDIT

I did not success in using an algorithm to calculate the pi digits but I can get them by the web using the \write18 command of LaTeX (require --shell-enabled) and the shell command wget and sed. All you need to do is substitute the row

\CatchFileDef{\PiG}{pi.txt}

in the code below with:

\IfFileExists{./digits.txt}{}
    {
    \immediate\write18{\detokenize{wget  http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html -O digits.txt}}
    \immediate\write18{sed -i '/[0-9]$/!d' digits.txt} 
    \immediate\write18{sed -i '1,13!d' digits.txt}
    \immediate\write18{sed -i 's/ //g' digits.txt}
    }
\CatchFileDef{\PiG}{digits.txt}

This is the correct modification to @user36411 answer. Just create a file pi.txt with the number of pi digits you need.

\documentclass[t]{beamer}
\usepackage{xstring,ifthen,catchfile,forloop}
\usepackage[absolute,overlay]{textpos}
\setbeamertemplate{navigation symbols}{}

\CatchFileDef{\PiG}{pi.txt}
\newcounter{Pi}
\setcounter{Pi}{1}
\newcommand{\pifoot}{
    \ifthenelse{\arabic{Pi}=2}{\stepcounter{Pi}}{}
    \begin{textblock*}{120mm}(0mm,84.3mm)
        \raggedleft \StrLeft{\PiG}{\arabic{Pi}}
    \end{textblock*}
    \stepcounter{Pi}
}


\begin{document}
\newcounter{framenum}
\forloop{framenum}{1}{\value{framenumber} < 50}{
    \begin{frame}
        \pifoot
    \end{frame}
}
\end{document}
8
  • 2
    To avoid the warnings when compiling, one can load the lmodern package. Sep 9, 2013 at 10:24
  • @Red: What if you have 44 slides or more? :)
    – Dror
    Sep 9, 2013 at 12:27
  • Adding a few more digits (e.g. from geom.uiuc.edu/~huberty/math5337/groupe/digits.html) works fine [I tried up to 100 slides]. I think the worst threat is to run out of space ... Sep 9, 2013 at 12:33
  • 11
    @Dror --- perhaps you could code up an arbitrary precision pi calculator in TeX... Sep 9, 2013 at 13:30
  • 1
    @Dror now it works to more than 44 digits :-)
    – Red
    Sep 9, 2013 at 20:35
28

Here's a way with LaTeX3 macros:

\documentclass[t]{beamer}
\usepackage{xparse}

\setbeamertemplate{navigation symbols}{}

 % just add the frame numbers
\setbeamertemplate{footline}{\hfill\Large\strut\pagepi{\arabic{framenumber}}\hspace*{1pc}}

\ExplSyntaxOn
\tl_const:Nn \c_pidigits_tl {1415926535897932384626433832795028842}
\DeclareExpandableDocumentCommand{\pagepi}{m}
 {
  \int_compare:nTF { #1 < 2 }
    { 3 }
    {
     3.
     \int_step_function:nnnN { 1 } { 1 } { #1-1 } \my_print_digits:n
    }
 }
\cs_new:Npn \my_print_digits:n #1
 {
  \tl_item:Nn { \c_pidigits_tl } { #1 }
 }
\ExplSyntaxOff
\begin{document}

\begin{frame}
a\pause
b
\end{frame}
\begin{frame}
a\pause
b
\end{frame}
\begin{frame}
a\pause
b
\end{frame}

\end{document}

enter image description here


A different implementation using a property list; here 250 digits are available, much more than a presentation should need.

\documentclass[t]{beamer}
\usepackage{xparse}

\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{\hfill\Large\strut\pagepi{\arabic{framenumber}}\hspace*{1pc}}

\ExplSyntaxOn
\tl_const:Nn \c_pidigits_tl 
 {3%. % source: http://www.eveandersson.com/pi/digits
  1415926535897932384626433
  8327950288419716939937510
  5820974944592307816406286
  2089986280348253421170679
  8214808651328230664709384
  4609550582231725359408128
  4811174502841027019385211
  0555964462294895493038196
  4428810975665933446128475
  6482337867831652712019091
 }
\tl_new:N \l__pidigits_cumulate_tl
\tl_set:Nn \l__pidigits_cumulate_tl { 3. }
\prop_new:N \g_pidigits_prop
\prop_gput:Nnn \g_pidigits_prop { 1 } { 3 }
\int_step_inline:nnnn { 2 } { 1 } { \tl_count:N \c_pidigits_tl }
 {
  \tl_set:Nx \l__pidigits_cumulate_tl
   { \l__pidigits_cumulate_tl \tl_item:Nn \c_pidigits_tl { #1 } }
  \prop_gput:NnV \g_pidigits_prop { #1 } \l__pidigits_cumulate_tl 
 }
\prop_show:N \g_pidigits_prop
\DeclareExpandableDocumentCommand{\pagepi}{m}
 {
  \prop_get:Nf \g_pidigits_prop { #1 } % #1 is \arabic{framenumber}
 }
\cs_generate_variant:Nn \prop_get:Nn { Nf }
\ExplSyntaxOff

\begin{document}

\begin{frame}
a\pause
b
\end{frame}
\begin{frame}
a\pause
b
\end{frame}
\begin{frame}
a\pause
b
\end{frame}

\end{document}
1
  • Note that as of September 2014, \prop_get:Nn has been renamed to \prop_item:Nn
    – egreg
    Sep 17, 2014 at 18:01
20

Without PSTricks. Only up to 19 slides!

\documentclass{beamer}
\usepackage[nomessages]{fp}
\usepackage{multido}

\begin{document}
\multido{\i=0+1}{19}{%
\FPtrunc{\x}{\FPpi}{\i}%
\begin{frame}{\x}
\end{frame}}
\end{document}

enter image description here

2
  • Nice and short, and sufficient for some conference talks which are limited to ~20min. But could you elaborate more on the limit? Why exactly 19?
    – math
    Sep 23, 2013 at 14:33
  • 1
    @math: The magic number 19 is the maximum number (provided by fp package) of digits after the decimal point. Sep 23, 2013 at 14:37
13

I tried to make the total be pi or \pi as well but beamer didn't want to play, this does the digit counting though.

\documentclass[t]{beamer}

\usepackage[absolute,overlay]{textpos}
\setbeamertemplate{navigation symbols}{}

\setbeamertemplate{footline}[frame number]

\newcommand{\pifoot}[1]{
    \begin{textblock*}{120mm}(0mm,84.3mm)
        \raggedleft #1
    \end{textblock*}
}
\def\pilist{3{.1}4159265.....}
\makeatletter
\def\insertframenumber{\csname pi-\the\c@framenumber\endcsname}
\@namedef{pi-1}{3}
\@namedef{pi-2}{3.1}
\@namedef{pi-3}{3.14}
\@namedef{pi-4}{3.141}
\@namedef{pi-5}{3.1415}
\makeatother


% this doesn't work:-)
\def\inserttotalframenumber{pi}

\begin{document}

    \begin{frame}
        \pifoot{3}
    \end{frame}

    \begin{frame}
        \pifoot{3.1}
    \end{frame}

    \begin{frame}
        \pifoot{3.14}
    \end{frame}

\end{document}
12

A slight modification to your code using the xstring package can do the trick. You have to enter Pi as a string, though:

\documentclass[t]{beamer}
\usepackage{xstring}
\usepackage[absolute,overlay]{textpos}
\setbeamertemplate{navigation symbols}{}

\newcounter{Pi}

\newcommand{\pifoot}{
    \begin{textblock*}{120mm}(0mm,84.3mm)
        \raggedleft \StrChar{3141592653589793238462643383279502884197169}{\arabic{Pi}}
    \end{textblock*}
    \stepcounter{Pi}
}


\begin{document}

    \begin{frame}
        \pifoot
    \end{frame}

    \begin{frame}
        \pifoot
    \end{frame}

    \begin{frame}
        \pifoot
    \end{frame}

\end{document}
2
  • 1
    I think you should treat the first frame as a special case so that the label is '3', and for others append '1', '14', '141', etc. to '3.' Sep 9, 2013 at 10:10
  • 1
    With this code I get the first frame blank, the second numbered 3 and the third numbered 1. I don't think that this actually answers the question.
    – cgnieder
    Sep 9, 2013 at 10:18
10

Here is a solution using lua. With this solution you don't have to bother about which slide is it, or whether you have given enough digits at the beginning. Obviously, it can be easily integrated into one of the themes of beamer, so you can typeset it in a fancy way as well.

\documentclass[t]{beamer}

\usepackage[absolute,overlay]{textpos}
\setbeamertemplate{navigation symbols}{}

\usepackage{luatextra}

\newcommand{\pifoot}{
  \begin{textblock*}{120mm}(0mm,84.3mm)
    \raggedleft $\luaexec{
      slidenumber = \insertframenumber
      if tonumber(slidenumber) == 1 then
         numbertoinsert = tostring(3)
      else
         numbertoinsert = "\%." .. tostring(slidenumber-1) .. "f"
      end
      tex.sprint(string.format(numbertoinsert,math.pi))
    }$
  \end{textblock*}
}


\begin{document}

\begin{frame}
  \pifoot
\end{frame}

\begin{frame}
  \pifoot
\end{frame}

\begin{frame}
  \pifoot
\end{frame}

\end{document}

Has to be processed using lualatex.

5
  • The numbers are wrong from slide #17 upwards.
    – Marco
    Sep 9, 2013 at 13:06
  • 1
    True indeed :( I don't know where the problem is coming from, but if I were to guess then I'd say due the bad representation of pi in lua.
    – Dror
    Sep 9, 2013 at 13:37
  • If someone has an idea how to solve the problem mentioned by @Marco, I would be delighted :)
    – Dror
    Sep 9, 2013 at 13:47
  • 1
    LuaTeX is compiled with the double precision floating point format, which is used in Lua for all numeric calculations. By default it does not offer a higher precision without implementing a custom number format or using an external library.
    – Marco
    Sep 9, 2013 at 13:49
  • 1
    Yes, this is what I figured out. There goes one of my first attempts with lua(TeX).
    – Dror
    Sep 9, 2013 at 13:52
10

Here's an approach using stringstrings package.

\documentclass[t]{beamer}
\usepackage{stringstrings}
\usepackage[absolute,overlay]{textpos}
\setbeamertemplate{navigation symbols}{}
\def\PI{3.141592653589793238462643383279502884197169}
\newcounter{sigdigits}
\setcounter{sigdigits}{0}
\newcommand{\pifoot}{%
   \if1\thesigdigits\stepcounter{sigdigits}\fi%
   \stepcounter{sigdigits}%
    \begin{textblock*}{120mm}(0mm,84.3mm)
        \raggedleft \substring{\PI}{1}{\thesigdigits}%
    \end{textblock*}
}

\begin{document}

    \begin{frame}
        \pifoot
    \end{frame}

    \begin{frame}
        \pifoot
    \end{frame}

    \begin{frame}
        \pifoot
    \end{frame}

\end{document}
3
  • fwiw, we once had a ctan package author who numbered versions 1, 1.1, 1.11, 1.111, etc (iirc, he got up to 1.111111111). i persuaded him that this didn't help people who wanted to know if they had the latest version. is pi any better in this regard? -- or does it not patter, since it's "only" a slide show? Sep 9, 2013 at 11:15
  • 1
    @wasteofspace With a name like yours, is that not the pot calling the kettle black :^) I figured the OP's question was not for his general use, but as a gimmick on a particular presentation, perhaps one entitled "The nature of irrational numbers" or something. Sep 9, 2013 at 11:40
  • 1
    @wasteofspace As indicated in the original question, this page-numbering scheme was used for a special occasion (pi-day). And working in a rather nerdy environment my colleges found it quite funny. Sep 9, 2013 at 12:24
6

Here I present a ConTeXt solution which admittedly cheats a little and resorts to Perl, instead of using Lua to calculate Pi. The idea can easily be ported to LaTeX as well.

First the TeX macro \PrintPi is created, which generates one more digit with every consecutive call. It only keeps track of the page number and calls Perl. This macro is then placed in the header of every page.

The performance should be sufficient for presentations with less than 400 slides.

\setuppapersize [S6]  %% screen size for slides

\define\PrintPi
  {\startluacode
    userdata     = userdata or {}
    userdata.num = userdata.num or 1
    userdata.num = userdata.num + 1

    f = io.popen("perl -Mbignum=bpi -wle 'print 0+substr(bpi("..userdata.num.."),0,-1)'")
    context( f:read("*a") )
  \stopluacode}

\setupheadertexts [\PrintPi]

\starttext
  \dorecurse{80}
    {\input ward\page}
\stoptext

Page 69:

screenshot

5

This can also be done by returning \pgfmathresult of pi with the precision determined by the frame number. I kinda like that \pgfmathprintnumber rounds the output, so the fourth frame will be 3.142, the fifth will be 3.1416, the sixth 3.14159, and so on.

\documentclass{beamer}
\setbeamertemplate{footline}{%
    \usebeamerfont{page number in head/foot}
    \pgfkeys{/pgf/number format/.cd,fixed,precision=\thepage}
    \pgfmathprintnumber{\pgfmathresult}
    \vskip.5ex %
}
% Define pi to as many digits as you need
\pgfmathparse{3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067}
\setcounter{page}{0}

\begin{document}
\frame{}\frame{}\frame{}\frame{}\frame{}\frame{}
\end{document}

You must log in to answer this question.

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