[Tex/LaTex] Helping cardinals elect the new Pope

datatoolfuntikz-pgf

Yes, we are talking about TeX and friends!

Edit: I decided to move the background motivation to the end of the question, since it seems to be kind of off-topic in the TeX context.

The following code is just a preliminary idea. My plan is to hide a notebook inside the Sistine Chapel, open a spreadsheet application and start to count votes for the new pope election.

The following list contains the name of the 115 candidates to become the new pope. In the whole process, data is manually tabulated according to the voting ballots, and every cardinal annotates in his own vota scrutinii the number of votes for each candidate. It's still ruled by ink and paper.

spreadsheet

Now, it's just a matter of exporting data to .csv and use Nicola Talbot's awesome datatool package in order to generate a beautiful report for the cardinals! But wait a minute, we still need to keep up the tradition, ink and pen.

Thanks to my buddy percusse, it's TikZ time! Let's fake some handwriting annotation for the votes! The idea here is very simple: get the number of votes, generate a TikZ picture with a visual representation of the current number and present it.

First things first. Here it is cardinals.csv:

Surname,Name,Votes
Kasper,Walter,0
Poletto,Severino,0
Sandoval Íñiguez,Juan,0
Danneels,Godfried,0
Errázuriz ossa,Francisco Javier,0
Farina,Raffaele,0
Agnelo,Geraldo Majella,7
Meisner,Joachim,0
Vela Chiriboga,Raúl Eduardo,0
Re,Giovanni Battista,0
Tettamanzi,Dionigi,0
Monterisi,Francesco,0
Hummes,Cláudio,15
Amigo Vallejo,Carlos,0
Sardi,Paolo,0
Cordes,Paul Josef,0
Rodé,Franc,0
Bertone,Tarcisio,11
Pham Minh Mân,Jean-Baptiste,0
Lajolo,Giovanni,0
Naguib,Antonios,0
Rigali,Justin Francis,0
De Paolis,Velasio,0
Abril y Castelló,Santos,0
da Cruz Policarpo,José ,0
Mahony,Roger Michael,0
Terrazas Sandoval,Julio,0
Dias,Ivan,0
Lehmann,Karl,0
Levada,William Joseph,0
Okogie,Anthony Olubunmi,0
Turcotte,Jean-Claude,0
Rouco Varela,Antonio María,0
Ortega y Alamino,Jaime Lucas,0
López Rodrígues,Nicolás de Jesús,0
Antonelli,Ennio,0
Sarr,Théodore-Adrien,0
Bergoglio,Jorge Mario,0
George,Francis Eugene,0
Bačkis,Audrys Juozas,0
Damasceno Assis,Raymundo,7
Nicora,Attilio,0
Martínez Sistach,Lluís,0
Vegliò,Antonio Maria,0
Romeo,Paolo,0
Coccopalmerio,Francesco,0
Monteiro de Castro,Manuel,0
Caffarra,Carlo,0
Amato,Angelo,0
O'Brien,Edwin Frederik,0
Dziwisz,Stanisław,29
Tong Hon,John,0
Brady,Seán Baptist,0
Monsengwo,Pasinya Laurent,0
Grocholewsky,Zenon,0
Toppo,Telesphore Placidus,0
Raï,Béchara Boutros,0
Vallini,Agostino,0
Wuerl,Donald William,0
Zubeir Wako,Gabriel,0
Napier,Wilfrid Fox,0
Pell,George,0
Scola,Angelo,0
Rivera Carrera,Norberto,0
Urosa Savino,Jorge Liberato,0
Salazar Gómez,Rubén,0
Bertello,Giuseppe,0
Ravasi,Gianfranco,0
Vingt-Trois,André,0
Rodríguez Maradiaga,Óscar Andrés,0
Bagnasco,Angelo,0
Calcagno,Domenico,0
Duka,Dominik,0
Sepe,Crescenzio,0
Tauran,Jean-Louis,0
Versaldi,Giuseppe,0
Comastri,Angelo,0
Sandri,Leonardo,0
Cipriani Thorne,Juan Luis,0
Onaiyekan,John Olorunfemi,0
Ouellet,Marc,0
O’Malley,Sean Patrick,0
Pengo,Polycarp,0
Piacenza,Mauro,0
Ricard,Jean-Pierre,0
Gracias,Oswald,0
Njue,John,0
Schönborn,Christoph,0
Alencherry,George,0
Sarah,Robert,0
Ryłko,Stanisław,0
Puljić,Vinko,0
Cañizares Llovera,Antonio,0
Filoni,Fernando,0
Collins,Thomas Christopher,0
Betori,Giuseppe,0
Braz de Avis,João,21
Patabendige Don,Albert Malcolm Ranjith,0
Burke,Raymond Leo,0
Turkson,Peter Kodwo Appiah,0
Robles Ortega,Francisco,0
Bozanić,Josip,0
DiNardo,Daniel Nicholas,0
Scherer,Odilo Pedro,37
Harvey,James Michael,0
Nycz,Kazimierz,0
Dolan,Timothy Michael,2
Koch,Kurt,0
Barbarin,Philippe,0
Erdő,Péter,0
Eijk,Willem Jacobus,0
Marx,Reinhard,0
Woelki,Rainer Maria,0
Tagle,Luis Antonio,0
Thottunkal,Baselios Cleemis,0

