2

How to draw a chessboard filled with numbers, as the following figure?

wanted

I've tried using the chessboard package but to no success.

benedito
  • 4,600

5 Answers5

14
\documentclass{report}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=1.25]
\foreach \x in {1,2,...,8}
\foreach \y in {1,2,...,8}
{
\draw (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);
\draw (\x,\y) node{\small (\y,\x)};
}
\foreach \x in {1,2,...,8}{
\node at (\x,0) {\x};
\node at (0,\x) {\x};
}
\end{tikzpicture}
\end{document}

enter image description here

Sigur
  • 37,330
11

If you really want to use a chessboard...

chess board

\documentclass{article}
\usepackage{chessfss}

\setlength{\parindent}{0in}

\newlength{\symsize}\setlength{\symsize}{24pt}
\newlength{\boardwidth}\setlength{\boardwidth}{8\symsize}
\setboardfontsize{\symsize}

\newcommand{\board}[1]{\setlength{\fboxsep}{0pt}%
\fbox{\parbox{\boardwidth}{\setlength{\baselineskip}{\symsize}#1}}}
\newcommand{\row}[1]{\parbox[c][\symsize]{\symsize}{\hfill{#1}}}
\newcommand{\col}[1]{\parbox[b]{\symsize}{\hfil{#1}}}
\newcommand{\chessboard}[1]{\begin{tabular}{cc}
\parbox{\symsize}{\setlength{\baselineskip}{\symsize}
\row{8} \row{7} \row{6} \row{5} \row{4} \row{3} \row{2} \row{1}}&\board{#1}\\
\row{ } &\mbox{\col{a}\col{b}\col{c}\col{d}\col{e}\col{f}\col{g}\col{h}}
\end{tabular}}

\newcommand{\TextOnWhite}[1]{\WhiteEmptySquare\hspace{-\symsize}%
\raisebox{.35\symsize}{\makebox[\symsize][c]{\small #1}}}
\newcommand{\TextOnBlack}[1]{\BlackEmptySquare\hspace{-\symsize}%
\raisebox{.35\symsize}{\makebox[\symsize][c]{\small #1}}}

\begin{document}
\large

\begin{center}\chessboard{
\TextOnWhite{(8,1)}\TextOnBlack{(8,2)}\TextOnWhite{(8,3)}\TextOnBlack{(8,4)}%
\TextOnWhite{(8,5)}\TextOnBlack{(8,6)}\TextOnWhite{(8,7)}\TextOnBlack{(8,8)}\\
\TextOnBlack{(7,1)}\TextOnWhite{(7,2)}\TextOnBlack{(7,3)}\TextOnWhite{(7,4)}%
\TextOnBlack{(7,5)}\TextOnWhite{(7,6)}\TextOnBlack{(7,7)}\TextOnWhite{(7,8)}\\
\TextOnWhite{(6,1)}\TextOnBlack{(6,2)}\TextOnWhite{(6,3)}\TextOnBlack{(6,4)}%
\TextOnWhite{(6,5)}\TextOnBlack{(6,6)}\TextOnWhite{(6,7)}\TextOnBlack{(6,8)}\\
\TextOnBlack{(5,1)}\TextOnWhite{(5,2)}\TextOnBlack{(5,3)}\TextOnWhite{(5,4)}%
\TextOnBlack{(5,5)}\TextOnWhite{(5,6)}\TextOnBlack{(5,7)}\TextOnWhite{(5,8)}\\
\TextOnWhite{(4,1)}\TextOnBlack{(4,2)}\TextOnWhite{(4,3)}\TextOnBlack{(4,4)}%
\TextOnWhite{(4,5)}\TextOnBlack{(4,6)}\TextOnWhite{(4,7)}\TextOnBlack{(4,8)}\\
\TextOnBlack{(3,1)}\TextOnWhite{(3,2)}\TextOnBlack{(3,3)}\TextOnWhite{(3,4)}%
\TextOnBlack{(3,5)}\TextOnWhite{(3,6)}\TextOnBlack{(3,7)}\TextOnWhite{(3,8)}\\
\TextOnWhite{(2,1)}\TextOnBlack{(2,2)}\TextOnWhite{(2,3)}\TextOnBlack{(2,4)}%
\TextOnWhite{(2,5)}\TextOnBlack{(2,6)}\TextOnWhite{(2,7)}\TextOnBlack{(2,8)}\\
\TextOnBlack{(1,1)}\TextOnWhite{(1,2)}\TextOnBlack{(1,3)}\TextOnWhite{(1,4)}%
\TextOnBlack{(1,5)}\TextOnWhite{(1,6)}\TextOnBlack{(1,7)}\TextOnWhite{(1,8)}
}\end{center}

\end{document}

Overlaying the text on top of \WhiteEmptySquare is sort of wasteful but the chessfss package sometimes uses two different colors, not just black and white.

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
8

A MetaPost solution, drawing something like a real chessboard, for the fun:

input latexmp; setupLaTeXMP(textextlabel = enable, mode = rerun);
u := 1.5cm; 
n := 8;
picture mypoint;
beginfig(1);
  for i = 1 upto n:
    for j = 1 upto n:
      mypoint := thelabel("$(" & decimal j & "," & decimal i & ")$", u*(i-.5, j-.5));
      if (i+j) mod 2 = 0:
        fill ((i-1, j-1) -- (i, j-1) -- (i, j) -- (i-1, j) -- cycle) scaled u;
        draw mypoint withcolor white;
      else: draw mypoint;
      fi;
    endfor;
    label.bot("$" & decimal i & "$", u*(i-.5, 0));
    label.lft("$" & decimal i & "$", u*(0, i-.5));
  endfor;
  draw (origin -- (n, 0) -- (n, n) -- (0, n) -- cycle) scaled u;
  setbounds currentpicture to boundingbox currentpicture enlarged 2bp;
endfig;
end.

If interested, typeset it with the Metafun format. For example, to obtain a PDF version directly, you can enter this in a command line:

mptopdf -metafun -latex chessboard.mp

The result is:

enter image description here

Franck Pastor
  • 18,756
2

A PSTricks solution using the xfp package:

\documentclass{article}

\usepackage{multido} \usepackage{pstricks} \psset{dimen = m} \usepackage{xfp}

% Constants. \newcommand\shift[1]{\fpeval{#1-0.5}} \newcommand\size[1]{\fpeval{\squaresize#1}} \newcommand\maximum{\fpeval{\squaresize*\boardsize}}

% Parameters. \def\squaresize{1} \def\boardsize{8}

\begin{document}

\begin{pspicture}(-0.3,-0.4)(\maximum,\maximum) \multido{\iB = 1+1}{\boardsize}{% \uput180{$\iB$} \uput270{$\iB$} \multido{\iA = 1+1}{\boardsize}{% \psframe(\size{\iA-1},\size{\iB-1})(\size{\iA},\size{\iB}) \rput(\size{\shift{\iB}},\size{\shift{\iA}}){$(\iA,\iB)$}}} \end{pspicture}

\end{document}

output

All you have to do is choose the value of \squaresize and \boardsize.

2

Just another much better solution with PSTricks.

\documentclass[pstricks,border=24pt]{standalone}
\usepackage{multido}
\begin{document}
\begin{pspicture}[dimen=m](8,8)
    \multido{\ix=1+1,\ic=97+1}{8}
    {% <--- I really don't understand why we need to append % here right before \pstVerb.
        \pstVerb{/hx \ix\space .5 sub def}
        \uput[d](!hx 0){$\char\ic\mathstrut$}
        \uput[l](!0 hx){$\ix\mathstrut$}
        \multido{\iy=1+1,\io=\ix+1}{8}
        {       
            \psframe[origin={\ix,\iy},fillstyle=\ifodd\io solid\else vlines\fi,hatchsep=.5pt,hatchcolor=lightgray](-1,-1)
            \rput(!hx \iy\space .5 sub){$(\iy,\ix)$}
        }
    }
\end{pspicture}
\end{document}

enter image description here

  • I can't compile it. Appears the errors: Line 15 - Missing number, treated as zero and Line 15 -Illegal unit measure (pt inserted) – benedito Jul 10 '14 at 08:15
  • @benedito: Compile it with latex-dvips-ps2pdf sequence or xelatex. – kiss my armpit Jul 10 '14 at 10:35
  • I tried - To see the message that appears : Miktex Problem Report:Windows API error 2 The system cannot find the file specified.(before I saved archive) – benedito Jul 10 '14 at 11:29