跳到内容

Introduction

This article explores the basics of chess typesetting, including brief reviews/summaries of some chess conventions that you might encounter in LaTeX chess package documentation. Readers who don't need that introductory material can jump straight to the LaTeX package examples.

One package to rule them all

The CTAN repository lists a number of chess-related packages, so you may be wondering which one to choose? This article uses the xskak package, which is highly recommended for typesetting chess due to its wide range of features, extensive configuration options and excellent documentation. xskak loads and extends the original skak package and also loads the chessboard package which provides a huge range of options to configure and create chess boards.

The examples provided by this article cannot substitute for the excellent documentation accompanying the xskak and chessboard packages, which we strongly encourage you to browse, review and explore because both sets of documentation contain numerous, and very helpful, examples.

Note: Both skak and xskak can parse and typeset chess games in (a subset of) the Portable Game Notation (PGN). xskak extends the subset of PGN supported by skak.

Trivia: skak is the Danish word for chess.

Some notes

Here are some important points noted in the xskak documentation:

xskak will load the package skak without option (and won’t pass options to it). So, if you want to load skak with options, you must do it before loading the package xskak.

xskak is an extension (and not a replacement) the main work is still done by the package skak... please read first the documentation of the package skak...

  • Note: Among the chess-related packages listed on CTAN is one called skaknew. Despite its name, the skaknew package is not a new or enhanced version of the original skak package: it is set of Type 1 chess fonts. The xskak package utilizes the skaknew fonts so there's no need for you to manually load them into your project.

Minimal xskak example

To "set the scene", and without further explanation, here is a minimal xskak package example that typesets two chess boards: one is empty, the other has all the chess pieces in their initial positions:

\documentclass{article}
% Note: you only need to load xskak,
% not the skak or chessboard packages.
\usepackage{xskak}
\begin{document}
% This \chessboard command draws 
% an empty chess board: the option
% showmover=false will be discussed
% later in the article
\chessboard[showmover=false]
% The \newchessgame command 
% initializes a new game:
\newchessgame
% Because a new game was initialized, 
% \chessboard now draws a 
% board with chess pieces:
\chessboard
The small white square to the right of the second board is called the \textit{mover}.
\end{document}

 Open this example in Overleaf

This xskak example produces the following output:

Chess conventions: a brief review

For the benefit of readers new to chess, especially typesetting it, the following sections provide brief introductions to topics and conventions you will encounter within LaTeX chess packages and their accompanying documentation. Readers familiar with chess conventions can skip these sections and jump to the LaTeX examples.

Portable Game Notation

The skak and xskak packages support a subset of the Portable Game Notation (PGN), a standard which was invented circa 1994 by Steven Edwards. The original PGN specification, dated 1994, is available on archive.org. A direct link to the standard is https://tim-mann.org/Standard.

According to the text of the PGN standard:

PGN is "Portable Game Notation", a standard designed for the representation of chess game data using ASCII text files. PGN is structured for easy reading and writing by human users and for easy parsing and generation by computer programs... PGN is proposed as a universal portable representation for data interchange.

Note that PGN files are designed for the interchange of game data; i.e., PGN files are designed to contain complete game information—such as the name of the players, details of the event/date etc., together with a record, in algebraic notation, of all moves in the game.

Note: For another short introduction to PGN visit http://www.enpassant.dk/chess/palview/p3manual/p3pgn.htm

Chess board ranks and files

A chess board is an 8×8 grid of squares which could, normally, be described as 8 horizontal rows and 8 vertical columns; however, a different terminology is used to describe a chess board:

  • there are 8 horizontal ranks (instead of rows)
  • there are 8 vertical files (instead of columns)

Each square on the chess board can be identified (named) using several conventions but the algebraic notation (coordinate system) is the accepted international standard.

Algebraic notation

Within the algebraic notation system the ranks and files are labelled as follows:

  • the 8 horizontal ranks are labelled using the numbers 1 to 8
  • the 8 vertical files are labelled using the letters a to h.

The next diagram shows a chess board with the algebraic notation labels used for the files and ranks:

Each square on the chess board is at the intersection of a particular file (column) and rank (row): the corresponding file and rank value can be used as coordinates to uniquely identify each square for use in describing the location and movement of chess pieces.

By way of example, the following diagram shows the square located at e7 followed by a second chess board which enumerates the location of all 64 squares:

If you are new to chess, the Wikipedia page Algebraic notation contains some useful background information, some of which we summarize below.

Notation for chess pieces

Each chess piece type, usually excepting pawns, is identified by an uppercase letter which can vary according the language being used. English-speaking players use the following letters:

Letter Pieces
K King
Q Queen
R Rook
B Bishop
N Knight

In standard algebraic notation pawns are not usually identified by an uppercase letter; instead, they are recognizable by the absence of an uppercase letter. However, that convention is not always adopted; the uppercase letter P may be used to identify pawns. In Forsyth-Edwards Notation (FEN) uppercase and lowercase letters are used to represent chess pieces.

Notation for an individual move

To record the movement of a chess-piece you need to identify the piece and its destination square. That can be achieved by using the piece's uppercase letter plus the coordinates of the destination square.

Note: when describing a pawn move, a letter indicating the piece is a pawn is not usually used; only the destination square is usually given, as shown in the following examples:

  • non-pawn moves:
    • Be5: Bishop moves to square e5
    • Nf3: kNnight moves to square f3
  • pawn moves:
    • c5: a pawn moves to square c5
    • Note: in certain circumstances (e.g., some LaTeX commands) pawns are identified by uppercase letter P; so c5 might be written Pc5.

Three dots (...) black piece move indication

Section 8.2.2.2 of the Portable Game Notation standard explains the use of three dots (...) after a move number indicator (e.g., 3...e6):

A white move number indication is formed from the integer giving the fullmove number with a single period character appended. A black move number indication is formed from the integer giving the fullmove number with three period characters appended.

Convention for a series of moves

As explained in the Wikipedia article on Algebraic notation, to describe a series of moves:

  • each individual move usually uses uppercase letters for non-pawn pieces, although, as noted above, in some circumstances (conventions) a capital P might be used to indicate a pawn move.
  • a series of chess moves is a sequence of <white piece move> <black piece move> pairs—note that each move pair is numbered (see example below)

Other chess notations: FAN and FEN

Figurine Algebraic Notation (or FAN)

Language-specific letters used to represent chess pieces are often replaced by universally recognized symbols representing chess pieces. This symbolic representation is known as Figurine Algebraic Notation, or FAN. For example, the symbol ♞ (or ♘) can be used in place of N (for kNight).

The Unicode specification encodes chess characters within the Miscellaneous Symbols set. The following table lists the Unicode value, name, and representative symbol for each chess character—the actual glyphs (character designs) used to display the chess symbols will depend on your device/browser fonts settings.

Representative symbol
(glyph)
Unicode value
(in HEX)
Unicode character name
(formal)
2654 WHITE CHESS KING
2655 WHITE CHESS QUEEN
2656 WHITE CHESS ROOK
2657 WHITE CHESS BISHOP
2658 WHITE CHESS KNIGHT
2659 WHITE CHESS PAWN
265A BLACK CHESS KING
265B BLACK CHESS QUEEN
265C BLACK CHESS ROOK
265D BLACK CHESS BISHOP
265E BLACK CHESS KNIGHT
265F BLACK CHESS PAWN

Forsyth-Edwards Notation (FEN)

As noted, PGN is used for the storage and interchange of entire games, including all moves played in that game. By contrast, the Forsyth-Edwards Notation (FEN) provides a snapshot of a game at some point in time. FEN notation makes use of uppercase and lowercase letters to represent chess pieces:

  • white pieces are identified by uppercase letters (PNBRQK)
  • black pieces are identified by lowercase letters (pnbrqk)

where P is for a white pawn and p is for a black pawn.

A detailed discussion of FEN, and comparing it to PGN, is outside the scope of this article. Readers wanting to better understand the differences between PGN and FEN may wish to explore the following video:

