How can I condense this code?

With key-value arguments, you usually don't get anything by setting a key more than once. There may be exceptions, with keys that denote actions rather than settings. chessboard's keys are sometimes actions, so some of them have to be repeated, but in this case setting the colour, you don't need that, so you can remove all but one occurrence of pgfstyle=color,color=green!50, as they are redundant.

Then, searching for colorbackfield in the chessboard documentation brings up colorbackfields:

enter image description here

so you're down to:

\documentclass{book}
\usepackage{xskak}
\begin{document}
\newgame
\chessboard[
  setfen=8/8/8/8/3Q4/8/8/8/8 w - - 0 0,
  color=green!50,
  colorbackfields={c5,b6,a7,e3,f2,g1,d1,d2,d3,d5,d6,d7,d8,c3,
                   c4,e4,e5,a1,b2,f6,g7,h8,a4,b4,f4,g4,h4},
  pgfstyle=color
]
\end{document}

enter image description here


Phelype pointed out the package has a multivalued key here but if it had not (or like me you didn't look for package documentation) then you can compress the code just using general tex coding eg

\documentclass[12pt]{book}
\usepackage{skak,chessboard}
\def\z#1#2{\ifx.#1\else colorbackfield=#1#2,\expandafter\z\fi}
\begin{document}
\newgame
\expanded{\noexpand\chessboard[
setfen=8/8/8/8/3Q4/8/8/8/8 w - - 0 0,
pgfstyle=color,color=green!50,
\z c5b6a7e3f2g1d1d2d3d5d6d7d8c3c4e4e5a1b2f6g7h8a4b4f4g4h4..
]} 
\end{document}

I also removed frame as \frame is a one argument macro, a variant of \fbox which doesn't seem wanted here (and doesn't work as an environment)

Tags:

Chess