4

It is a lot of lines, how can I condense this code so it is shorter? I just wanted to show how the queen moves on a chess board.

\documentclass[12pt]{book}
\usepackage{skak} %or xskak
\usepackage{chessboard}

% https://tex.stackexchange.com/questions/215330/chess-notation-with-skak-how-to-highlight-squares

\begin{document} \begin{frame} \newgame \chessboard[setfen=8/8/8/8/3Q4/8/8/8/8 w - - 0 0, % pgfstyle=border,markfields={d4,d6}, color=green!50, colorbackfield=c5, pgfstyle=color, color=green!50, colorbackfield=b6, pgfstyle=color, color=green!50, colorbackfield=a7, pgfstyle=color, color=green!50, colorbackfield=e3, pgfstyle=color, color=green!50, colorbackfield=f2, pgfstyle=color, color=green!50, colorbackfield=g1, pgfstyle=color, color=green!50, colorbackfield=d1, pgfstyle=color, color=green!50, colorbackfield=d2, pgfstyle=color, color=green!50, colorbackfield=d3, pgfstyle=color, color=green!50, colorbackfield=d5, pgfstyle=color, color=green!50, colorbackfield=d6, pgfstyle=color, color=green!50, colorbackfield=d7, pgfstyle=color, color=green!50, colorbackfield=d8, pgfstyle=color, color=green!50, colorbackfield=c3, pgfstyle=color, color=green!50, colorbackfield=c4, pgfstyle=color, color=green!50, colorbackfield=e4, pgfstyle=color, color=green!50, colorbackfield=e5, pgfstyle=color, color=green!50, colorbackfield=a1, pgfstyle=color, color=green!50, colorbackfield=b2, pgfstyle=color, color=green!50, colorbackfield=f6, pgfstyle=color, color=green!50, colorbackfield=g7, pgfstyle=color, color=green!50, colorbackfield=h8, pgfstyle=color, color=green!50, colorbackfield=a4, pgfstyle=color, color=green!50, colorbackfield=b4, pgfstyle=color, color=green!50, colorbackfield=f4, pgfstyle=color, color=green!50, colorbackfield=g4, pgfstyle=color, color=green!50, colorbackfield=h4, pgfstyle=color, % opacity=0.5, % color=red, %markfield={d5} ] \end{frame} \end{document}

SomeT
  • 337
  • 2
    With "condense" do you mean we should golf that code far beyond being illegible? – Skillmon Oct 31 '20 at 23:52
  • 1
    You don't need to set color=green!50,pgfstyle=color, over and over again. Just once is enough – Phelype Oleinik Oct 31 '20 at 23:52
  • Skillmon not sure what you are referring to in the game of golf but this relates more to chess? @Phelype Oleinik I thought that and it works, however do I really have to write colour back field each time? – SomeT Oct 31 '20 at 23:54
  • 1
    Code golfing means to reduce code as much as possible while still doing the same thing. For reference: https://codegolf.stackexchange.com/ – Skillmon Oct 31 '20 at 23:58
  • Ah ok, then I guess I want to do that. – SomeT Oct 31 '20 at 23:59
  • colorbackfields={b5,c6} I found this here: http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/chessboard/chessboard.pdf but does not work – SomeT Nov 01 '20 at 00:03
  • You really don't want a golfed answer: https://codegolf.stackexchange.com/a/192737/77083 – Skillmon Nov 01 '20 at 00:03
  • @Skillmon of course there's Tips for golfing in TeX/LaTeX if the OP really wants to go down that route. @ SomeT on behalf of your future self, don't golf code except for golfing contests. Even normal code can be hard enough to read; golfed code can end up very similar to obfuscated code – Chris H Nov 02 '20 at 09:02

2 Answers2

15

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

10

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)

David Carlisle
  • 757,742