5

I wish to create a chess worksheet with 4 groups: Basic, Pawn, 2Pawns, and Minor+Pawns. A manual worksheet is done by me. But I wish to automate this process. The worksheet will pull \chessboard from each group and set them to print alongside each other. See my manual worksheet. I couldn't figure out how to implement macro to build it.

\documentclass{article}
\usepackage{graphicx}
\usepackage{xskak,chessboard}
\parindent=0pt
\addtolength{\voffset}{-1in}
\addtolength{\hoffset}{-1in}
\addtolength{\textwidth}{2.95cm}
\addtolength{\textheight}{10\baselineskip}

%*** MACRO BASIC ********************************
\newcommand\groupbasic{
\rotatebox{90}{\makebox[0.28\linewidth]{\textbf{basic}}}}
%******MACRO PAWN LEVEL **********************************
\newcommand\grouppawn{
\rotatebox{90}{\makebox[0.28\linewidth]{\textbf{pawns}}}}
%*****MACRO 2PAWN LEVEL *********************************
\newcommand\grouptwopawn{
\rotatebox{90}{\makebox[0.28\linewidth]{\textbf{2 pawns}}}}
% ***********MACRO MINORPAWNS ********************
\newcommand\groupminorpawn{
\rotatebox{90}{\makebox[0.28\linewidth]{\textbf{Minor + Pawns}}}}
%*****************************************

\begin{document}
%********************** BASIC LEVEL **********************
\chessboard[smallboard,setfen=3k4/8/2Q5/4K3/8/8/8/8 w - - 0 1]
\chessboard[smallboard,setfen=8/8/2k5/7R/6R1/8/5K2/8 w - - 0 1]
\chessboard[smallboard,setfen=1k6/7R/3K4/8/8/8/8/8 w - - 0 1]
%******* PAWN LEVEL *****************
\chessboard[smallboard, setfen=8/8/8/8/8/1k6/7P/4K3 w - - 0 1]
\chessboard[smallboard,setfen=8/8/8/8/4k3/4p3/4K3/8 w - - 0 1]
\chessboard[smallboard, setfen=8/1k6/8/8/1PK5/8/8/8 w - - 0 1]
%************ 2 PAWN LEVEL *******************
\chessboard[smallboard,setfen=8/8/8/4p1K1/2k1P3/8/8/8 w - - 0 1]
\chessboard[smallboard,setfen=,setfen=8/8/8/p7/P7/7k/7P/6K1 w - - 0 1]
\chessboard[smallboard,setfen=k7/P1K5/6p1/7p/7P/8/8/8 w - - 0 1]
%************ MINOR + PAWN LEVEL ********************
\chessboard[smallboard, setfen=4BK2/6P1/5k2/8/8/1b6/8/8 w - - 0 1]
\chessboard[smallboard, setfen=8/8/4k3/2p4n/2P1K3/8/7B/8 w - - 0 1]
\chessboard[smallboard,setfen=8/p7/P5p1/1P4P1/5P2/3kb3/8/3K4 w - - 0 1]

\clearpage
%********************** BASIC LEVEL **********************
\groupbasic
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,setfen=3k4/8/2Q5/4K3/8/8/8/8 w - - 0 1]
\end{minipage}%
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,setfen=8/8/2k5/7R/6R1/8/5K2/8 w - - 0 1]
\end{minipage}%
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,setfen=1k6/7R/3K4/8/8/8/8/8 w - - 0 1]
\end{minipage}%   
\vspace{.40cm}
%******* PAWN LEVEL *****************
\grouppawn
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,
    setfen=8/8/8/8/8/1k6/7P/4K3 w - - 0 1]
\end{minipage}%
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,setfen=8/8/8/8/4k3/4p3/4K3/8 w - - 0 1]
\end{minipage}%
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,
    setfen=8/1k6/8/8/1PK5/8/8/8 w - - 0 1]
\end{minipage}% 
\vspace{.40cm}
%************ 2 PAWN LEVEL *******************
\grouptwopawn
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,setfen=8/8/8/4p1K1/2k1P3/8/8/8 w - - 0 1]
\end{minipage}%
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,
    setfen=,setfen=8/8/8/p7/P7/7k/7P/6K1 w - - 0 1]
