| 1 |
%% This is file `stocksize.sty' |
| 2 |
%% |
| 3 |
%% Copyright (C) 2024–26 by João M. Lourenço <[email protected]> |
| 4 |
%% |
| 5 |
%% This file may be distributed and/or modified under the conditions of |
| 6 |
%% the LaTeX Project Public License, either version 1.3c of this license |
| 7 |
%% or (at your option) any later version. |
| 8 |
%% |
| 9 |
\documentclass[11pt,a4paper]{article} |
| 10 |
|
| 11 |
% ============================================================================ |
| 12 |
% 1. PACKAGES AND SETUP |
| 13 |
% ============================================================================ |
| 14 |
|
| 15 |
% --- Layout & Geometry --- |
| 16 |
\usepackage[margin=1in,showframe]{geometry} |
| 17 |
\usepackage{printlen} % To print lengths (paperwidth, etc) |
| 18 |
\uselengthunit{cm} % Set printlen unit to cm |
| 19 |
|
| 20 |
% --- Graphics & Colors --- |
| 21 |
\usepackage{xcolor} |
| 22 |
\usepackage{tikz} |
| 23 |
\usetikzlibrary{calc} |
| 24 |
|
| 25 |
% --- Code Presentation --- |
| 26 |
\usepackage{minted} |
| 27 |
\setminted{bgcolor=red!20!yellow!5, bgcolorpadding=0.25em} |
| 28 |
|
| 29 |
% --- Tables & Typography --- |
| 30 |
\usepackage{kantlipsum} % Dummy text |
| 31 |
\usepackage{booktabs} % Better tables |
| 32 |
\usepackage{makecell} % Multiline table cells |
| 33 |
|
| 34 |
% --- Functionality --- |
| 35 |
\usepackage{etoolbox} |
| 36 |
\usepackage[colorlinks=true,allcolors=blue]{hyperref} |
| 37 |
|
| 38 |
% --- The stocksize Package --- |
| 39 |
\usepackage{stocksize} |
| 40 |
|
| 41 |
% ============================================================================ |
| 42 |
% 2. DOCUMENT MACROS & COMMANDS |
| 43 |
% ============================================================================ |
| 44 |
|
| 45 |
% --- Shorthands for Documentation --- |
| 46 |
\DeclareRobustCommand{\cs}[1]{\texttt{\char`\\#1}} |
| 47 |
\DeclareRobustCommand{\marg}[1]{\texttt{\{#1\}}} |
| 48 |
\DeclareRobustCommand{\oarg}[1]{\texttt{[#1]}} |
| 49 |
\newcommand{\ssname}{\textsf{stocksize}} |
| 50 |
\newcommand{\blue}[1]{\textcolor{blue!80!black}{#1}} |
| 51 |
\renewcommand{\theadfont}{\bfseries} |
| 52 |
\makeatletter |
| 53 |
\newcommand{\showframeon}{\Gm@showframetrue} |
| 54 |
\newcommand{\showframeoff}{\Gm@showframefalse} |
| 55 |
\makeatother |
| 56 |
|
| 57 |
% --- Helper: Print Table with Current Dimensions --- |
| 58 |
\newcommand{\printpagedims}[1][]{% |
| 59 |
\begin{center} |
| 60 |
\ifstrequal{#1}{t}{}{\vfill}% |
| 61 |
\begin{tabular}{rrr} |
| 62 |
\toprule |
| 63 |
\multicolumn{3}{c}{\thead{Current page dimensions:}} \\ |
| 64 |
\midrule[0.2pt] |
| 65 |
& \thead{Height (cm)} & \thead{Width (cm)}\\ |
| 66 |
\midrule |
| 67 |
\thead[r]{Paper} & \color{blue}\printlength{\paperheight} & \color{blue}\printlength{\paperwidth} \\ |
| 68 |
\thead[r]{Text} & \color{blue}\printlength{\textheight} & \color{blue}\printlength{\textwidth} \\ |
| 69 |
\bottomrule |
| 70 |
\end{tabular} |
| 71 |
\ifstrequal{#1}{c}{\vfill}{}% |
| 72 |
\end{center} |
| 73 |
} |
| 74 |
|
| 75 |
% --- Helper: Draw Visual Rulers (TikZ) --- |
| 76 |
\newcommand{\myrules}{ |
| 77 |
\begin{tikzpicture}[remember picture, overlay] |
| 78 |
% Reference: Top-Left Corner |
| 79 |
\coordinate (NW) at (current page.north west); |
| 80 |
|
| 81 |
% Offsets: 1cm from margins for ruler baselines |
| 82 |
\coordinate (H0) at ($(NW)+(0,-1cm)$); % Horizontal ruler |
| 83 |
\coordinate (V0) at ($(NW)+(1cm,0)$); % Vertical ruler |
| 84 |
|
| 85 |
% --- Horizontal Ruler (Orange) --- |
| 86 |
\draw[orange,line width=0.5pt] (H0) -- ($(H0)+(\paperwidth,0)$); |
| 87 |
|
| 88 |
% CM Ticks & Numbers |
| 89 |
\foreach \i in {0,...,30}{ |
| 90 |
\draw[orange,line width=0.5pt] |
| 91 |
($(H0)+(\i cm,0)$) -- ($(H0)+(\i cm,3mm)$); |
| 92 |
\node[font=\scriptsize,anchor=south,text=orange] |
| 93 |
at ($(H0)+(\i cm,3mm)$) {\i}; |
| 94 |
} |
| 95 |
% MM Ticks |
| 96 |
\foreach \j in {0,...,300}{ |
| 97 |
\pgfmathtruncatemacro{\m}{mod(\j,10)} |
| 98 |
\ifnum\m=0\relax\else |
| 99 |
\draw[orange,line width=0.3pt] |
| 100 |
($(H0)+(\j mm,0)$) -- ($(H0)+(\j mm,1.5mm)$); |
| 101 |
\fi |
| 102 |
} |
| 103 |
|
| 104 |
% --- Vertical Ruler (Orange) --- |
| 105 |
\draw[orange,line width=0.8pt] (V0) -- ($(V0)+(0,-\paperheight)$); |
| 106 |
|
| 107 |
% CM Ticks & Numbers |
| 108 |
\foreach \i in {0,...,40}{ |
| 109 |
\draw[orange,line width=0.8pt] |
| 110 |
($(V0)+(0,-\i cm)$) -- ($(V0)+(3mm,-\i cm)$); |
| 111 |
\node[font=\scriptsize,anchor=west,text=orange] |
| 112 |
at ($(V0)+(3mm,-\i cm)$) {\i}; |
| 113 |
} |
| 114 |
% MM Ticks |
| 115 |
\foreach \j in {0,...,400}{ |
| 116 |
\pgfmathtruncatemacro{\m}{mod(\j,10)} |
| 117 |
\ifnum\m=0\relax\else |
| 118 |
\draw[orange,line width=0.5pt] |
| 119 |
($(V0)+(0,-\j mm)$) -- ($(V0)+(1.5mm,-\j mm)$); |
| 120 |
\fi |
| 121 |
} |
| 122 |
\end{tikzpicture} |
| 123 |
} |
| 124 |
|
| 125 |
% ============================================================================ |
| 126 |
% 3. MAIN DOCUMENT |
| 127 |
% ============================================================================ |
| 128 |
|
| 129 |
\begin{document} |
| 130 |
|
| 131 |
\showframeoff |
| 132 |
|
| 133 |
\title{The \ssname\ package} |
| 134 |
\author{João M. Lourenço\\\url{https://github.com/joaomlourenco/stocksize}} |
| 135 |
\date{\filedate\ (v\fileversion)} |
| 136 |
|
| 137 |
\maketitle |
| 138 |
|
| 139 |
\begin{abstract} |
| 140 |
The \ssname\ package provides commands to modify and restore the physical stock (paper) size within a \LaTeX{} document. It supports nested page size changes using a stack-based approach (LIFO) and allows for automatic margin preservation. The package integrates seamlessly with the \textsf{geometry} package, ensuring that logical layout changes are always synchronized with the physical output page dimensions. |
| 141 |
\end{abstract} |
| 142 |
|
| 143 |
\tableofcontents |
| 144 |
|
| 145 |
% -------------------------------------------------------------------------- |
| 146 |
\section{Introduction} |
| 147 |
% -------------------------------------------------------------------------- |
| 148 |
The \textsf{geometry} package provides a comprehensive interface for specifying page layout parameters such as margins and text block dimensions. However, changing the layout mid-document often leaves the physical paper size (the “canvas” used by PDF viewers or printers) out of sync with the logical text area. |
| 149 |
|
| 150 |
The \ssname\ package provides \textbf{robust control of the physical PDF page size} (the \textbf{stock size}) inside a LaTeX document. It complements the \texttt{geometry} package by managing \textbf{paper size transitions} reliably, including \textbf{nested changes}, while delegating margin and layout calculations to \texttt{geometry}. |
| 151 |
|
| 152 |
\subsection*{Terminology} |
| 153 |
\begin{itemize} |
| 154 |
\item \textbf{Stock size}: The physical dimensions of the sheet (e.g., A4, Letter) as interpreted by the output driver (PDF or Printer). |
| 155 |
\item \textbf{Paper size}: The logical page dimensions defined within the \LaTeX{} document variables (e.g., \cs{paperwidth}). |
| 156 |
\end{itemize} |
| 157 |
|
| 158 |
% -------------------------------------------------------------------------- |
| 159 |
\section{User Interface} |
| 160 |
% -------------------------------------------------------------------------- |
| 161 |
|
| 162 |
% .......................................................................... |
| 163 |
\subsection{Loading the package} |
| 164 |
% .......................................................................... |
| 165 |
To use the package, load it in the preamble: |
| 166 |
\begin{minted}{latex} |
| 167 |
\usepackage[options]{stocksize} |
| 168 |
\end{minted} |
| 169 |
The package automatically loads \textsf{geometry} (if not already loaded) and works with pdf\LaTeX, Xe\LaTeX, and Lua\LaTeX. |
| 170 |
|
| 171 |
\subsubsection{Package Options} |
| 172 |
\begin{description} |
| 173 |
\item[\texttt{patch-geometry}] (boolean, default: \texttt{false}) — When enabled, this option patches the standard \cs{newgeometry}, \cs{restoregeometry}, and \cs{loadgeometry} commands to automatically synchronize the physical stock size whenever the layout changes. |
| 174 |
\end{description} |
| 175 |
|
| 176 |
% .......................................................................... |
| 177 |
\subsection{Features} |
| 178 |
% .......................................................................... |
| 179 |
\begin{description} |
| 180 |
\item [Dynamic synchronization:] Automatically updates the physical paper dimensions when the layout changes. |
| 181 |
\item [Nested layouts:] Implements a Last-In-First-Out (LIFO) stack for page geometries, which allows nesting multiple \cs{newstocksize} (or \cs{newgeometry}) calls and restoring them in reverse order. |
| 182 |
\item [Margin preservation:] The \texttt{keepmargins} option allows resizing the paper while maintaining the existing margin offsets. |
| 183 |
\item [Seamless integration:] Built directly on top of the \textsf{geometry} package. |
| 184 |
\end{description} |
| 185 |
|
| 186 |
% .......................................................................... |
| 187 |
\subsection{Changing the stock size} |
| 188 |
% .......................................................................... |
| 189 |
To start a new page with specific stock (paper) dimensions, use the \cs{newstocksize} command. To return to the previous layout, use \cs{restorestocksize}. |
| 190 |
|
| 191 |
\begin{description} |
| 192 |
\item[] \cs{newstocksize}\marg{options} |
| 193 |
Starts a new page with the specified stock size. The \texttt{options} argument accepts: |
| 194 |
\begin{itemize} |
| 195 |
\item \texttt{keepmargins}: Preserves the current top, bottom, left, and right margins in the new layout. |
| 196 |
\item \emph{Geometry options}: Any valid \textsf{geometry} key-value pair (e.g., \texttt{layoutsize=\{...\}}, \texttt{margin=...}). These are passed directly to \cs{newgeometry}. |
| 197 |
\end{itemize} |
| 198 |
|
| 199 |
\item[] \cs{restorestocksize} |
| 200 |
Ends the current layout and restores the previous stock size and geometry settings from the stack. |
| 201 |
\end{description} |
| 202 |
|
| 203 |
% .......................................................................... |
| 204 |
\subsection{Nesting example} |
| 205 |
% .......................................................................... |
| 206 |
Multiple stock sizes can be nested. Each \cs{restorestocksize} call resumes the dimensions active before the most recent change. |
| 207 |
|
| 208 |
\begin{minted}{latex} |
| 209 |
% 1. Default page (e.g., A4) |
| 210 |
This page has the default size. |
| 211 |
|
| 212 |
% 2. Change to 15x10cm |
| 213 |
\newstocksize{layoutsize={15cm,10cm}, margin=1.5cm} |
| 214 |
This page is 15cm x 10cm. |
| 215 |
|
| 216 |
% 3. Nested change to 20x20cm |
| 217 |
\newstocksize{layoutsize={20cm,20cm}, margin=4.0cm} |
| 218 |
This page is 20cm x 20cm. |
| 219 |
|
| 220 |
% 4. Restore previous (returns to 15x10cm) |
| 221 |
\restorestocksize |
| 222 |
Back to 15cm x 10cm. |
| 223 |
|
| 224 |
% 5. Restore original (returns to A4) |
| 225 |
\restorestocksize |
| 226 |
Back to default A4. |
| 227 |
\end{minted} |
| 228 |
|
| 229 |
% -------------------------------------------------------------------------- |
| 230 |
\section{Notes and Limitations} |
| 231 |
% -------------------------------------------------------------------------- |
| 232 |
\begin{itemize} |
| 233 |
\item \textbf{Drivers}: While this package supports PDF output engines (pdfTeX, LuaTeX, XeTeX), some DVI drivers may require additional configuration or special commands to reflect size changes in viewers. |
| 234 |
\item \textbf{Stack limit}: Extremely deep nesting may hit \TeX{} register limits, though this is unlikely in typical use cases. |
| 235 |
\item \textbf{Packages}: External packages that cache paper dimensions at the start of the document may not immediately reflect changes made by \ssname. |
| 236 |
\end{itemize} |
| 237 |
|
| 238 |
% -------------------------------------------------------------------------- |
| 239 |
\section{License} |
| 240 |
% -------------------------------------------------------------------------- |
| 241 |
This work may be distributed and/or modified under the conditions of the |
| 242 |
\LaTeX{} Project Public License, version 1.3c or later. |
| 243 |
The latest version is available at \url{https://www.latex-project.org/lppl/}. |
| 244 |
|
| 245 |
% -------------------------------------------------------------------------- |
| 246 |
\section{Version History} |
| 247 |
% -------------------------------------------------------------------------- |
| 248 |
\begin{description} |
| 249 |
\item[v2.0.1 — January 2026:] Fixed some bugs on the documentation date and version. |
| 250 |
\item[v2.0.0 — December 2025:] Major rewrite. |
| 251 |
\begin{itemize} |
| 252 |
\item Fixed problem with the location of the page numbers after a \cs{restorestocksize} command. |
| 253 |
\item Redefined \cs{newgeometry} and \cs{restoregeometry} to operate as a LIFO stack, enabling robust nesting of layouts. |
| 254 |
\item Added automatic synchronization between logical and physical paper sizes. |
| 255 |
\item Added \texttt{patch-geometry} package option. |
| 256 |
\end{itemize} |
| 257 |
\item[v1.0.4 — November 2025:] Improved documentation. |
| 258 |
\item[v1.0.3 — November 2024:] Minor fix in the documentation. |
| 259 |
\item[v1.0.2 — November 2024:] Added \texttt{keepmargins} option to preserve margins when changing stock size. |
| 260 |
\item[v1.0.1 — November 2024:] Initial version. |
| 261 |
\end{description} |
| 262 |
|
| 263 |
% -------------------------------------------------------------------------- |
| 264 |
\section{A Real Example of Different Page Sizes} |
| 265 |
% -------------------------------------------------------------------------- |
| 266 |
\showframeon |
| 267 |
\myrules |
| 268 |
\kant[1-2] |
| 269 |
|
| 270 |
%- SCENARIO 1: Small Page --------------------------------------------- |
| 271 |
\printpagedims[b] |
| 272 |
\newstocksize{layoutsize={11cm,9cm}, includeheadfoot, margin=1cm} |
| 273 |
\myrules |
| 274 |
\printpagedims[t] |
| 275 |
|
| 276 |
\bigskip |
| 277 |
\kant[1] |
| 278 |
\bigskip |
| 279 |
|
| 280 |
%- SCENARIO 2: Wide Strip with no margins (Nested) --- |
| 281 |
\printpagedims[c] |
| 282 |
\newstocksize{layoutsize={24cm,7cm}, margin=0cm} |
| 283 |
\myrules |
| 284 |
|
| 285 |
% Visual markers |
| 286 |
\foreach \i in {1,...,100} {\i\ } |
| 287 |
\printpagedims[c] |
| 288 |
\foreach \i in {1,...,100} {\i\ } |
| 289 |
|
| 290 |
\restorestocksize |
| 291 |
% ------------------------------------------------------------------- |
| 292 |
|
| 293 |
%- SCENARIO 3: Wide Strip with header and footer (Nested) ----------- |
| 294 |
\newstocksize{layoutsize={24cm,10cm}, includeheadfoot, margin=0cm} |
| 295 |
\myrules |
| 296 |
|
| 297 |
\foreach \i in {1,...,100} {\i\ } |
| 298 |
\printpagedims[c] |
| 299 |
\foreach \i in {1,...,100} {\i\ } |
| 300 |
|
| 301 |
\restorestocksize |
| 302 |
% ------------------------------------------------------------------- |
| 303 |
|
| 304 |
\textbf{And} \verb!\restorestocksize! \textbf{will restore the previous layout.} |
| 305 |
\printpagedims[b] |
| 306 |
|
| 307 |
\restorestocksize |
| 308 |
%- END SCENARIO 1 ----------------------------------------------------- |
| 309 |
|
| 310 |
\printpagedims[t] |
| 311 |
\textbf{Normal page layout and size.} |
| 312 |
\kant[1-5] |
| 313 |
|
| 314 |
\end{document} |