0

So far I have a chessboard which is displayed fully, however, I want to modify it such that it has missing opposite diagonal corner pieces, I don't mind which corner.

This is my code so far:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{csquotes}
\usepackage[style=verbose-ibid,backend=bibtex]{biblatex}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\bibliography{bibliography}
\usepackage{amsmath}

\begin{figure}[h!]
\caption{Chess board with opposite diagonal corner pieces missing.}
\label{fig:mutilated_chessboard}
\begin{center}
\begin{tikzpicture}[x=1cm]
    \foreach \x in {0,...,7} \foreach \y in {0,...,7}
    {
        \pgfmathparse{mod(\x+\y,2) ? "black" : "white"}
        \edef\colour{\pgfmathresult}
        \path[fill=\colour] (\x,\y) rectangle ++ (1,1);
    }
    \draw (0,0)--(0,8)--(8,8)--(8,0)--cycle;
\end{tikzpicture}
\end{center}
\end{figure}

I was thinking of adding an if statement to only run the contents of the nested for loops if the x and y values were not both equal to 0 or both equal to 8. How should I do this?

Tom Finet
  • 103
  • 2

1 Answers1

2

Using you suggestion of some \ifs.

It's a solution, but it's too much work for you and for TikZ to check every square. A better solution would be to draw on top of the chess board.

Also, are you aware of the skak package?

You didn't specify which two corners, so I made the two variants:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}[h!]
\caption{Chess board with opposite diagonal corner pieces missing.}
\label{fig:mutilated_chessboard}
\begin{center}
\begin{tikzpicture}[x=1cm]
    \foreach \x in {0,...,7} \foreach \y in {0,...,7}
    {
        \pgfmathparse{mod(\x+\y,2) ? "black" : "white"}
        \edef\colour{\pgfmathresult}
% This one for the bottom-left and top-right corners
        % \ifnum\x=\y
        %   \ifnum\x=7
        %     \def\colour{black}
        %   \else\ifnum\x=0
        %     \def\colour{black}
        %   \fi\fi
        % \fi
% % % % % %
% And this one for the top-left and bottom-right corners
        \ifnum\x=\numexpr\y-7
          \ifnum\x=0
            \def\colour{white}
          \fi
        \fi
        \ifnum\y=\numexpr\x-7
          \ifnum\y=0
            \def\colour{white}
          \fi
        \fi
% % % % % %
        \path[fill=\colour] (\x,\y) rectangle ++ (1,1);
    }
    \draw (0,0)--(0,8)--(8,8)--(8,0)--cycle;
\end{tikzpicture}
\end{center}
\end{figure}

\end{document}
  • I don't think that skak is suitable here. Imho the OP is trying to draw squares. "chessboard" describes the geometry and not the game. – Ulrike Fischer Feb 06 '18 at 22:34
  • @UlrikeFischer I agree. But told him just in case :) – Phelype Oleinik Feb 06 '18 at 22:39
  • Cheers, solved my problem! – Tom Finet Feb 06 '18 at 22:46
  • I think those \numexpr will keep expanding until they run into either \def or something \path expands to. I recommend either appending \relax after the -7s or swapping the order of those two \ifnum so that the = terminates the \numexpr. – TH. Feb 07 '18 at 02:03