13

I've seen some package to draw chessboard on LaTeX such as skak and chessboard. However, after reading the documentation, they don't seem to support a change of the board size. I'm talking about the number of squares in the board, not the visual size.

If you're curious, I'm trying to represent solutions to the n-queens problem.

So a simple way to draw a chessboard of any size and some queens in it is what I'm looking for. Also, some way to write numbers on the side and some specific letters under the board would be great.

Edit : I used Mike Renfro's solution but I changed it a bit to get variables on the bottom label and remove the mover square:

\documentclass{article}
\usepackage{chessboard}
\storechessboardstyle{4x4}{maxfield=d4}
\begin{document}
    \def\mylabelformat{%
    {\makebox[0pt][c]{%
    {$x_\arabic{filelabel}$}}}}

    \begin{tabular}{cc}
        \chessboard[style=4x4,setwhite={Qa3,Qb1,Qc4, Qd2}, labelbottomformat=\mylabelformat, showmover=false]
        &  
        \chessboard[style=4x4,setwhite={Qa3,Qb1,Qc4, Qd2}, labelbottomformat=\mylabelformat, showmover=false]
    \end{tabular}
\end{document}

Solutions for 4-Queens

Caramdir
  • 89,023
  • 26
  • 255
  • 291
tmoisan
  • 233
  • I have never understood the point of the problem; there is only a 8-queens problem, just like there is only a 8x8 chessboard (there doesn't exist any other)./nitpick – morbusg Apr 08 '11 at 06:53
  • 2
    @morbusg: it is the profession of mathematicians to take a problem, and abstract away a lot of the boring real-life constraints to obtain a different problem to study. For instance, the Banach-Tarski theorem talks about dividing a sphere into pieces based essentially on the premise that it is composed of a continuous medium, which is also not true of real objects. So whenever you hear anyone talking about an n×n chessboard, recognize that they are speaking a dialect in which "chessboard" means "groblefink" (where a groblefink is a generalized chessboard). – Niel de Beaudrap Apr 08 '11 at 10:12
  • (Also: since when can you have 8 queens in chess, without having any kings on the board?) – Niel de Beaudrap Apr 08 '11 at 10:13
  • And I'm quite interested in the 73 queens on a torus problem... – Brent.Longborough Apr 08 '11 at 10:42
  • @Niel: Yeah, I was aware of everything you said, I was merely nitpicking on the word "chessboard" (of which there exists exactly one: 8x8). Replace the "groblefink" with "checkerboard" and it all makes sense. I couldn't understand what you were trying to say with your second comment. – morbusg Apr 08 '11 at 17:50
  • @morbusg: well, how can there "only be" an 8-queens problem (whether on a chess or checkerboard, as you prefer) unless you are somehow implying that an n-queens problem is only a valid problem if it coheres in some way to the standards of a game of chess, and that the only valid solution is n=8? I don't see how the 8-queens problem is especially coherent to the game of chess, in contrast e.g. to 9-queens or 73-queens-on-a-torus. The accident of an 8×8 board and constraints being based on the moves of a piece called the "queen" are the only chessy attributes even about the 8×8 instance. – Niel de Beaudrap Apr 09 '11 at 08:25
  • @Niel: Ah, now I see what you were getting at. With my original comment I was trying to tie-in the word "chessboard" with the "8-queens problem" (albeit the other way around). For a profession so inclined on using correct terms, the use of "chessboard" in "n-queens problem" is clearly wrong, don't you think? "Checkerboard", on the other hand, doesn't carry the 8x8 limitation and hence, IMO, would suit better. – morbusg Apr 09 '11 at 08:51
  • Does checkerboard not have the connotation of 8×8? Any board on which I've played checkers have been that size, and I'm not sure that any other size is standard. Unless you mean a "board with a 'checkerboard' pattern"; but then why not apply the same generalization to chessboards? Anyway, this is an excellent example of an off-topic dispute without an objectively correct answer, so I'll leave off for my part. – Niel de Beaudrap Apr 09 '11 at 10:52
  • Please don't add answers to questions. If you want to answer your own question, please add a new answer. – cabohah Feb 14 '24 at 10:33

2 Answers2

26

Adpated from chessboard manual (and edited per comments below):

\documentclass{article}
\usepackage{chessboard}
\storechessboardstyle{4x4}{maxfield=d4}
\begin{document}
\chessboard[style=4x4,setwhite={Qa1,Qd2},showmover=false]
\end{document}

4x4 board

Mike Renfro
  • 20,550
  • 6
    It is not necessary to load xskak or to call \newgame before the board. Also clearboard is not needed: \setwhite clears the board anyway (while addwhite would add more pieces to the board). I would also use showmover=false for this type of problems. – Ulrike Fischer Apr 08 '11 at 08:01
  • I am sorry to reopen this but @Mike using this code for a 6x6 chessboard does not work. The bottom (letter) axis stays on 4 (a,b,c,d) all the time; for example this is a 3x3:

    `\documentclass[10pt,a4paper]{article}

    \usepackage[T1]{fontenc} \usepackage{chessboard}

    \storechessboardstyle{3x3}{maxfield=d3}

    \begin{document}

    \chessboard[style=3x3,setwhite={Qa1,Qc2},showmover=false]

    \end{document}`

    – jtimz May 01 '14 at 14:43
  • I'll have to look in more detail later, but I think a d3 board would have letters a-d, and numbers 1-3. If you want a 6x6, I think that'd be an f6 board. – Mike Renfro May 01 '14 at 14:49
  • @MikeRenfro yep you are indeed correct I thought d was not for the letter actual range but a measure of "distance". My bad! thanks! – jtimz May 01 '14 at 14:54
1

Here is a quick solution, using nodes. You may of course adapt it to your needs. By changing the macro \boardsize, you can change the size of the board, as you wanted.

\begin{tikzpicture}
    \pgfmathsetmacro{\boardsize}{4}

    \def\letters{{"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}}

    \foreach \i in {1,...,\boardsize}{
        \foreach \j in {1,...,\boardsize}{
            \pgfmathsetmacro{\weight}{(1 + (-1)^(\i+\j))*50};
            \node[rectangle,fill=gray!\weight,minimum size=1cm] (node\i-\j) at (\i,\j) {};
        }
    }
    \foreach \j in {1,...,\boardsize}{
        node[left=2mm of node1-\j] {\j};
    }

    \foreach \i in {1,...,\boardsize}{
        \node[below=4mm of node\i-1,anchor=base] {\pgfmathparse{\letters[\i]}\pgfmathresult};
    }

    \end{tikzpicture}
Frédéric
  • 11,087
  • 3
  • 37
  • 43