A page is a stack of boxes and TeX only lays out one of them
TeX knows about the text block: a rectangle \textwidth by \textheight, subdivided by \columnsep in two-column work. Everything else — the paper size, the header, the footer, the margin notes — is expressed as a distance from an edge of that block. That is why the layout of a document is a handful of lengths rather than a canvas you draw on, and why a layout package can rewrite the whole page from five numbers.
Horizontally, the distance from the left edge of the paper to the left of the text block is 1in + \hoffset + \oddsidemargin (and \evensidemargin on the even pages of a two-sided document). Vertically, the top of the header box sits 1in + \voffset + \topmargin below the top edge, then come \headheight, \headsep, the text block itself, and \footskip, which is measured not from the bottom of the block but from the last baseline of text to the baseline of the footer.
That one-inch term is the single most misunderstood fact about LaTeX page geometry, and it is inherited from TeX's assumption that output is being prepared for a typesetter working on a virtual page inset by an inch. It is why \oddsidemargin=0pt does not put text at the edge of the paper but 25.4mm in, and why hand-set margins look sane only after you subtract an inch. Everything that adds negative values in old class files and templates is fighting that constant.
Consequence worth knowing before you touch anything: if the sum of the vertical parts is larger than the paper, TeX does not complain about the geometry, it complains about each overfull page. The log message is a badbox line with Height too large or a big Overfull \vbox — the classic one being 115.7pt too high — and the fix is the geometry, not the paragraph. This is also what the heightrounded option of geometry prevents, by rounding \textheight down to a whole number of \baselineskips.
\begin{array}{rcl}\text{left edge to text} & = & \text{one inch} + \texttt{hoffset} + \texttt{oddsidemargin} \\ \text{top edge to header} & = & \text{one inch} + \texttt{voffset} + \texttt{topmargin}\end{array}
geometry: what each key actually writes
\usepackage[margin=2.5cm]{geometry} is the whole interface most documents need, and it is a calculator rather than a switch: you state the distances you care about, and the package solves for \textheight, \textwidth, the side and top lengths and the header and footer skips, then prints what it chose into the log so you can read it back.
Edge keys: margin for all four, hmargin and vmargin for a pair, left/right/top/bottom individually, and inner/outer which mean the binding edge and the fore-edge when the document is twoside. bindingoffset adds room at the inner edge without moving anything else, which is the only correct way to prepare a thesis for a print shop. The block keys are the other direction: textwidth=/width= and textheight=/height= state the block itself and let the package choose the margins, and total= states the block plus header and footer.
The inclusion keys are where most confusion lives, because they change what a word like top means. By default the margins apply to the text block alone: top=3cm means 3cm from the paper edge to the first line of text, and the header is placed inside that space. With includehead, the header is inside the 3cm and the text block starts lower; includefoot does the same for the footer, and includeheadfoot both. So a document whose header collides with the body after you add \pagestyle{headings} does not need more \headsep — it needs includehead.
A second family of keys sets the block by ratio rather than by absolute measure, which is what classical page design does: hdivide={*,12.5cm,*} and vdivide={...} lay out named strips, and ratio=9:16 (or hratio, vmarginratio) picks the block from the paper by proportion. vcenter/centering decide whether the block is vertically centred, and invertedmargin is a debugging toy that swaps margins for text so you can see the geometry at a glance.
\begin{array}{lll}\text{key} & \text{writes} & \text{counts the header?} \\\hline \texttt{top=3cm} & \texttt{topmargin} & \text{no, unless includehead} \\ \texttt{textwidth=12cm} & \texttt{textwidth} & \text{irrelevant} \\ \texttt{includefoot} & \text{reuses footskip} & \text{yes, the footer}\end{array}
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
% The one package that owns page geometry. Options at load time are the same
% as \geometry{...} in the preamble, split over lines here for legibility.
\usepackage[%
a4paper, % paper: letterpaper, b5paper, a5paper, ...
margin=2.5cm, % all four edges at once
headheight=14pt, % fancyhdr will ask for this explicitly
headsep=16pt,
footskip=28pt,
includeheadfoot, % the header/footer live INSIDE the 2.5cm
bindingoffset=8mm, % extra room on the inner side, for printing
twoside, % inner/outer instead of left/right
heightrounded, % an whole number of lines per page
showframe, % draw the box edges -- remove for the final build
]{geometry}
\begin{document}
% What the keys resolved to: look for these lines in the .log file.
\typeout{geometry gave: textwidth=\the\textwidth, textheight=\the\textheight,
topmargin=\the\topmargin, oddsidemargin=\the\oddsidemargin}
\section*{A page is a decision, not a canvas}
The text block is the only thing TeX lays out; everything else on this page
is a distance from an edge of the paper to a side of that block. Load
\texttt{geometry}, state the block once, and stop touching it: any
\texttt{\textbackslash vspace} or \texttt{\textbackslash enlargethispage} used
to repair one bad page will move two pages later, because the reflow changes
line breaking from there on.
\end{document}
One geometry for part of a document, and seeing it on paper
The package interface is not frozen at load time: \geometry{headheight=15pt} in the preamble adds or changes settings, and \newgeometry{...} / \restoregeometry bracket a region with its own layout — a wide table page, a landscape figure, a poem that wants a narrower measure. \newgeometry issues a \clearpage for you, so it can only start at a page boundary: the region will begin at the top of the next page, which surprises people who expected it to begin mid-page.
\savegeometry{wide} and \loadgeometry{wide} name a configuration so a long appendix can switch between two known states instead of re-listing six keys each time, and they also make the change reviewable in a diff. This is the difference between layout as a design decision and layout as an argument with the page.
To see what you have, three tools. showframe draws a rule at every boundary — text block, header, footer, margin-note box, paper edge — which is the fastest way to find out why something is where it is. The layout package prints an annotated dummy page (\layout and friends) showing the block, the lines, the margins and the depth of the last page, which is the diagram to keep in a folder when a press asks for specifications. And \typeout{\the\textwidth, \the\textheight} in the preamble writes the actual numbers into the log, since geometry has not resolved them at the moment your source says margin=2.5cm.
What not to do, in order of how often it is attempted: change \textwidth after the preamble and expect the page to reflow correctly (floats, header rules and caption widths were already computed), set margins with \oddsidemargin and forget the inch, use \enlargethispage as a spacing tool rather than a one-page repair, and reach for the raw class lengths when the document loads geometry — the package will simply overwrite them.
Paper size, and the driver that has to agree
Paper size is a class option in the standard classes — \documentclass[a4paper]{article}, and equally letterpaper (the default on US installations), a5paper, b5paper, legalpaper, executivepaper, plus landscape to rotate the block — and it is also a geometry key, which wins when both are given. Either route sets \paperwidth and \paperheight; the geometry key additionally recomputes the margins, which the class option alone does not.
Setting the two lengths is not the same as telling the output driver. pdfTeX writes the media box from its own primitives (\pdfpagewidth, \pdfpageheight), and dvips-based routes read a papersize special; when the two disagree you get a document that looks right on screen and prints cropped, or an A4 block centred on a letter sheet. geometry handles the driver side for you if you let it — its paper=/a4paper keys set both the lengths and the driver sizes, and it accepts the driver options pdftex, xetex, luatex, dvips, dvipdfm if autodetection ever fails.
The other half of the decision is the class. A press or a university that supplies a class file has already chosen the block, the leading and often the fonts, and its documentation will say whether to pass a4paper or use \geometry at all; overriding a house class with your own geometry is the layout equivalent of overriding a journal bibliography style by hand. If there is no class file, the standard route is: paper size as a class option, margins through geometry, nothing else.
For books there is a second tradition, older than geometry: KOMA-script's typearea computes a classically proportioned text block from the paper and a division number (\usepackage[DIV=9,BCOR=15mm]{typearea}, where a larger DIV means a smaller block and wider margins, and BCOR is the binding correction), and memoir does the same with its own commands: \settypeblocksize{245mm}{145mm}{*}, \setlrmargins{*}{*}{1.4}, \setulmargins{*}{*}{1.2} and then \checkandfixthelayout, which is mandatory after any of them because the package must re-derive every dependent length.
% Two ways to get a classically proportioned page without geometry.
\documentclass[12pt,a4paper,twoside,openright]{book}
% Route A: KOMA-script's typearea computes the text block from the paper and
% a division ratio (the bigger DIV, the smaller the block).
\usepackage[DIV=9,BCOR=15mm]{typearea}
% Route B: the raw TeX lengths, which is what every layout package writes.
% Remember the one-inch assumption: \oddsidemargin is measured from 1in in.
% \setlength{\textwidth}{14.5cm}
% \setlength{\textheight}{24.5cm}
% \setlength{\oddsidemargin}{-0.6cm} % 1in - 0.6cm from the paper edge
% \setlength{\topmargin}{0.5cm} % 1in + 0.5cm down to the header
% \setlength{\headheight}{14pt}
% \setlength{\headsep}{18pt}
% \setlength{\footskip}{26pt}
% \addtolength{\textheight}{-\headheight}
% \addtolength{\textheight}{-\headsep}
\usepackage{setspace}
\usepackage{microtype}
\begin{document}
\onehalfspacing % \setstretch under the hood, applied for you
\raggedbottom % do not stretch the last page of a chapter
\chapter{Why heightrounded matters}
If \texttt{textheight} is not an exact multiple of \texttt{baselineskip}, TeX
either overflows the page by a fraction of a line -- the log calls it
\texttt{Height too large} in a \texttt{badbox} -- or leaves the last baseline
short. Rounding the height to a whole number of lines is the difference
between a stable page grid and one that shifts when you edit a paragraph.
\end{document}
Line spacing: the register, the factor and the package
The distance from baseline to baseline is \baselineskip, and it is chosen by the size switch in force: 10pt type on 12pt leading in the standard article class, 12pt on 14.5pt at the 12pt base. When people say a document is "too tight" they are almost always describing that ratio, not the size, so the correct object to change is the leading, and it should be changed once, globally.
The raw mechanism is \linespread{factor}\selectfont. \linespread only stores a multiplier, and the new value is applied when a font size is next selected — which is why a \linespread{1.05} without \selectfont does nothing visible, and why the pair belongs in the preamble where the class selects \normalfont anyway. \selectfont is not decoration; leaving it out is the most common reason a leading change appears to be ignored.
The setspace package wraps the same mechanism and adds the parts a raw multiplier misses: \setstretch{1.18} applies the factor for you, \onehalfspacing and \doublespacing give the two word-processor conventions, and the spacing/doublespace environments scope a change to one block. The named settings are deliberately not 1.5 and 2.0 — they are tuned against the leading the class already supplies, which is why double-spaced LaTeX looks looser than double-spaced output from a word processor and satisfies the same requirement in a submission guideline. setspace also keeps captions and footnotes on their own, tighter spacing so a doubled body does not double the marginal text.
Two neighbouring registers finish the vertical story. \parskip is the gap between paragraphs and belongs to the paragraph-style decision made in the previous lesson, not to line spacing; stretch and shrink in it (0.45\baselineskip plus 2pt minus 1pt) is what lets pages breathe. And \lineskip with \lineskiplimit takes over when two consecutive lines are too tall to be separated by the nominal baseline distance — the reason a page with a run of nested fractions can look uneven while the rest of the document is fine.
\begin{array}{c}\texttt{topmargin}\;(\text{from one inch down}) \\ \hline \texttt{headheight} \to \texttt{headsep} \to \underbrace{\texttt{textheight}}_{\text{the text block}} \\ \hline \texttt{footskip}\;(\text{last baseline to the footer baseline})\end{array}
Two-sided pages, running heads and margin notes
A twoside document has two different text positions, so the class asks for \oddsidemargin and \evensidemargin separately and geometry speaks of inner and outer instead. The typographic reason for the split is the binding: the inner margin is the one the reader's thumbs fight, so it should be the larger, and a symmetric left/right layout on a bound document reads as cramped. Add bindingoffset=10mm when a print shop asks for a specific glue allowance rather than a visual margin.
Running heads come from \markboth{left-side text}{right-side text} (or \markright) fed by \pagestyle{headings}, and the class fills them at chapter and section headings. In a two-sided document the left header normally carries the chapter and the right the section, which is why the classes write both marks at every heading — and why fancyhdr, with \fancyhead[LE,RO]{\nouppercase{\leftmark}}-style slots, is the honest way to change it. Its famous warning, headheight is too small (12.0pt), is not a nuisance: it is telling you the header content does not fit the box the geometry reserved, and the fix is headheight=14pt in geometry, which moves the text block down as well.
Margin notes are a box with their own three lengths: \marginparwidth, \marginparsep and \marginparpush, and on a two-sided page the usable width differs left from right, so a note that fits on a recto page can hang on a verso one. Plain \marginpar puts the note at the current line and is a magnet for collisions; the marginnote package places notes independently of line breaks and marginfix re-orders them so they stop overlapping, and KOMA-script offers \marginline{...} which does the alignment work for you.
If your document wants text and margin notes at the same vertical position with real paragraphs in the margin, that is a different layout entirely — Tufte-style — and the packages that own it are tufte-latex or KOMA's wide-margin classes. In that world the page geometry and the \marginpar behaviour are coupled by design, and hand-tuning either length breaks the other.
\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[a4paper,top=3cm,bottom=3.5cm,left=2.5cm,right=2.5cm]{geometry}
\usepackage{setspace}
\usepackage{needspace}
\usepackage{marginnote}
\usepackage{fancyhdr}
\setstretch{1.18}
\setlength{\parskip}{0.45\baselineskip plus 2pt minus 1pt}
\setlength{\parindent}{0pt}
% widows and clubs: one line of a paragraph left behind is worth a page break
\widowpenalty=10000
\clubpenalty=10000
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\small\nouppercase{\leftmark}}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\begin{document}
% \newgeometry issues a \clearpage of its own, so it can only be used after
% \begin{document} -- never in the preamble, where \geometry{...} belongs.
\newgeometry{left=4cm,right=2cm} % a wide-inner region for asides
\section{Margins are set once}
A wide-margin region for a quotation, then the document geometry back again:
\marginnote{Margin notes are a box of their own: \texttt{marginparwidth}
sets the width, \texttt{marginparsep} the gap to the text, and
\texttt{marginparpush} the minimum distance between two of them.}
\begin{quote}
Quoted material inherits the text block, not the page. If the quote wants
different margins, that is an environment with its own \leftmargin,
not a change to the geometry.
\end{quote}
\restoregeometry
\section{Space that must not collapse}
\needspace{7\baselineskip}
This heading and the six lines below it belong together; \texttt{needspace}
starts a new page rather than splitting them, which is what
\verb|\nopagebreak| and a chain of forced breaks only imitate.
\end{document}
Penalties: how a page decides where it ends
Page breaking is an optimisation, and the numbers it optimises are public. \widowpenalty prices a line left at the top of the next page, \clubpenalty the first line stranded at the bottom of the previous one, \displaywidowpenalty the line before a displayed equation, \brokenpenalty a page break after a hyphenated line and \interlinepenalty every intermediate line of a split paragraph. TeX's own defaults are mild; the standard advice for prose is \widowpenalty=10000 and \clubpenalty=10000 in the preamble, which makes both cases effectively impossible.
Penalties are also the answer to "this block must not split". \nopagebreak at the end of a line, \needspace{6\baselineskip} before a heading, a minipage around a paragraph and its figure, or a [H]-style non-float for an image that must not move. Each of them is a statement about the page, not a chunk of glue, so they survive edits — which is exactly what a hand-placed \vspace does not.
Floats have their own budget, and a document "full of whitespace at the top of pages" is usually a float-placement problem rather than a margins one. The five numbers that matter are \textfraction (the minimum fraction of a page that must hold text), \topfraction and \bottomfraction (how much of a page floats may occupy at the top or bottom), \floatpagefraction (how full a page of pure floats must be) and \totalnumber (how many floats may share one page). Raising \topfraction to 0.85 and \floatpagefraction to 0.8 is the standard pair; do it in the preamble with \renewcommand on the \topfraction family, and prefer the placeins package's \FloatBarrier to a shotgun of \clearpages.
Finally, the last vertical decision: \raggedbottom lets a short page end where it ends, \flushbottom stretches the interline glue so the last baseline lands at a fixed height. article defaults to ragged, report and book to flush, which is why a chapter's last page in a thesis can have airier spacing than the rest of it. If that bothers you, the switch is a single line in the preamble — and the thing you should not do is add space to that page by hand.
A layout you can defend, in five lines
Start from the requirement rather than from the look. A thesis office asking for "A4, 2.5cm margins, 1.5 line spacing, 15mm binding allowance" translates directly into \documentclass[12pt,a4paper,twoside,openright]{report} plus \usepackage[a4paper,inner=2.5cm,outer=2.5cm,top=3cm,bottom=3.5cm,bindingoffset=15mm,includeheadfoot,headheight=14pt,heightrounded]{geometry} plus \onehalfspacing from setspace. Nothing else on the page is a layout decision; the rest is typography, and typography is a separate argument.
Then check the result on the machine that will read it: compile once with showframe, look at the first page and at a page with a displayed equation, and look at the log for Overfull \vbox (…pt too high), which heightrounded should have made impossible, and for the headheight is too small warning that tells you the header box was never sized for the content you put in it.
Count the lines on a full page and the characters on a full line. A measure of roughly 60 to 75 characters is the readable band for a single-column text face, and the count falls out of \textwidth divided by the average character width of your font at your size — which is why \typeout{\the\textwidth} plus one look at a line of body text is a better design tool than an opinion about margins. If the measure is too wide, the fix is the block width or a two-column layout (twocolumn, or the multicol package with \columnsep and \columnseprule), not smaller type.
The rule that keeps all of this maintainable: geometry belongs to the preamble, spacing belongs to a package, breaks belong to penalties, and the only per-page tool is \enlargethispage — used on one page, for one reason, with a comment saying what. Every other local patch you make to a page is a promise that the next edit will break two pages.
Lesson cheat sheet
One key line of code from each section — jump to it or copy it straight from here.
\begin{array}{rcl}\text{left edge to text} & = & \text{one inch} + \texttt{hoffset} + \texttt{oddsidemargin} \\ \text{top edge to header} & = & \text{one inch} + \texttt{voffset} + \texttt{topmargin}\end{array}
\documentclass[11pt]{article}
\documentclass[12pt,a4paper,twoside,openright]{book}
\begin{array}{c}\texttt{topmargin}\;(\text{from one inch down}) \\ \hline \texttt{headheight} \to \texttt{headsep} \to \underbrace{\texttt{textheight}}_{\text{the text block}} \\ \hline \texttt{footskip}\;(\text{last baseline to the footer baseline})\end{array}
\documentclass[10pt,a4paper]{article}
Common mistakes
- Setting
\oddsidemargin=0ptand getting a 1-inch margin anyway. TeX measures the block from a point one inch from the paper edge, so a real 2.5cm margin is-0.6cm— or, far better, letgeometrydo the arithmetic. \linespread{1.5}with no\selectfontafterwards, then concluding the command is broken. The factor is only read when a size is selected;setspacehandles the pair for you.- Passing
a4paperto the class and a different paper togeometry, or setting\paperwidthby hand: the driver writes one media box and the printer crops the page. - Adding
includehead/includefootlate, or not at all, then fighting a header that sits inside the top margin. Without those keys the margins apply to the text block alone. - Fighting a widow or a split block with
\\,\vspaceor an empty paragraph. Set\widowpenaltyand\clubpenalty, or wrap the block in aminipage/ ask for space with\needspace. - Ignoring
Overfull \vbox ... (badness)andheadheight is too smallas warnings. Both name a length that is wrong by a known amount, and both are fixed in the preamble rather than on the page. - Changing the geometry in the middle of a document with
\newgeometryand expecting it to apply from the current line: it ends the page first, because the text block cannot change mid-page. - Hand-tuning one bad page with
\enlargethispageand keeping it. It is a one-page repair; once the text reflows upstream, it is an unexplained gap two sections later.
Questions about this lesson
How do I change margins in LaTeX?
\usepackage[margin=2.5cm]{geometry}, or left/right/top/bottom individually, or inner/outer for a two-sided document. Do not set \oddsidemargin and \topmargin by hand unless you are deliberately working around the one-inch offset TeX assumes — geometry exists to do that arithmetic for you.How do I double space in LaTeX?
\doublespacing in the preamble; \onehalfspacing gives the 1.5-line convention most guidelines ask for, and \setstretch{1.18} takes any factor you like. The doublespace value is not literally a factor of two because the class already supplies leading, and the package keeps captions and footnotes tighter than the body.How do I set A4 paper size in LaTeX?
\documentclass[a4paper]{article} — or as a geometry key, \usepackage[a4paper]{geometry}. Use geometry when you also set margins, because it adjusts the paper and the block together and passes the right size to the output driver; the class option alone only changes the lengths.What is textheight and textwidth in LaTeX?
\topmargin, \headheight, \headsep above it, \footskip below, \oddsidemargin or \evensidemargin to its left. The number of lines that fit on a page is textheight divided by baselineskip, which is why geometry has a heightrounded option.Why is my LaTeX page mostly empty at the bottom?
\textfraction) and a maximum for floats at the top and bottom (\topfraction, \bottomfraction), so a stubborn figure pushes text down. Raising \topfraction and \floatpagefraction slightly, or moving the figure with \FloatBarrier from the placeins package, fixes the pages without touching the margins.How do I keep a heading with the text after it?
\needspace{4\baselineskip} from the needspace package starts a new page when four lines are not available. The class already attaches headings to what follows through \@afterheading, so a heading orphaned on the last line of a page is nearly always a case of the following block being a float or a table rather than plain text.Live playground: edit the LaTeX, see it render
The preview updates as you type — break it on purpose to see how errors look.
Keep reading
Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.