You're reading: Travels in a Mathematical World

Introducing hexboard – a LaTeX package for drawing games of Hex

Chris Sangwin and I wrote a LaTeX package for drawing Hex boards and games called hexboard. It can produce diagrams like this.

Hex board with counters.

First: why? Then: how do you use it?

Why?

Recently I noticed Chris Sangwin’s book review of Hex: The Full Story in AMS Notices had some nice-looking Hex diagrams.

Hex: The Full Story first page, including diagram of Hex board.

I have been struggling to find a nice way to represent Hex board in my classes for our final year module Game Theory and Recreational Mathematics. I show Hex as an example of a combinatorial game (while mostly talking about Nim), and later use a 2×2 Hex game for a simple Minimax game tree search exercise for students (while mostly covering Noughts and Crosses). There are two LaTeX packages I’m aware of, havannah and hexgame – neither of which quite look right to me. What really appealed about Chris’ diagrams is that they look quite similar to the Hex board in our Maths Arcade, which came from Nestor Games.

I asked Chris what he’d used to draw his diagrams. It turns out he’d written his own code to draw these, which he shared with me. Unfortunately it was written in PSTricks, which I don’t know, but Chris mentioned updating this to use TikZ, which I do. So I remade the code in TikZ and added a few bits. I was aiming to get to the point where the code would do everything in Chris’ intro to Hex worksheet and everything I needed for my class.

Anyway, our new package has led to what I consider to be an improvement in my Hex materials this year.

Hex diagrams using havannah (left) and hexboard (right).

Getting started

First, do you have hexboard installed? Make a LaTeX file with \usepackage{hexboard} in the preamble and see if it runs. If you use MiKTeX, it should be that compiling a document that uses the hexboard package will install it for you. If you use TeXLive, you may need to update packages. On my Ubuntu machine with TeXLive 2021 installed manually, I ran tlmgr install hexboard and it installed. As a last resort, you can download the package from CTAN, unzip the contents and run latex hexboard.ins which will create hexboard.sty. Then put hexboard.sty in the same folder as your LaTeX file.

To use the package, include in your preamble \usepackage{hexboard}.

Basically, Hex diagrams exist in a hexpicture environment. Here is a simple example. It just draws a blank Hex game board.

\begin{hexpicture}
\hexboard{11}
\end{hexpicture}

The output should look like this:

11 by 11 Hex board

You can place various objects on a board, including counters, dots, lines and general LaTeX code (e.g. text). Here’s a more elaborate example.

\begin{hexpicture}
\hexboard{3}
\hexcounter{b}{1}{A}
\hexcellshaded{b}{2}
\hexdot{a}{2}
\hexdot{b}{2}
\hexconnect{b}{1}{a}{2}
\hexconnect{b}{1}{b}{2}
\hexcontent{a}{3}{x}
\end{hexpicture}

Here \hexcounter{b}{1}{A} draws a counter for player A in cell b1, \hexdot{a}{2} draws a dot in cell a2, \hexconnect{b}{1}{a}{2} draws a line from cell b1 to cell a2, \hexcellshaded{b}{2} shades cell b2, and \hexcontent{a}{3}{x} puts an x in cell a3. This should appear like this:

Hex board with red counter, dots, lines, a letter x and a shaded cell.

There is also an environment to display games in progress. Actually two – hexgame and hexgamelabels. In these, you specify moves in a game and the package keeps track of whose turn it is and colours the counters accordingly. The difference is that hexgamelabels puts a number on each counter to show when it was played. Here is an example of hexgamelabels.

\begin{hexgamelabels}[5]
\hexmove{c}{2}
\hexmove{b}{4}
\hexmove{b}{3}
\hexmove{d}{3}
\hexmove{a}{5}
\hexmove{a}{4}
\hexmove{c}{3}
\hexmove{c}{4}
\hexmove{e}{3}
\hexmove{e}{2}
\end{hexgamelabels}

This should look like this:

Hex game with moves labelled.

There is some customisation (including size and colours), and other options such as drawing partial board diagrams (and the news that secretly hexpicture is a type of tikzpicture). Much more on the package can be found in a series of examples in the documentation.

The code and a place to log issues are available as hexboard on GitHub.

(will not be published)

$\LaTeX$: You can use LaTeX in your comments. e.g. $ e^{\pi i} $ for inline maths; \[ e^{\pi i} \] for display-mode (on its own line) maths.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>