[Tex/LaTex] How to optimize pdf with many links and simple pages

file sizefunperformance

So… I can play tic-tac-toe (naughts and crosses) in a pdf: each board configuration is one page, and each cell of the board is a link which goes to the page corresponding to playing that move. The resulting pdf file contains 6046 pages and is 3MB large. My pdf viewer is a bit slow to display it.

What are ways I could reduce the size of such a pdf, which has a large number of simple pages and about 50000 links? I am also interested in ways to speed up compilation.

(To produce the pdf, run pdflatex on the following.)

\documentclass[12pt]{article}
\usepackage[paperwidth=48pt,paperheight=48pt,margin=0pt]{geometry}
\usepackage[hidelinks]{hyperref}
\newcount\0 \newcount\1 \newcount\2
\newcount\3 \newcount\4 \newcount\5
\newcount\6 \newcount\7 \newcount\8
\newcount\p
\def\here{\the\numexpr\0+3*(\1+3*(\2+3*(\3+3*(\4+3*(\5+3*(\6+3*(\7+3*\8)))))))}
\def\cell#1#2{\ifnum#1=\p\c#1\else\hyperlink{\here+\p*#2}{\c#1}\fi}
\def\c#1{\hbox to 14.5pt{\hfil\ifcase#1\or x\else o\fi\hfil}}
\def\|{\hskip-.2pt\vrule width.4pt\hskip-.2pt}%
\def\-{\noalign{\hrule height.4pt}}%
\def\mk#1{\p#1
  \hypertarget{\here}{%
    \hbox to 48pt{\hfil\vbox to 48pt{\vfil
        \everycr{}\tabskip 0pt\relax
        \halign{&\strut##\cr
          \cell\0  {1}\|&\cell\1   {3}\|&\cell\2   {9}\cr\-%
          \cell\3 {27}\|&\cell\4  {81}\|&\cell\5 {243}\cr\-%
          \cell\6{729}\|&\cell\7{2187}\|&\cell\8{6561}\cr}%
        \vfil}\hfil}}\newpage}
\begin{document}
\def\ter#1#2{#1=0 #2\relax #1=1 #2\relax #1=-1 #2\relax}
\ter\0{\ter\1{\ter\2{\ter\3{\ter\4{\ter\5{\ter\6{\ter\7{\ter\8{%
  \ifcase\numexpr\0+\1+\2+\3+\4+\5+\6+\7+\8\relax\mk{1}\or\mk{-1}\fi}}}}}}}}}
\end{document}

Best Answer

One way to get less pages would be if you eliminate all the ones with more than 1 row/column/diagonal of X's and O's, since you wouldn't reach those when you're properly playing tic tac toe.

Probably takes even longer to compile though.

Related Question