atbegshi
Enables execution of custom commands at specific page output points during document compilation, providing fine-grained control over page processing.
Overview
Provides sophisticated control over document processing by allowing precise timing of command execution during the page output phase. This low-level functionality is particularly valuable for package developers and advanced users who need to modify or enhance LaTeX's page-building behavior.
- Offers hooks for executing commands just before pages are physically written to the output.
- Serves as a modern, more robust alternative to the older everyshi package.
- Commonly used for implementing watermarks, headers, background graphics, and other page-level modifications.
- Compatible with both LaTeX and plain TeX, making it versatile for different TeX-based workflows.
- Leverages e-TeX extensions when available for improved functionality.
Getting Started
To use atbegshi
, include it in your document preamble:
\documentclass{article}
\usepackage{atbegshi}
The package works with both LaTeX and plain TeX documents. If you're using plain TeX, load it with:
\input atbegshi.sty
Examples
Adding a watermark to every page of a document.
\documentclass{article}
\usepackage{atbegshi}
\usepackage{graphicx}
\usepackage{eso-pic}
% Define a watermark command that will be executed at shipout time
\AtBeginShipout{
\AtBeginShipoutUpperLeft{%
\put(\paperwidth-50,\paperheight-70){\rotatebox{45}{\textcolor{gray}{\huge DRAFT}}}
}
}
\begin{document}
\section{Introduction}
This is a sample document with a watermark added using the atbegshi package.
The watermark is added at shipout time, which means it's applied just before
each page is actually output.
\newpage
This is the second page, which also has the same watermark applied automatically.
The AtBeginShipout command ensures the watermark appears on every page.
\end{document}
Adding page numbers in a custom format at the bottom of each page.
\documentclass{article}
\usepackage{atbegshi}
\usepackage{tikz}
% Remove default page numbering
\pagestyle{empty}
% Add custom page numbering at shipout time
\AtBeginShipout{
\AtBeginShipoutUpperLeft{%
\put(0.5\paperwidth,-0.95\paperheight){%
\tikz\node[draw=black, fill=lightgray, rounded corners, text=black] {Page \thepage\ of \pageref{LastPage}};
}
}
}
% Create a label for the last page
\AtEndDocument{\label{LastPage}}
\begin{document}
\section{First Section}
This document demonstrates custom page numbering added with atbegshi.
The page numbers appear in gray boxes at the bottom of each page.
\newpage
\section{Second Section}
This is the second page of our document. The page numbering is consistent
across all pages because it's handled at shipout time.
\newpage
\section{Third Section}
This is the third and final page of our document.
\end{document}
Implementing a simple page counter that prints to the terminal during compilation.
\documentclass{article}
\usepackage{atbegshi}
% Set up a counter for pages
\newcounter{pagecount}
% At each shipout, increment the counter and print a message
\AtBeginShipout{
\stepcounter{pagecount}
\typeout{Processing page \thepagecount}
}
\begin{document}
\section{First Section}
This document demonstrates using atbegshi to track page processing.
During compilation, a message will be printed to the terminal for each page.
\newpage
\section{Second Section}
This is page 2 of our document. When this page is processed, a message will
be printed to the terminal.
\newpage
\section{Third Section}
This is page 3 of our document. The atbegshi package allows us to execute
code at shipout time, which is when each page is finalized for output.
\end{document}