And now, our beloved vota.tex:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[top=1.5cm, bottom=1.5cm, left=1.5cm, right=1.5cm, landscape]{geometry}

\usepackage{lmodern}
\usepackage{datatool}
\usepackage{multicol}
\usepackage{xspace}

\usepackage{tikz}
\usetikzlibrary{calc,decorations}
\makeatletter
\pgfdeclaredecoration{penciline}{initial}{
\state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,auto corner on length=1mm,]{\pgfpathcurveto{%
\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{\pgfdecorationsegmentamplitude}}{\pgfmathrand\pgfpointadd{%
\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}}{\pgfqpoint{-\pgfdecorationsegmentaspect\pgfdecoratedinputsegmentremainingdistance}
{\pgfmathresult\pgfdecorationsegmentamplitude}}}{\pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}}}\state{final}{}}

\tikzset{
    prison/.style={
    append after command={
        \pgfextra{\ifnum#1=0\else
        \foreach \x in {0,...,\number\numexpr#1-1\relax}{
        \coordinate (mytemp) at ($(\tikzlastnode.north west)!0.3333*\x!(\tikzlastnode.north east)$);
        \draw[decorate] (mytemp) -- (mytemp|-\tikzlastnode.south);}\fi
            }
        }
    },
    fived/.style={append after command={
        \pgfextra{
        \draw[decorate] ([shift={(-2mm,1mm)}]\tikzlastnode.south west)--([shift={(2mm,-1mm)}]\tikzlastnode.north east);}
        }
    }
}
\makeatother

\newcommand{\drawvotes}[1]{%
\begin{tikzpicture}[decoration=penciline]
\def\numofscr{#1}
\pgfmathtruncatemacro\residue{Mod(\numofscr,5)}
\pgfmathtruncatemacro\numoflots{\numofscr/5}
\ifnum\numofscr<5\else 
\foreach \x in {1,...,\numoflots}{
\node[prison=4,fived] (a) at (\x,0) {};
}
\fi
\node[prison=\residue] at (\numoflots+1,0) {};
\end{tikzpicture}
}

\DTLloaddb{cardinals}{cardinals.csv}

\begin{document}

\pagestyle{empty}

\begin{center}
{\Huge\textsc{Vota Scrutinii}}
\end{center}

\vspace{1em}

\begin{multicols}{2}
\begin{enumerate}
\DTLforeach{cardinals}{\surname=Surname,\name=Name,\votes=Votes}{%
\item \textsc{\surname\xspace\drawvotes{\votes}}}
\end{enumerate}
\end{multicols}

\end{document}

A taste of the output:

output

Let's highlight the voting part:

voting scheme

Yay, we just brought TeX and friends to the election of the pope! Now, I'd love to improve the code and add, if possible, the following features:

  1. A cardinal is elected pope if he obtains 2/3 of all votes. If one the candidates reach this number, we could add a nice blue circle around his name in the list, and probably a note with Habemus Papam! near it. That way, the lazy cardinals will easily spot the elected one. :)

  2. If no one reaches 2/3 of the votes, at least we now have a perspective of the voting scheme, like the top voted cardinals, which will influence the next voting sessions. My idea is, if no cardinal reaches 2/3 of all votes, let's add a nice red circle around the names of the 5 top voted cardinals, so we can easily spot the trend going on. :)

