in chess, kid, latex, python

OK, I’m shameful. After fighting with Scid’s exporter, and then correcting bugs in pgn2ltx’s source, I finally decided to take a look at that PGN file format. And guess what? It’s already composed of FEN notation. And guess what? that super-über cool new skaknew module for LaTeX gets its input as FEN!!

So, if I had a working brain 1 week ago (at least), I would write a small python program that would eat that PGN and make it into a nicely formatted skaknew TeX formatted file!

Here is that much wanted program, that took just 5 minutes to write:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# vim:syntax=python:sw=4:ts=4:expandtab
import sys,re

HEADER="""\
\\documentclass[10pt,twocolumn]{article}

\\usepackage[skaknew]{chessboard,skak}
%\\usepackage[ps]{skak}
\\usepackage{latexsym}
\\usepackage[utf8]{inputenc}
\\font\\logo=logo10
\\font\\sknf=SkakNew-Figurine
\\font\\sknfbx=SkakNew-FigurineBold
\\font\\skndia=SkakNew-DiagramT
\\def\\Metafont{\\mbox{\\logo METAFONT}}

\\usepackage{a4wide}
%\\usepackage{geometry} % to change the page dimensions
%\\geometry{a4paper}
%%\\geometry{margin=5in}
%\\geometry{portrait}

\\usepackage{fancyhdr} % This should be set AFTER setting up the page geometry
\\pagestyle{fancy} % options: empty , plain , fancy
\\renewcommand{\\headrulewidth}{0pt} % customise the layout...
\\lhead{}\\chead{}\\rhead{}
\\lfoot{}\\cfoot{\\thepage}\\rfoot{}

\\frenchspacing
\\begin{document}
"""

TEMPLATE="""\
\\chessboard[
   setfen=%s,
   ]

"""

END_DOC = "\\end{document}"

def main():
    pre = sys.argv[1].split('.',2)[0]
    output_file = open("%s-out.tex" % pre,'w')

    # First, write the header
    output_file.write(HEADER)

    expr = re.compile('^\[FEN "([^"]+)"\]')
    for line in open(sys.argv[1]).xreadlines():
        result = expr.search(line)
        if result:
            output_file.write(TEMPLATE % result.group(1))

    output_file.write(END_DOC)
    output_file.close()

if __name__ == '__main__':
    main()

And the best of all, is that I have now full control in its output (well, in my light LaTeX’s knowledge limits). So now I can add a marker to tell which turn it is (little colored square box next to each game). You wll still get 196 pages, that is 1171 mate in one diagrams to solve.

So, to summarize, here are the files: