0

I like to learn how to create a listing of all the chess moves in a table.

In my MWE, I manually enter in the 1st three moves. Is there a way I can do it through looping?

\documentclass{article}
\usepackage{skak, xskak,chessboard}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{array}
\usepackage{comment}
\usepackage[table]{xcolor}

\setlength{\arrayrulewidth}{1mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{1.5}

\begin{document}
\newchessgame
\noindent
\mainline{
1.e4 e5 2.Nf3 d6 3.d4 Bg4 4.dxe5 Bxf3 5.Qxf3 dxe5 6.Bc4 Nf6 7.Qb3 Qe7 8.Nc3 c6 9.Bg5 b5 10.Nxb5 cxb5 11.Bxb5+ Nbd7 12.O-O-O Rd8 13.Rxd7 Rxd7 14.Rd1 Qe6 15.Bxd7+ Nxd7 16.Qb8+ Nxb8 17.Rd8#}


\vspace{1cm}
\begin{tabular}{ | p{2cm} | p{2cm} | p{2cm} |  }
    \hline\textbf{Move}&\textbf{White}&\textbf{Black}\\
    \hline
    1 & e4 & e5\\
    \hline
    2 & Nf3 & d6\\
    \hline
    3 & d4 & Bg4 \\
    \hline
}
\end{tabular}
\end{document}
  • Please make your example compilable. With the current code I get a xcolor option clash(Can be fixed by just deleting \usepackage[...]{xcolor} and a unmatched } (The single } in the line before \end{tabular}) – Marcel Krüger Jun 20 at 9:49
  • Thank you for your advice. My codes work in Overleaf. But I will take adopt your suggestions. – user92127 Jun 20 at 18:47
3

Creating a tabular with loops is not easy, as tabular cells build groups. Normally the best is to assemble the content of the tabular in a macro and to use this. E.g.

\documentclass{article}
\PassOptionsToPackage{table}{xcolor}
\usepackage{xskak}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{array}
\usepackage{comment}

\setlength{\arrayrulewidth}{1mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{1.5}
\usepackage{etoolbox}

\begin{document}
\newchessgame
\noindent
\mainline{
1.e4 e5 2.Nf3 d6 3.d4 Bg4 4.dxe5 Bxf3 5.Qxf3 dxe5 6.Bc4 Nf6 7.Qb3 Qe7 8.Nc3 c6 9.Bg5 b5 10.Nxb5 cxb5 11.Bxb5+ Nbd7 12.O-O-O Rd8 13.Rxd7 Rxd7 14.Rd1 Qe6 15.Bxd7+ Nxd7 16.Qb8+ Nxb8 17.Rd8#}

\newcommand\tabularcontent{}
\whiledo{\xskaktestmoveid
{\xskakget{movenr}}{\xskakget{player}}}
{%
 \ifthenelse{\equal{\xskakget{player}}{w}}
 {%
  \eappto\tabularcontent
   {\xskakget{movenr}&
    \noexpand\xskakset{moveid=\xskakget{moveid}}%
    \noexpand\xskakget{lan}&}%
 }
 {%
  \eappto\tabularcontent
   {\noexpand\xskakset{moveid=\xskakget{moveid}}}
  \appto\tabularcontent{\xskakget{lan}\\\hline}% 
 }
 \xskakset{stepmoveid}
}%
\ifthenelse{\equal{\xskakgetgame{lastplayer}}{w}}{\appto\tabularcontent{\\\hline}}{}


\vspace{1cm}
\begin{tabular}{ | p{2cm} | p{2cm} | p{2cm} |  }
    \hline\textbf{Move}&\textbf{White}&\textbf{Black}\\\hline
    \tabularcontent
\end{tabular}
\end{document}

enter image description here

  • Thank you for teaching me. – user92127 Jun 20 at 10:45

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.