28

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}
12
  • 2
    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
  • 3
    This is amazing.
    – Clément
    Jun 26, 2014 at 9:22
  • 1
    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.
    – jub0bs
    Jun 26, 2014 at 10:50
  • 1
    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
  • 2
    @Jubobs there is a limit on the total number of pages of a pdf file, so no, chess is out. Jun 27, 2014 at 3:37

2 Answers 2

4

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.

1
  • 2
    Please consider adding some actual code to indicate what your advice is based on.
    – Mico
    Nov 4, 2014 at 19:43
3

I translated your tex-file to pdf by using pdflatex. I obtained a pdf-document of 6046 pages. This is the correct number of boards that contain an equal number of Os and Xes, plus the number of boards that contain one more X than Os ( because it is X that starts the game ). You may easily obtain the number 6046 by adding up products of the appropriate binomial coefficients. However, some of the hyperlinks are superfluous, because they would never be used in a real game. For example, page 3113 of the pdf shows a board with 3 Xes and 2 Os, like this : X X X \ - O - \ O - - ; a situation where X has won the game. Then this particular page should not contain any hyperlinks, because the game may be considered over. But page 3113 of the produced pdf-file still has 7 hyperlinks, as can easily be seen by clicking on each of the 9 boxes : 7 of the 9 possible clicks will send you to a different page. Hence, my conclusion is, that by improving the used algorithm, you may be able to reduce the number of hyperlinks and hence the size of the pdf-file. How to improve the algorithm would be a problem for further study.

2
  • True, but that is basically what @Zuerill suggests.
    – quinmars
    Nov 5, 2014 at 22:42
  • @quinmars My suggestion was to eliminate links that let you continue playing after one of the players has won. Eliminating some of the pages is another, but different idea. Remember that the board is full when the fifth cross is set. Which pages with 4 crosses is it that you suggest might be eliminated?
    – user35145
    Nov 5, 2014 at 23:11

You must log in to answer this question.

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