TeXipedia

etoolbox

Provides advanced programming tools and utilities for creating LaTeX classes and packages, with particular focus on e-TeX functionality.

Overview

Serves as a comprehensive toolkit for LaTeX developers and package authors, offering modern programming facilities and enhanced functionality beyond LaTeX's basic capabilities.

  • Implements convenient front-end interfaces to e-TeX primitives, making them more accessible to package developers.
  • Includes robust list processing functions, boolean switches, and hooks for document manipulation.
  • Offers efficient alternatives to traditional LaTeX programming constructs without modifying the core LaTeX kernel.
  • Particularly valuable for class and package authors developing sophisticated LaTeX extensions or customizations.
  • Commonly used in conjunction with other packages to implement advanced document formatting and automation features.

Getting Started

To use etoolbox, include it in your document preamble:

\documentclass{article}
\usepackage{etoolbox}

This package is primarily intended for package authors. The main functionality is:

% Example of a common etoolbox command
\appto\somecommand{additional content}

Examples

Using etoolbox to define and manipulate lists.

\documentclass{article}
\usepackage{etoolbox}

% Define a list
\newcommand{\mylist}{}
\begin{document}
\section{List manipulation with etoolbox}

% Add items to the list
\listadd{\mylist}{Apple}
\listadd{\mylist}{Banana}
\listadd{\mylist}{Cherry}

% Process the list
\noindent Items in my list:\par
\renewcommand*{\do}[1]{\textbullet\ #1\par}
\dolistloop{\mylist}

% Check if an item exists in the list
\noindent Checking list contents:\par
\ifinlist{Banana}{\mylist}{Banana is in the list.}{Banana is not in the list.}\par
\ifinlist{Orange}{\mylist}{Orange is in the list.}{Orange is not in the list.}
\end{document}

Using etoolbox's patching commands to modify existing commands.

\section{Original section}

% Let's patch the \section command to add stars before and after the title
\makeatletter
\pretocmd{\@sect}{\par\noindent Before patching: }{}{}
\apptocmd{\@sect}{\par\noindent After patching}{}{}
\makeatother

\section{Modified section}

Now let's make a more substantial modification to the section command:

% Create a completely patched version that adds stars around the title
\makeatletter
\patchcmd{\@sect}{#7}{$\star$ #7 $\star$}{}{}
\makeatother

\section{Starred section}

This section has stars around its title.

Using etoolbox's boolean facilities and conditional commands.

\documentclass{article}
\usepackage{etoolbox}

% Define some boolean flags
\newbool{draftmode}
\booltrue{draftmode}
\newbool{printimages}
\boolfalse{printimages}

\begin{document}
\section{Using boolean flags}

% Using boolean values to control document behavior
\ifbool{draftmode}
  {\textbf{Draft Mode:} This document is in draft mode.}
  {This document is in final mode.}

\par\bigskip

% Toggle a boolean
\notbool{printimages}{\booltrue{printimages}}{\boolfalse{printimages}}

% Check the toggled value
\ifbool{printimages}
  {Images will be printed.}
  {Images will not be printed.}

\par\bigskip

% Using numeric comparison
\newcommand{\score}{85}
\ifnumcomp{\score}{>}{70}
  {The score of \score\ is passing.}
  {The score of \score\ is failing.}

\par\bigskip

% Using string comparison
\newcommand{\status}{complete}
\ifstrequal{\status}{complete}
  {The project is complete.}
  {The project is still in progress.}
\end{document}