1

Is there a way to import chess data from a file? The content of the file can be restricted to the moves only.

It is clear to me that

\hidemoves{\input{datafile}}

can not work. However, is it possible to expand \input{datafile} first and feed the result to \hidemoves afterwards?

2
  • 1
    You haven't provided a test file so untested but probably \expandafter\expandafter\expandafter\hidemoves\expandafter\expandafter\expandafter{\csname @@input\endcsname datafile} will work Aug 9, 2017 at 12:00
  • Depends on the content of the file. If it contains only moves of one game it should be possible, but if you have a pgn file with various game you need some script to preprocess the file. Aug 9, 2017 at 12:10

1 Answer 1

2

Using the readarray package to place the file contents into a \def named \myhidemoves. Then one only needs to expand \myhidemoves once before passing it to \hidemoves.

EDITED to change # catcode for \readdef.

\documentclass{book}
\usepackage{xskak,filecontents,readarray}
\begin{filecontents*}{datafile}
1.e4 e6 2.d4 b6#
\end{filecontents*}

\begin{document}
\newchessgame[id=title]
\catcode`#=12 %
\readdef{datafile}{\myhidemoves}
\catcode`#=6 %
\expandafter\hidemoves\expandafter{\myhidemoves}

\tableofcontents

\chapter{\protect\printchessgame[id=title]}

\printchessgame[id=title]
\chessboard[id=title]

\end{document}

enter image description here

Note: copied MWE from \chapter command and skak package.

5
  • This won't work generally, chess changes the catcode of # (the mate symbol). Aug 9, 2017 at 12:08
  • @UlrikeFischer I believe you, of course, but don't fully grok what you are saying. Is there a simple breaking example for datafile you could type in? (I am not a skak user) Aug 9, 2017 at 12:10
  • Try \newchessgame\mainline{1.e4 e6 2.d4 b6#} and in comparision 1.e4 e6 2.d4 b6# in your datafile. Aug 9, 2017 at 12:13
  • @UlrikeFischer It would seem that simply changing the # catcode for the \readdef is sufficient to remedy the issue. Aug 9, 2017 at 12:22
  • @UlrikeFischer Thank you, that is exactly what I needed.
    – Heinz M.
    Aug 9, 2017 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .