Typevia
Graphics and Diagrams

Introduction to Chess in LaTeX

Whether you are writing a chess book, a club newsletter, or documenting your own games, LaTeX provides the most professional way to typeset chess notation and diagrams. Instead of relying on low-resolution images or manual formatting, LaTeX uses specialized packages to generate crisp, vector-based chessboards and perfectly aligned game scores.

The most powerful and modern way to handle chess in LaTeX is through a combination of three packages:

  1. skak: The foundation for chess notation and move processing.
  2. xskak: An extension of skak that adds more flexibility and better handling of game histories.
  3. chessboard: A highly customizable package specifically for printing the board.

In this guide, we will focus on using xskak and chessboard to create professional-grade chess content.


Setting Up Your Document

To get started, you need to include the necessary packages in your LaTeX preamble. While you can use skak alone, xskak provides better compatibility with modern LaTeX features.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% Main packages for chess
\usepackage{xskak}
\usepackage{chessboard}

\begin{document}
% Content goes here
\end{document}

Note: Most TeX distributions (like TeX Live or MiKTeX) include these packages by default. If you get an error saying the style file is not found, you may need to use your package manager to install xskak and chessboard.


Creating Chess Diagrams

The simplest way to display a board is to use the \chessboard command. By default, this command displays the starting position of a standard game.

The Starting Position

To show the board at the beginning of a game, simply use:

\section*{The Starting Position}
\chessboard

Displaying Specific Positions (FEN)

In chess programming and publishing, FEN (Forsyth-Edwards Notation) is the standard for representing a specific board position in a single line of text. The chessboard package allows you to input a FEN string to display any legal or illegal position.

\section*{A Famous Puzzle}
% The FEN for the "Evergreen Game" finish
\chessboard[setfen=1rb4r/pk1q1p1p/1p1p1bp1/1n1P4/5N2/1P1BP3/P1Q2PPP/2R2RK1 w - - 0 1]

Customizing Board Appearance

You can change the size, colors, and labels of the board using the \setchessboard command or by passing options directly to \chessboard.

\chessboard[
    showlabels=false,   % Hide a-h and 1-8 labels
    boardfontsize=15pt, % Change the size of the pieces
    setwhite={Ke1, Qd1, Ra1, Rh1}, % Only show specific white pieces
    setblack={Ke8, Qd8, Ra8, Rh8}  % Only show specific black pieces
]

Recording and Typesetting Notation

While you can type move notation manually (e.g., "1. e4 e5"), using the xskak package allows LaTeX to "understand" the game. This means LaTeX tracks the position of every piece as you type the moves.

The \mainline Command

The \mainline command is used to input the primary moves of a game.

\section*{The Scholar's Mate}
\newchessgame % Initialize a new game state
\mainline{1. e4 e5 2. Qh5 Nc6 3. Bc4 Nf6?? 4. Qxf7#}

\begin{center}
    \chessboard
\end{center}

When you use \mainline, LaTeX updates the internal board state. If you call \chessboard after a \mainline command, it will automatically show the position reached after those moves.

Adding Commentary

In chess literature, we often see moves followed by text analysis. You can interject text into your notation using the \variation command or simply by closing and reopening the \mainline command.

\newchessgame
\mainline{1. e4 e5 2. Nf3} 
White develops the Knight and attacks the e5 pawn.
\mainline{2... Nc6} 
Black defends. 

Note: When resuming a mainline starting with a Black move, use the triple dot syntax (2... Nc6) so LaTeX knows you are continuing the second half of the move.


Highlighting Moves and Squares

Instructional books often use arrows or colored squares to highlight threats or plans. The chessboard package provides a robust "color layer" system for this.

Squares and Arrows

You can highlight squares using markfield and draw arrows using markarrow.

\chessboard[
    setfen=r1bqkb1r/pppp1ppp/2n2n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 0 1,
    % Highlight the 'c4' bishop and its target 'f7'
    markfields={f7},
    color=red,
    opacity=0.5,
    % Draw an arrow from c4 to f7
    markarrow={c4}{f7},
    color=blue,
    markfield={c4}
]

Common Styles

  • markfield: Colors a specific square.
  • markfields: Colors a list of squares.
  • markarrow: Draws an arrow from the center of the first square to the center of the second.
  • color: Sets the color for the following marks.

Best Practices and Common Pitfalls

1. Case Sensitivity

In LaTeX chess packages, piece notation is case-sensitive.

  • Uppercase (K, Q, R, B, N) refers to White pieces.
  • Lowercase (k, q, r, b, n) refers to Black pieces. This is especially important when writing FEN strings or using setwhite/setblack.

2. Using Long Games

For very long games, consider breaking the notation into logical chunks. You can save the state of a game using \storechessgame{name} and resume it later with \restorechessgame{name}. This keeps your code organized.

3. Font Limitations

Not all LaTeX engines handle chess fonts the same way. If you are using XeLaTeX or LuaLaTeX, you might need to ensure your distribution's chess fonts are properly mapped. For beginners, sticking with pdfLaTeX is often the smoothest experience for chess typesetting.

4. Space Management

Chessboards can be large. Use the smallboard or tinyboard options if you need to fit multiple diagrams on a single page, or wrap the board in a figure environment to allow it to "float" to the best position.

\chessboard[tinyboard]

Summary Example

Here is how a complete analysis snippet might look in your document:

\section*{The Ruy Lopez}

The Ruy Lopez is one of the oldest and most studied openings in chess history.

\newchessgame
\mainline{1. e4 e5 2. Nf3 Nc6 3. Bb5}

\begin{center}
    \chessboard[
        % Highlight the pressure on the knight
        markarrow={b5}{c6},
        color=green,
        markfield={e5}
    ]
    \\[1ex] \textit{Position after 3. Bb5}
\end{center}

White develops the bishop to put indirect pressure on the e5 pawn by attacking its defender. Black's most common response is \mainline{3... a6}, forcing the bishop to decide its future.