Feel free to add more features if you like. :)

Well, that's it. Hope you guys like the initial code and idea. :) Now, we need to kindly ask egreg or Claudio Fiandrino to go to Rome and convince the cardinals to use our proposal. It will be way funnier. :)

Any help is appreciated. :)


Background

Being catholic, the election of a new pope is a milestone for me. Actually, with Pope Benedict XVI resigning, this will be the second election I watch – Pope John Paul II was elected in 1978, a few years before I was born, but of course I watched Cardinal Ratzinger being elected. :) I even saw the white smoke on television!

My dad used to tell me a lot of stories from past elections. I think the whole process is quite amusing, specially the way cardinals vote for their candidates.

Cardinal José Freire Falcão, former archbishop of Brasília, gave a very nice interview to a Brazilian website in which he showed documents from the last conclave. This is the voting ballot, where the cardinal writes down the name of his choice. After every cardinal casts his vote, it's time to tabulate data! This is done in this other document, called vota scrutinii.

I'm almost sure the process is the very same done since the beginning of time, with ink and paper. But of course, how about giving them a little help with LaTeX and friends? :)

Best Answer

My datatool skills are limited, so the following solution uses TeX primitives and PGFs mathematical engine and also its \pgfutil@in@ macro/conditional.

Interaction

  • Line 99: Minimum ratio a Cardinal needs to get elected as Pope.

    \pgfmathparse{2/3*\the\value{sumVotes}}
    
  • Line 70: Number of Cardinals that will be marked if no one has reached the limit. As this will be used inside a \foreachloop it should be at least 3 (→ Top 3).

    \setcounter{numberOfTopCardinals}{5}
    

    There are some explanations needed: If two or more Cardinals got the same number of votes they will both be added to the Top list. This means that, although the numberOfTopCardinals is set to, say 5, we could have six or more Cardinals in the Top 5 list. Aka “Tie”.

  • The \ifHabemusPapam conditional can be used to check whether the limit of minimum votes has been reached by one of the Cardinals.

  • The styles papa, not papa and top as well as every name can be used to change the appearance of the list.

How does it work?

Fairly simply:

  • First loop:
    • Prepare the macros for the top list (\votesForPlace<place>)
  • Second loop:
    • Sum over all votes
    • If one’s Cardinal vote is greater than one of the number of votes in the list include it and push the last one out. (I’m sure this sorting “alogrithm” has a name …)
  • Third loop:
    • The \votesOfTopCardinals macro will contain all number of votes that appear in the top list.
  • Check if the top number of votes (\votesForPlace1) is greater or equal than the amount needed. This will set \ifHabemusPapam.
  • Main loop (via \setStyleForVotes):
    • If someone has more than two thirds of the votes, he will be assigned style papa.
    • If no one has more than two thirds of the votes, it will be checked if the amount of votes exist in the top list. If yes, he will be assigned top, otherwise not papa.
    • The resulting TikZ node uses the selected style and the every name style.

Code

\documentclass{article}
\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}

\usepackage[top=1.5cm, bottom=1.5cm, left=1.5cm, right=1.5cm, landscape]{geometry}

\def\theenumi{\Roman{enumi}}

\usepackage{lmodern,datatool,multicol,xspace}
\usepackage{tikz}
\usetikzlibrary{calc,decorations}

\makeatletter
\pgfdeclaredecoration{penciline}{initial}{
  \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,
                  auto corner on length=1mm] {
    \pgfpathcurveto{
      \pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
                {\pgfdecorationsegmentamplitude}
    }{\pgfmathrand\pgfpointadd{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
                                         {0pt}}
      {\pgfqpoint{-\pgfdecorationsegmentaspect\pgfdecoratedinputsegmentremainingdistance}
        {\pgfmathresult\pgfdecorationsegmentamplitude}
      }
    }{\pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}}
  }
  \state{final}{}
}
\makeatother

\tikzset{
  prison/.style={
    append after command={
      \pgfextra
        \ifnum#1=0\else
          \foreach \x in {0,...,\number\numexpr#1-1\relax} {%
            \coordinate (mytemp)
              at ($(\tikzlastnode.north west)!0.3333*\x!(\tikzlastnode.north east)$);
            \draw[decorate] (mytemp) -- (mytemp |- \tikzlastnode.south);}%
        \fi
      \endpgfextra
    }
  },
  fived/.style={
    append after command={
      \pgfextra
        \draw[decorate] ([shift={(-2mm,1mm)}]\tikzlastnode.south west) --
                        ([shift={(2mm,-1mm)}]\tikzlastnode.north east);
      \endpgfextra
    }
  },
}

