# How to render a Futoshiki puzzle?

I'd like to have a Futoshiki puzzle in a document. It is a game similar to Sudoku, with the added information that there are signs between some adjacent cells that indicate which should contain a greater number.

A Futoshiki puzzle is a simple square grid with a few numbers in it (all cells will contain one number in a solved puzzle). The complication is that there are ">" signs between some adjecent cells that specify which of the two numbers has to be greater. The signs can appear between two cells that are adjacent horizontally or vertically. Traditionally, the cells are drawn separated from each other, with white space (gutters) between them, where the ">" signs are drawn. Here's an example from the above source/link for a 5x5 puzzle (unfortunately it does not have any vertical inequality signs. They are needed too):

...and its accompanying solution:

The implementation could be anything practical. I first thought of a tabular environment, but had no idea how to have gutters and write in them.

• Welcome to the TeX.SE. community. I think that to generate this matrix (puzzle) it is necessary or Tikz-pgf or pstriks or Asymptote....or with another strategies. Nov 8, 2021 at 17:01
• The first thing to do is to propose a way to indicate this in a LaTeX code, In other words, you must propose a small DSL. Nov 8, 2021 at 17:36

## 4 Answers

You can use the tikz-cd package, which also produces a matrix of nodes (as in Jasper Habicht's answer), but allows you to define arrows that consist of glyphs. So the greater signs can be created via arrows, which you can set inside of the diagram, e.g. \arrow[r] indicates that the current cell is larger than the one to its right. For your convince the arrow command is stored in the \mygreater macro.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}
\tikzcdset{Futoshiki/.style={nodes in empty cells,
row sep=1.2em,column sep=1.2em,
every arrow/.append style={-greater,shorten >=0.3em,shorten <=0.3em},
cells={nodes={draw,minimum size=2.4em,text depth=0.4em,
text height=1.2em,
anchor=center}}},
greater/.tip={Glyph[glyph math command=mygreater, glyph length=1.5ex]}}
\newcommand{\mygreater}{>}
\begin{document}

\begin{tikzcd}[Futoshiki]
\arrow[r]    & \arrow[r]  & \arrow[r]    & \arrow[r]  &              \\
\boldsymbol4 &            &              &            & \boldsymbol2 \\
&            & \boldsymbol4 &            &              \\
&            &              &            & \boldsymbol4 \\
&            &              &            &
\end{tikzcd}
\end{document}


• How would you create vertical inequalities between the nodes?
– Werner
Nov 8, 2021 at 19:21
• @Werner With \arrow[d] or \arrow[u] if the cell below or above is smaller, respectively.
– user255043
Nov 8, 2021 at 19:22

You could make use of a TikZ matrix of nodes:

\documentclass[border=1mm, tikz]{standalone}

\usetikzlibrary{matrix, calc}

\begin{document}

\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells,
row sep=1em, column sep=1em,
nodes={draw, minimum width=2em, minimum height=2em, inner sep=0pt, anchor=center}] {
&            &            &            &            \\
\textbf{4} &            &            &            & \textbf{2} \\
&            & \textbf{4} &            &            \\
&            &            &            & \textbf{4} \\
&            &            &            &            \\
};
\foreach \x/\y in {1-1/1-2, 1-3/1-4, 1-4/1-5} {
\node at ($(m-\x)!0.5!(m-\y)$) {$>$};
}
\foreach \x/\y in {4-5/4-4, 5-3/5-2, 5-2/5-1} {
\node at ($(m-\x)!0.5!(m-\y)$) {$<$};
}
\end{tikzpicture}

\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells,
row sep=1em, column sep=1em,
nodes={draw, minimum width=2em, minimum height=2em, inner sep=0pt, anchor=center}] {
5          & 4          & 3          & 2         & 1          \\
\textbf{4} & 3          & 1          & 5         & \textbf{2} \\
2          & 1          & \textbf{4} & 3         & 5          \\
3          & 5          & 2          & 1         & \textbf{4} \\
1          & 2          & 5          & 4         & 3          \\
};
\foreach \x/\y in {1-1/1-2, 1-3/1-4, 1-4/1-5} {
\node at ($(m-\x)!0.5!(m-\y)$) {$>$};
}
\foreach \x/\y in {4-5/4-4, 5-3/5-2, 5-2/5-1} {
\node at ($(m-\x)!0.5!(m-\y)$) {$<$};
}
\end{tikzpicture}

