Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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 8 '11 at 6: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 8 '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 8 '11 at 10:13
    
And I'm quite interested in the 73 queens on a torus problem... –  Brent.Longborough Apr 8 '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 8 '11 at 17:50

2 Answers 2

up vote 20 down vote accepted

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

share|improve this answer
5  
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 8 '11 at 8: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 1 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 1 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 1 at 14:54

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.