\newcommand{\drawvotes}[1]{%
  \begin{tikzpicture}[decoration=penciline]
    \def\numofscr{#1}
    \pgfmathtruncatemacro\residue{Mod(\numofscr,5)}
    \pgfmathtruncatemacro\numoflots{\numofscr/5}
    \ifnum\numofscr<5\else 
      \foreach \x in {1,...,\numoflots} \node[prison=4,fived] (a) at (\x,0) {};
    \fi
    \node[prison=\residue] at (\numoflots+1,0) {};
  \end{tikzpicture}%
}

\DTLloaddb{cardinals}{cardinals.csv}

\newif\ifHabemusPapam
\newcounter{sumVotes}
\newcounter{numberOfTopCardinals}

%%%%% Number of Cardinals that will be marked if no one reached two thirds %%%
\setcounter{numberOfTopCardinals}{5}% > 2                                  %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\foreach \i in {1,...,\value{numberOfTopCardinals}}{%
  \expandafter\gdef\csname votesForPlace\i\endcsname{0}%
}

\DTLforeach{cardinals}{\votes=Votes}{%
  \addtocounter{sumVotes}{\votes}%
  \foreach \i in {1,...,\value{numberOfTopCardinals}} {
    \ifnum\csname votesForPlace\i\endcsname=\votes\breakforeach\fi
    \ifnum\csname votesForPlace\i\endcsname<\votes
      \foreach \j in {\the\numexpr\value{numberOfTopCardinals}-1\relax,...,\i} {%
        \global\expandafter\let
        \csname votesForPlace\the\numexpr\j+1\relax\expandafter\endcsname
        \csname votesForPlace\j\endcsname
      }%
      \expandafter\xdef\csname votesForPlace\i\endcsname{\votes}%
      \breakforeach
    \fi
  }%
}
%\errmessage{\csname votesForPlace1\endcsname}% (debug)
\def\votesOfTopCardinals{}%
\def\votesOfPapa{10000}% = infinity
\foreach \i in {1,...,\value{numberOfTopCardinals}} {%
    \xdef\votesOfTopCardinals{\votesOfTopCardinals,\csname votesForPlace\i\endcsname,}%
}
%\errmessage{\votesOfTopCardinals}% (debug)
\pgfmathparse{2/3*\the\value{sumVotes}}
%\errmessage{\pgfmathresult}% (debug)
\ifdim\pgfmathresult pt>\csname votesForPlace1\endcsname pt
  \else\HabemusPapamtrue\fi

\makeatletter
\def\qrr@pgfutil@in@#1#2{% to save \expandafters
  \edef\pgf@tempa{{#1}{#2}}%
  \expandafter\pgfutil@in@\pgf@tempa}

\newcommand*{\setStyleForVotes}[2]{% #1 = votes
                                   % #2 = macro to save style
  \def\myStyle{not papa}%
  \ifHabemusPapam
    \ifnum\csname votesForPlace1\endcsname=#1\relax
      \def#2{papa}%
    \fi
  \else
    \qrr@pgfutil@in@{,#1,}\votesOfTopCardinals
    \ifpgfutil@in@
      \def#2{top}%
    \fi
  \fi
}
\makeatother

\tikzset{
  not papa/.style={draw=none, fill=none},
  papa/.style={draw=blue},
  top/.style={draw=red},
  expand me/.style={#1},
  every name/.style={
    font=\scshape,
    anchor=base,
    rounded corners,
  }
}
\begin{document}
\pagestyle{empty}
\begin{center}
{\Huge\textsc{Vota Scrutinii}}
\end{center}

\vspace{1em}
\begin{multicols}{2}
\begin{enumerate}
\DTLforeach{cardinals}{\surname=Surname,\name=Name,\votes=Votes}{%
  \setStyleForVotes\votes\myStyle
  \item \tikz[baseline]\node[expand me/.expand once=\myStyle,every name]{\surname};%
        \xspace\drawvotes{\votes}%
}%
\end{enumerate}
\end{multicols}
\end{document}

Output

enter image description here

Related Question