\end{document}


If vertically aligned < and > signs are needed, you can just rotate the nodes by 90 degrees: \node[rotate=90] at ($(m-1-1)!0.5!(m-2-1)$) {$>$};

You can use picture mode here, no packages are needed.

\documentclass{article}

\begin{document}

\fbox{\large\begin{picture}(180,180)

% row 1
\put(00,160){\framebox(20,20){5}}
\put(30,160){\makebox(0,20){\boldmath$>$}}
\put(40,160){\framebox(20,20){4}}
\put(70,160){\makebox(0,20){\boldmath}}
\put(80,160){\framebox(20,20){3}}
\put(110,160){\makebox(0,20){\boldmath$>$}}
\put(120,160){\framebox(20,20){2}}
\put(150,160){\makebox(0,20){\boldmath$>$}}
\put(160,160){\framebox(20,20){1}}

% row 2
\put(00,120){\framebox(20,20){4}}
\put(30,120){\makebox(0,20){\boldmath}}
\put(40,120){\framebox(20,20){3}}
\put(70,120){\makebox(0,20){\boldmath}}
\put(80,120){\framebox(20,20){1}}
\put(110,120){\makebox(0,20){\boldmath}}
\put(120,120){\framebox(20,20){5}}
\put(150,120){\makebox(0,20){\boldmath}}
\put(160,120){\framebox(20,20){2}}

% row 3
\put(00,80){\framebox(20,20){2}}
\put(30,80){\makebox(0,20){\boldmath}}
\put(40,80){\framebox(20,20){1}}
\put(70,80){\makebox(0,20){\boldmath}}
\put(80,80){\framebox(20,20){4}}
\put(120,80){\makebox(0,20){\boldmath}}
\put(120,80){\framebox(20,20){3}}
\put(150,80){\makebox(0,20){\boldmath}}
\put(160,80){\framebox(20,20){5}}

% row 4
\put(00,40){\framebox(20,20){3}}
\put(30,40){\makebox(0,20){\boldmath}}
\put(40,40){\framebox(20,20){5}}
\put(70,40){\makebox(0,20){\boldmath}}
\put(80,40){\framebox(20,20){2}}
\put(110,40){\makebox(0,20){\boldmath}}
\put(120,40){\framebox(20,20){1}}
\put(150,40){\makebox(0,20){\boldmath$<$}}
\put(160,40){\framebox(20,20){4}}

% between row 4 and 5
\put(80,20){\makebox(20,20){\boldmath$<$}}

% row 5
\put(00,0){\framebox(20,20){1}}
\put(30,0){\makebox(0,20){\boldmath$<$}}
\put(40,0){\framebox(20,20){2}}
\put(70,0){\makebox(0,20){\boldmath$<$}}
\put(80,0){\framebox(20,20){5}}
\put(110,0){\makebox(0,20){\boldmath}}
\put(120,0){\framebox(20,20){4}}
\put(150,0){\makebox(0,20){\boldmath}}
\put(160,0){\framebox(20,20){3}}
\end{picture}}
\end{document}

• I would prefer \vee or \wedge for the vertical inequality. Nov 8, 2021 at 19:19
• @Teepeemm I'd have used \rotatebox{90}{$<$} but I wasn't sure if a rotated one was wanted, the question didn't have an example. Nov 8, 2021 at 19:29

This is a possible (simple) idea using only fancyvrb package with the option {Verbatim}[commandchars=\\\{\}] to have characters in bold.

\documentclass[12pt]{article}
\usepackage{fancyvrb}

\begin{document}
\begin{Verbatim}[commandchars=\\\{\}]
|. | 2 | . | . | .|
|- - - - v - - - -|
|. > . | . | . | .|
|- - - - - - - - -|
|. | . < \textbf{4} | . | .|
|- - - - v - - - -|
|. | . | . | . | .|
|^ - - - > - - - -|
|. | . | 5 | . | .|
\end{Verbatim}
\end{document}


• @DavidCarlisle Hi. I had tried with a LaTeX editor called Papeeria but it wouldn't compile. Now I'll provide it. Thanks Nov 8, 2021 at 19:51
• @DavidCarlisle Many times it not compiles probably for your server. I thought an error for missing package. Nov 8, 2021 at 20:17