A first example with the xskak package

Making your moves

To typeset chess moves as the game progresses you use the command \mainline{list of moves} where list of moves is expressed in algebraic chess notation. We'll demonstrate typesetting the following sequence of moves:

  1. e4 e5: white pawn moves to e4, black pawn moves to e5
  2. Nf3 Nc6: white kNight moves to f3, black kNight moves to c6
  3. Bb5 a6: white Bishop moves to b5, black pawn moves to a6

This sequence of moves can be written using the xskak package command \mainline{1.e4 e5 2.Nf3 Nc6 3.Bb5 a6}—note that each move–pair is numbered (e.g., 1.e4 e5):

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\mainline{1.e4 e5 2.Nf3 Nc6 3.Bb5 a6}
\showboard % A skak package command. Future examples will use \chessboard[...]
\end{document}

 Open this example in Overleaf

This xskak example produces the following output:

The following commands are used:

  • \newchessgame[〈key=value list〉]: starts a new game and initializes the chess board. The [〈key=value list〉] is optional so you can also write \newchessgame and rely on the default board setup. The set of optional keys is listed is the xskak documentation.
  • \mainline{〈list of chess moves〉}: processes the 〈list of chess moves〉 and updates the board so that \showboard (or \chessboard) typesets the board's current status.
  • \showboard: typesets the chess board. The \showboard command originates from the underlying skak package, which xskak extends. The chessboard package, which is loaded by xskak, provides the very versatile \chessboard[〈key=value list〉] command to which the xskak package adds additional keys. Because \chessboard[...] is intended to replace \showboard, future examples in this article will only use the \chessboard[...] command.

Introducing the \chessboard command

The command \chessboard[〈key=value list〉]: typesets chess boards using an optional 〈key=value list〉. The list of keys available to use is extensive, providing a great deal of control over the size, content, layout, design, annotation etc of chess boards. Readers are encouraged to read the excellent package documentation for chessboard (PDF) and xskak (PDF), both of which contain numerous helpful examples—many more than we can demonstrate in this article.

Because the keys are optional you can write \chessboard which uses package default values to typeset a chess board; for example, the following code typesets two boards using \chessboard without any options. Further annotations, added manually in Inkscape, show the movers (here as squares) that are typeset by default:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\mainline{1.e4 e5 2.Nf3 Nc6 3.Bb5}
\chessboard % instead of \showboard

\newchessgame
\mainline{1.e4 e5 2.Nf3 Nc6 3.Bb5 a6}
\chessboard % instead of \showboard
\end{document}

 Open this example in Overleaf.

The following graphic shows the two chess boards produced by the code above, both were typeset using \chessboard:

Unsurprisingly, various keys are provided to control the mover, including showing or hiding it; for example, to hide the mover you can use the showmover key with a value of false:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\mainline{1.e4 e5 2.Nf3 Nc6 3.Bb5 a6}\par
\chessboard[showmover=false] % Hide the mover
\end{document}

 Open this example in Overleaf.

This example produces the following output—a chess board without the mover:

The chessboard package documentation (PDF) provides a list of keys for customizing the mover, together with a note on how to design your own mover!

Additional xskak examples

Text comments

The PGN specification supports making comments using braces around the text of the comment: {〈Text〉}; however, the xskak package prefers text comments to be written as \xskakcomment{〈Text〉}, as demonstrated in the following example:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame %Start a new chess game
\mainline{1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 \xskakcomment{ This is a comment. }}\vspace{3mm}

\chessboard
\end{document}

 Open this example in Overleaf.

This example produces the following output:

Initializing a board using FEN notation

You can use FEN to start the board in any position. The following example uses the setfen key in a \chessboard command to demonstrate a FEN example taken from the skak package documentation:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\chessboard[setfen=r5k1/1b1p1ppp/p7/1p1Q4/2p1r3/PP4Pq/BBP2b1P/R4R1K w - - 0 20]
\end{document}

 Open this example in Overleaf.

