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.

What is the best way to draw checker board in tikz? I'm interested in what is the best way to draw many circular pieces on board. (the checker background is not important)

share|improve this question
add comment

4 Answers

up vote 11 down vote accepted

I suggest to use a matrix of nodes from the tikz matrix library.

A small example using the style devloped in the other topic:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\tikzstyle{ball} = [circle, shading=ball,
    ball color=black!80!white, minimum size=1cm]
\begin{tikzpicture}
\matrix (m) [matrix of nodes,nodes=ball] {
 {} &    & {} &    & {} &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
 {} &    & {} &    & {} &    & {} & \\};
\end{tikzpicture}
\end{document}

Output:

alt text

share|improve this answer
    
You are invaluable help. Big thanks ! –  Łukasz Lew Aug 17 '10 at 17:51
    
Is there a way to perfectly align a grid to this matrix of notes? –  Łukasz Lew Aug 17 '10 at 18:14
1  
You could refer to the matrix nodes for alignment, like (m-2-3). But in comments it's hard to discuss, perhaps create a new question with a good description what shall be achieved. –  Stefan Kottwitz Aug 17 '10 at 18:20
    
I think this will be enough, thanks. If I fail I will ask :) –  Łukasz Lew Aug 17 '10 at 18:23
add comment

The difficulty with Caramdir's approach is that it is too regular so it would be hard, for example, to display the layout of the board in an actual game. Stefan's is better at that, but doesn't have the grid. However, a grid is very regular so Caramdir's approach can be adapted to that.

So here's my version which is an amalgamation of the two. First, the result:

checker board

Some things to note:

  1. These are meant to be checkers pieces, after all!
  2. Using the "between origins" option in the matrix spacing means that the checkers (aka nodes) are placed very precisely on the squares.
  3. As in Stefan's answer, the syntax for actually placing the checkers on their squares is very intuitive.
  4. The only thing left is a simple syntax for changing the colour of the pieces!
\documentclass{article} 
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes.geometric}

\begin{document}
\tikzstyle{checker} = [cylinder, minimum width=.8cm, 
 shape border rotate=90,cylinder end fill=gray!50!white,
 cylinder body fill=gray, cylinder uses custom fill]
\begin{tikzpicture}
\draw[ultra thick] (0,0) rectangle (8,-8);
    \foreach \row in {0,1, ..., 7} {
        \foreach \column in {0, ..., 3} {
    \fill ({2*\column + mod(\row,2)}, -\row) rectangle +(1,-1);
        }
    }
\matrix (m) at (0,0) [matrix of nodes,nodes=checker,
  anchor=north west,column sep={1cm,between origins},
  row sep={1cm,between origins}] {
 {} & {} & {} &    & {} &    & {} & \\
    &    &    & {} & {} & {} &    & {} \\
    & {} & {} &    &    &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
 {} &    &    & {} & {} &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
    &    & {} &    &    &    & {} & \\
    &    &    & {} & {} & {} &    & {} \\
};
\end{tikzpicture}
\end{document}

Update 2012-03-09: As this has just returned to the front page, I decided to see if I could fix the colours and the "kings". Thanks to Ryan Reich's trace-pgfkeys package, I figured out a way to set the colours and size.

