## Luc Stepniewski in chess, kid, latex, python | December 28, 2010

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

\\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
\\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')

expr = re.compile('^$FEN "([^"]+)"$')
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: