A LaTeX style file for typesetting logic puzzles

User Tools

Site Tools



The puzzle

Try to find the positions of the ships listed below the puzzle. The numbers on the side of the puzzle reveals how many ship segments can be found in the rows and columns. All remaining fields indicate ’water’. Consider the following rules: The ships are arranged horizontally and vertically. No ship touches another ship at any point, not even diagonally. Here’s a little self-explanatory example:

battleship game



  • rows (5) defines the number of rows in the grid

  • columns (5) specifies the number of columns in the grid

  • width (6cm) sets the width of the minipage, in which the grid is typeset

  • scale (1) scales the size of the grid in the minipage

  • fontsize (Large) specifies the size of the numbers next to the grid. Here, the usual LaTeX sizes are used. Possible values: tiny, scriptsize, footnotesize, small, normalsize, large, Large, LARGE, huge, Huge

  • title () sets the title of a puzzle

  • titleindent (0.75cm) defines the indent of the title

  • titlewidth (5.15cm) specifies the width of the box the title is set in

  • sbindent (0.75cm) defines the indent of the ship box below the grid

  • sbwidth (5.15cm) specifies the width of the minipage, in which the ships are typeset

  • sbshipscale (1) scales the size of the ships in the ship box

  • shipcolor (green) sets the color of the ship segments

  • bgcolor () sets the background color of the grid

  • counterstyle (none) defines the counter style. Predefined styles: none, left, right

  • cvoffset (-38pt) sets the vertical offset of the counters in the margin





The environment is the central core of the style file. With the optional argument of the environment, you can reset the options with local scope. Here, a blank grid is created that you can fill with ships using other commands.


Ships, water, islands, ...



With the command \placeship you can place complete ships in the grid. It expects the specification of the {direction} as horizontal (H) or vertical (V). Furthermore, it requires the starting coordinates {column}{row} and the {length} of the ship.


\placesegment{column}{row}{ship segment}

The command \placesegment is used for the placement of ship segments in the grid. In the mandatory argument {ship segment}, you can use the following commands:

  • \Ship
  • \ShipC
  • \ShipB
  • \ShipT
  • \ShipL
  • \ShipR


The command \ship was replaced by the command \placesegment. The command is deprecated and should not be used longer. It may still be used, but it is not recommended.



With the command \placewater you can place water markers in the grid.



With the command \placeisland you can place islands in the grid. The island outlines are created randomly.



\shipH{csv list}

The command \shipH typesets the horizontal numbers above the grid. It expects a comma-separated list as an argument.


\shipV{csv list}

The command \shipV typesets the vertical numbers beside the grid. It also expects a comma separated list.


\shipbox{csv list}

The command \shipbox defines the number and size of the ships, which are typeset under the grid.


The command allows you to define your own styles. For example, the style left is defined as follows:

  \tikz\node[shape=rectangle,fill=yellow!40,inner sep=7pt,
             draw,rounded corners=3pt,thick]

To typeset the counter into the margin we use the command \marginnote. We need to use the command \reversemarginpar to set the counter into the left margin. Of course, we must use this command in a group for local scope. Finally we use \puzzlecounter in a \tikz node with a vertical offset set with the option cvoffset.



The command provides the counter in textual form to use it for example in \definecounterstyle.



With the command \titleformat, you can define the format of the title. By default, the definition is as follows:





With the command \battleshipsetup you can reset the options with global scope.


\classicgame{csv list}

The command \classicgame typesets a game sheet for playing classic Battleship. It expects a comma separated list with the number and sizes of the ships.



With the command \setpuzzlecounter, you can reset the puzzle counter, for example before the solutions.

Examples & Solutions

You can download application examples and their solutions from the project page. The puzzles are originally licensed under CC-BY-NC-SA.

You can also download a game sheet for playing the classic Battleship.


battleship.txt · Last modified: 2015/09/06 09:51 (external edit)