\end{minipage}%
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,setfen=k7/P1K5/6p1/7p/7P/8/8/8 w - - 0 1]
\end{minipage}% 
\vspace{.40cm}
%************ MINOR + PAWN LEVEL ********************
\groupminorpawn
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard, setfen=4BK2/6P1/5k2/8/8/1b6/8/8 w - - 0 1]
\end{minipage}%
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard, setfen=8/8/4k3/2p4n/2P1K3/8/7B/8 w - - 0 1]
\end{minipage}%
\begin{minipage}[b]{0.35\linewidth}
\chessboard[smallboard,setfen=8/p7/P5p1/1P4P1/5P2/3kb3/8/3K4 w - - 0 1]
\end{minipage}% 
\end{document}


naphaneal
  • 2,614
user92127
  • 187

1 Answers1

7

Your chess package loads tizk, so you can use \foreach. In your document you get tons of overfull complaints, so I made the page a bit wider (and added showframe to show that it works and how much space is left). Then you can draw the full thing in a nested \foreach loop.

\documentclass{article}
\usepackage{xskak,chessboard}
\usepackage[showframe,margin=1cm]{geometry}
\begin{document}
\foreach \X/\Y in 
{basic/{3k4/8/2Q5/4K3/8/8/8/8 w - - 0 1,
8/8/2k5/7R/6R1/8/5K2/8 w - - 0 1,
1k6/7R/3K4/8/8/8/8/8 w - - 0 1},
%******* PAWN LEVEL *****************
pawn/{8/8/8/8/8/1k6/7P/4K3 w - - 0 1,
8/8/8/8/4k3/4p3/4K3/8 w - - 0 1,
8/1k6/8/8/1PK5/8/8/8 w - - 0 1},
%************ 2 PAWN LEVEL *******************
2 pawns/{8/8/8/4p1K1/2k1P3/8/8/8 w - - 0 1,
8/8/8/p7/P7/7k/7P/6K1 w - - 0 1,
k7/P1K5/6p1/7p/7P/8/8/8 w - - 0 1},
%************ MINOR + PAWN LEVEL ********************
Minor + Pawns/{4BK2/6P1/5k2/8/8/1b6/8/8 w - - 0 1,
 8/8/4k3/2p4n/2P1K3/8/7B/8 w - - 0 1,
8/p7/P5p1/1P4P1/5P2/3kb3/8/3K4 w - - 0 1}}
{\noindent\tikz{\node[rotate=90,font=\bfseries,align=center,text width=4cm]{\X};}
\foreach \Z in \Y
{\begin{minipage}[b]{0.3\linewidth}
\chessboard[smallboard,setfen=\Z]
\end{minipage}}

}
\end{document}

[![enter image description here][1]][1]

Here is another version after a comment by Ulrike Fischer (Thanks!). It adds a \par if there are more than 3 FENs in a row.