This example produces the following output:

The FEN notation populates the board starting with the uppermost rank (number 8) and works downwards to populate subsequent ranks. For example, to initialize the board above rank 8 is described using r5k1:

  • rook
  • 5 empty squares
  • the king
  • 1 empty square

hence using the notation r5k1. Note the use of lowercase letters to describe black chess pieces together with the use of uppercase P for white pawns and lowercase p for black pawns.

Using \lastmove

The \lastmove command can be used for commenting on the last move made:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\mainline{1.e4}

\chessboard

\lastmove{} is the most common opening move.
\end{document}

 Open this example in Overleaf.

This example produces the following output:

Variations and an example of the ... notation

The following reproduces an example contained in the skak package documentation, updated to use xskak commands. Here, we see an example of the \variation command and the three-dot (...) syntax used to indicate a black move:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\mainline{1.e4 e5 2.Nf3 Nc6 3.Bb5}

\chessboard

\mainline{3...a6}

A variant \variation{3...Nf6} is used here to show a \texttt{\string\variation} command.

\mainline{4.Ba4}

\chessboard
\end{document}

 Open this example in Overleaf.

This example produces the following output:

Showing only certain chess pieces

Because xskak loads the chessboard package you can use the extremely powerful \chessboard[〈key=value list〉] command to show or hide areas of the board, or collections of chess pieces, that are of interest. The chessboard documentation contains numerous examples, such as those reproduced below.

Using keys to hide certain fields

This example uses the keys hideall, showpieces and addpieces:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\chessboard[hideall,
showpieces={P,p},
addpieces=pe5]
\end{document}

 Open this example in Overleaf

This xskak example produces the following output:

This example uses the keys hidefiles, showfiles and showranks:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\def\myfiles{a,b}
\chessboard[hidefiles=\myfiles,
addpieces=Ra2,
showfiles=a,
showranks=2]
\end{document}

 Open this example in Overleaf

This xskak example produces the following output:

Using keys to hide all white or all black pieces

The following example demonstrates the keys hidewhite and hideblack:

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newchessgame
\mainline{1.e4 e5 2.Nf3 Nc6}\chessboard[hidewhite]

\newchessgame
\mainline{1.e4 e5 2.Nf3 Nc6}\chessboard[hideblack]
\end{document}

 Open this example in Overleaf

This xskak example produces the following output:

Further examples from tex.stackexchange

The following examples use solutions found on tex.stackexchange.com.

How to highlight squares?

Source: based on https://tex.stackexchange.com/a/215334

\documentclass{article}
\usepackage{xskak}
\begin{document}
\newgame
\chessboard[setfen=8/8/8/3Q4/8/8/8/8 w - - 0 0,
            pgfstyle=border,markfields={d4,d6},
            color=blue!50,
            colorbackfield=c5,
            pgfstyle=color,
            opacity=0.5,
            color=red,
            markfield={d5}]
\end{document}

 Open this example in Overleaf.


Creating different board styles

Source: based on https://tex.stackexchange.com/a/358041

\documentclass{article}
\usepackage{xskak}
\begin{document}
\storechessboardstyle{4x16}{%
  maxfield=d16,
  borderwidth=1mm,
  color=white,
  colorwhitebackfields,
  color=black,
  colorblackbackfields,
  blackfieldmaskcolor=black,
  whitepiececolor=yellow,
  whitepiecemaskcolor=red,
  blackpiececolor=cyan,
  blackpiecemaskcolor=blue,
  addfontcolors,
  pgfstyle=border,
  color=white,
  markregion=a1-d16,
  showmover=false,
  hlabelwidth=18pt,
  vlabellift=16pt}
  \chessboard[
    style=4x16,
    setpieces={Qa8,Qb4,Qc1,Qd3,Qb16,Qc12,Qa7,Qc15,qa1,qb14,qc11,qd13,qb6,qc2,qa4,qc5},
    padding=1ex,
  ]
\end{document}

 Open this example in Overleaf.

This xskak example produces the following output:

Further reading

For more information see:

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX