3

I am trying to create a checkerboard in LaTeX. I use TikZ's matrix for the task. It works if there's only text or only fills in the matrix. However, when I use both text and matrix, everything start shifting weirdly. How can I fix this?

Below is the code I'm using and its output.

\documentclass[12pt, a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
%This one is the problematic one
\begin{minipage}{0.32\textwidth}
\begin{tikzpicture}
    \draw[xstep=1cm,ystep=1,color=gray] (0,0) grid (4,4);
    \matrix[matrix of nodes,
    inner sep=0pt,
    anchor=south west,
    nodes={inner sep=0pt,text width=1cm,align=center,minimum height=1cm}
    ]{
        |[fill=black!255]| & A & |[fill=black!255]| & \\
        & |[fill=black!255]| & B & |[fill=black!255]| \\
        |[fill=black!255]| & C & |[fill=black!255]| & \\
        & |[fill=black!255]| & D & |[fill=black!255]| \\
    };
\end{tikzpicture}
\end{minipage}
% Nothing's wrong if I only add text and fill on the highest layer
\begin{minipage}{0.32\textwidth}
\begin{tikzpicture}
    \draw[xstep=1cm,ystep=1,color=gray] (0,0) grid (4,4);
    \matrix[matrix of nodes,
    inner sep=0pt,
    anchor=south west,
    nodes={inner sep=0pt,text width=1cm,align=center,minimum height=1cm}
    ]{
        |[fill=black!255]| & A &   & \\
         &   & B & \\
         & C &   & \\
         &   & D & \\
    };
\end{tikzpicture}
\end{minipage}
% Also, everything's okay if there are only fills with no text
\begin{minipage}{0.32\textwidth}
\begin{tikzpicture}
    \draw[xstep=1cm,ystep=1,color=gray] (0,0) grid (4,4);
    \matrix[matrix of nodes,
    inner sep=0pt,
    anchor=south west,
    nodes={inner sep=0pt,text width=1cm,align=center,minimum height=1cm}
    ]{
        |[fill=black!255]| & & |[fill=black!255]| & \\
        & |[fill=black!255]| & & |[fill=black!255]| \\
        |[fill=black!255]| & & |[fill=black!255]| & \\
        & |[fill=black!255]| & & |[fill=black!255]| \\
    };
\end{tikzpicture}
\end{minipage}

\end{document}  

Compiled output of the code

1
  • Welcome to TeX.SE!
    – Mensch
    Sep 26 at 15:47
4

Here is my way. I add something: [anchor=center], [nodes in empty cells], c/.style, [row sep=-\pgflinewidth], [column sep=-\pgflinewidth]; and remove some unnecessary options.

enter image description here

\documentclass[12pt, a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\tikzset{c/.style={fill=yellow}}
\tikzset{mystyle/.style={matrix of nodes,
        nodes in empty cells,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={draw,minimum width=1cm,minimum height=1cm,anchor=center}}}

\begin{tikzpicture}
\matrix[mystyle]{
|[c]|&A    &|[c]|&\\
     &|[c]|&B    &|[c]|\\
|[c]|&C    &|[c]|&\\
     &|[c]|&D    &|[c]|\\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix[mystyle]{
|[c]|&    &|[c]|&\\
&|[c]|&    &|[c]|\\
|[c]|&    &|[c]|&\\
&|[c]|&    &|[c]|\\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix[mystyle]{
&A    &&\\
&&B    &\\
&C    &&\\
&&D    &\\
};
\end{tikzpicture}
\end{document}  
5

For first minipage:

\documentclass[12pt, border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}
\matrix [matrix of nodes,
         nodes in empty cells,  % <---
         nodes={minimum size=1cm, anchor=center,
                inner sep=0pt, outer sep=0pt},
         draw, inner sep=0pt
        ]
{
|[fill]| & A        & |[fill]|  &           \\
         & |[fill]| & B         & |[fill]|  \\
|[fill]| & C        & |[fill]|  &           \\
         & |[fill]| & D         & |[fill]| \\
};
    \end{tikzpicture}
\end{document}

enter image description here

5

Just a little change on Zarko's answer. It's possible to define the chess board combining row and columns style, without having to specifically fill the desired cells.

\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
    \begin{tikzpicture}
\matrix [matrix of nodes,
         nodes in empty cells,  % <---
         every odd column/.style={every odd row/.append style={nodes={fill=black}}},
         every even column/.style={every even row/.append style={nodes={fill=black}}},
         nodes={minimum size=1cm, anchor=center,
                inner sep=0pt, outer sep=0pt},
         draw, inner sep=0pt
        ]
{
 & A &   &  \\
 &   & B &  \\
 & C &   &  \\
 &   & D &  \\
};
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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