Here's the new code; I've slightly cleaned up the original and added the colour checking. The complicated bit (which Ryan's code was invaluable for) was figuring out how to set the colour from any ambient colour that might have been set.

\documentclass{article} 
%\url{http://tex.stackexchange.com/a/1911/86}
\usepackage{tikz}
%\usepackage{trace-pgfkeys}
\usetikzlibrary{matrix,shapes.geometric}

\makeatletter
\colorlet{checkertint}{gray!70!white}
\tikzset{
  checker/.style={
    draw,
    cylinder,
    minimum width=.8cm, 
    shape border rotate=90,
    set checker colour=#1,
    cylinder uses custom fill,
  },
  king/.style={
    minimum height=.8cm,
  },
  checker default colour/.initial=gray!50!white,
  set checker colour/.code={%
    \tikz@addoption{%
      \def\ch@color{#1}%
      \def\@tempa{\pgfkeysnovalue}%
      \ifx\ch@color\@tempa
       \ifx\tikz@fillcolor\pgfutil@empty
        \ifx\tikz@strokecolor\pgfutil@empty
         \ifx\tikz@textcolor\pgfutil@empty
          \pgfkeysgetvalue{/tikz/checker default colour}{\ch@color}%
         \else
          \let\ch@color\tikz@textcolor
         \fi
        \else
         \let\ch@color\tikz@strokecolor
        \fi
       \else
        \let\ch@color\tikz@fillcolor
       \fi
      \fi
      \pgfsetstrokecolor{\ch@color!50!checkertint}%
      \pgfkeys{
       /tikz/cylinder end fill=\ch@color!80!checkertint,
       /tikz/cylinder body fill=\ch@color!50!checkertint
      }
    }
  }
}
\makeatother
\begin{document}

\begin{tikzpicture}
\draw[ultra thick] (0,0) rectangle (8,-8);
    \foreach \row in {0,1, ..., 7} {
        \foreach \column in {0, ..., 3} {
    \fill ({2*\column + mod(\row,2)}, -\row) rectangle +(1,-1);
        }
    }
\matrix (m) at (0,0) [
  matrix of nodes,
  checker default colour=white,
  nodes=checker,
  anchor=north west,
  column sep={1cm,between origins},
  row sep={1cm,between origins}
] {
 |[black]| {} & {} & {} &    & {} &    & {} & \\
    &    &    & {} & {} & {} &    & {} \\
    &|[black,king]| {} & {} &    &    &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
|[king]| {} &    &    & {} & {} &    & {} & \\
    & {} &    & {} &    & {} &    & {} \\
    &    & {} &    &    &    & {} & \\
    &    &    & {} & {} & {} &    & {} \\
};
\end{tikzpicture}
\end{document}

Here's a new image.

Checkers with colours and kings

share|improve this answer
    
You're going to report the Markdown issue on meta, right ;-) –  Joseph Wright Aug 17 '10 at 21:05
    
5. And a similarly appealing syntax for marking kings versus normal checkers :) (Possibly as simple as {K}, where the letter is used to increase the height of the node, since they currently have no content...) –  Ben Lerner Mar 8 '12 at 23:53
    
@BenLerner: Done. The K also increased their width and made the height non-configurable. Keys seemed a better way to go. –  Loop Space Mar 9 '12 at 8:51
add comment

TikZ usually has more than one way to produce a result. Stefan's solution is perfectly fine, but here is another way: TikZ has some programming constructs and many math functions built in that can be used to quickly generate all kinds of diagrams:

\begin{tikzpicture}
    \foreach \row in {0, 1, 2} {
        \foreach \column in {0, 1, ..., 3} {
            \fill[shading=ball, ball color=black!80]
                ({2*\column+mod(\row,2)}, -\row) circle (0.5cm);
        }
    }
\end{tikzpicture}

This produces exactly the same picture as Stefan's solution does (well, except for a slightly changed bounding box because of the matrix padding). The advantage is that you can easily change the size by changing a few numbers and that you can put anything you like inside the foreach loops.

share|improve this answer
add comment

This is a late solution using matrix.skeleton. The advantage is the ease with which the board is drawn without sacrificing the syntax for placing pieces.

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix.skeleton}
\begin{document}
\tikzstyle{ball} = [circle, shading=ball, minimum size=1cm]

\newcommand{\bl}{\node [ball, ball color=black!80!white, draw=black!65!white, thin]{};}
\newcommand{\wh}{\node [ball, ball color=white] {};}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells, label skeleton, nodes={minimum size = 1.2cm}] {
\bl &     & \bl &     & \bl &     & \bl &     \\
    & \bl &     & \bl &     & \bl &     & \bl \\
\bl &     & \bl &     &     &     & \bl &     \\
    &     &     & \bl &     &     &     &     \\
\wh &     &     &     &     &     &     &     \\
    &     &     & \wh &     & \wh &     & \wh \\
\wh &     & \wh &     & \wh &     & \wh &     \\
    & \wh &     & \wh &     & \wh &     & \wh \\
};

\foreach \row in {1, ..., 8} {
  \foreach \col in {1, ..., 8} {
    \pgfmathparse{Mod(\row + \col, 2) ? "white" : "black"}
    \colorlet{squarebg}{\pgfmathresult}
    \fitandstyle[background]{(m-cell-\row-\col)}{fill = squarebg}
  }
}
\end{tikzpicture}
\end{document}

output

share|improve this answer
add comment

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.