\documentclass{article}
\usepackage{xskak,chessboard}
\usepackage[showframe,margin=1cm]{geometry}
\begin{document}
\foreach \X/\Y in 
{basic/{3k4/8/2Q5/4K3/8/8/8/8 w - - 0 1,
8/8/2k5/7R/6R1/8/5K2/8 w - - 0 1,
1k6/7R/3K4/8/8/8/8/8 w - - 0 1},
%******* PAWN LEVEL *****************
pawn/{8/8/8/8/8/1k6/7P/4K3 w - - 0 1,
8/8/8/8/4k3/4p3/4K3/8 w - - 0 1,
8/8/8/8/4k3/4p3/4K3/8 w - - 0 1,
8/8/8/8/4k3/4p3/4K3/8 w - - 0 1,
8/1k6/8/8/1PK5/8/8/8 w - - 0 1},
%************ 2 PAWN LEVEL *******************
2 pawns/{8/8/8/4p1K1/2k1P3/8/8/8 w - - 0 1,
8/8/8/p7/P7/7k/7P/6K1 w - - 0 1,
k7/P1K5/6p1/7p/7P/8/8/8 w - - 0 1},
%************ MINOR + PAWN LEVEL ********************
Minor + Pawns/{4BK2/6P1/5k2/8/8/1b6/8/8 w - - 0 1,
 8/8/4k3/2p4n/2P1K3/8/7B/8 w - - 0 1,
8/p7/P5p1/1P4P1/5P2/3kb3/8/3K4 w - - 0 1}}
{\noindent\tikz{\node[rotate=90,font=\bfseries,align=center,text width=4cm]{\X};}
\foreach \Z [count=\Cnt] in \Y
{\pgfmathtruncatemacro{\itest}{ifthenelse(\Cnt==1,1,mod(\Cnt-1,3))}%
\ifnum\itest=0%
\par
\fi%
\begin{minipage}[b]{0.3\linewidth}
\chessboard[smallboard,setfen=\Z]
\end{minipage}%
}
\par
}
\end{document}
  • I was thinking of foreach loop but I couldn't figure out how to do it. Thank you for your time. Much appreciated. I also noticed the page number at the bottom is cut off due to bottom margin being big. Any suggestion? – user92127 Jun 22 '19 at 01:35
  • I test your solution, but there's a problem. Add 3 more into each group and you will see that the loop doesn't work properly. Any suggestion? – user92127 Jun 22 '19 at 02:00
  • @user92127 I thought you always had 3 per row because your example had this feature. Yes, one can make it work for arbitrary numbers but this will require a clear prescription. How many chess boards per row? What should happen to the rotated title? (Will be back online in a bit.) –  Jun 22 '19 at 02:02
  • Everything is set up correctly. Except on the next worksheet, we will do the same thing again with different fen for each group. Then on the next worksheet, the process repeats itself. – user92127 Jun 22 '19 at 02:13
  • @user92127 I have just put two of these nested loops in succession, of course separated by a new line to start a new paragraph, and the result looks OK to me. Do your problems get better/disappear when you put a \par before the closing } of the outer loop? –  Jun 22 '19 at 03:22
  • can you show me what you mean by " put two of these nested loops in succession." – user92127 Jun 22 '19 at 03:27
  • @user92127 I added the corresponding code. –  Jun 22 '19 at 03:43
  • the updated codes work. I now have two worksheets. My goal is not to do these repeated steps but to have the results done automatically through looping. That's the reason for my questions. At the end of the 3rd fen in a group, can the loop be started again iat the 4th fen? – user92127 Jun 22 '19 at 04:02
  • @user92127 I wrapped another loop around these two nested loops. Is that what you want? Of course, you can also define a command for the inner loop and just invoke this command several times, or in a loop. –  Jun 22 '19 at 04:17
  • With the current code, you will have 4 worksheets with the same fen. Now, If I add 4-6fen for each group, and change the nested loop number 2, there's an error message. The nested loop should give me a 2nd worksheet with fen from 4-6 for each group. – user92127 Jun 22 '19 at 04:34
  • @user92127 Could you please add the exact code you are trying to your question?(Most likely I do not yet understand your real aim here.) –  Jun 22 '19 at 04:41
  • The loop you created works well for the 1st worksheet, In the 2nd worksheet with an additional 4-6 fens added to each worksheet, the loop doesn't put the fens in proper order. It groups them together than go to the next group. Does this have to do with a counter? or the fact that foreach loop doesn't remember the last fen? – user92127 Jun 22 '19 at 05:07
  • @user92127 I really do not understand what you are trying to tell me. I am not a chess expert, I really do not know what you mean by "with an additional 4-6 fens". Please add an explicit example. –  Jun 22 '19 at 05:09
  • @marmot A "FEN` is a description of a chess position. And I guess the OP wants a loop that can also handle the case that there are more than 3 positions for a topic. – Ulrike Fischer Jun 22 '19 at 08:39
  • @UlrikeFischer Thanks! –  Jun 22 '19 at 12:38
  • @UlrikeFischer - can you suggest a solution? Marmot solution of adding \par doesn't work when I compile. – user92127 Jun 22 '19 at 19:03
  • @user92127 What happens if you compile the lower example code of my answer? –  Jun 22 '19 at 19:09
  • I still get an error. Can I repost my question to show you the error? – user92127 Jun 22 '19 at 19:58
  • @user92127 Do you get an error if you compile any of my above codes? If so, what does the error message say? If you compile a modified code, either add it to your question or ask a new question. (Obviously the codes run through on my machine. I am using pdflatex on an updated TeXLive 2019 distribution.) –  Jun 22 '19 at 20:10
  • I am using pdflatex in Overleaf. I reframe the question here: https://tex.stackexchange.com/questions/496966/foreach-loop-error-in-generating-diagrams – user92127 Jun 22 '19 at 20:27