# How to optimize pdf with many links and simple pages

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}
\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\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}

• I should note that the code is an ugly mixture of TeX and LaTeX2e. One reason I didn't use just plain TeX is that hyperref is specific to LaTeX. One reason I didn't use proper LaTeX is that \begin{tabular}{c|c|c}...&...&...\\\hline...&...&...\\\hline...&...&...\end{tabular} does a lot more work than necessary, and for 6046 pages, it adds up. Also, I'm not good with box manipulations in LaTeX. Jun 26, 2014 at 5:33
• This is amazing. Jun 26, 2014 at 9:22
• Could you do the same for chess, or even better, the game of go? :p You need to add a "Player so-and-so wins!" message and a "Play again?" link. It "runs" pretty smoothly in Preview on my 2010 Macbook. What machine/OS are you using? You could decrease the number of links by getting rid of those on boxes already filled. Jun 26, 2014 at 10:50
• Like HTML, PDF is actually ASCII and certain editors will read it as such. Looking at your PDF, it consists almost entirely of links. Each link has a border, annotation, etc. There isn't a whole lot you can do to speed up that process. Jun 26, 2014 at 16:53
• Adobe Acrobat has a "Reduce file size" feature which cut the size of this PDF in half. Jun 26, 2014 at 18:29