Typevia
Fonts and Typography

Understanding Lengths in LaTeX

In LaTeX, "length" refers to any measurement used to define the size of elements, such as the width of a margin, the distance between paragraphs, or the size of a box. Unlike word processors where you might drag a slider, LaTeX requires you to specify these dimensions using numerical values followed by specific unit abbreviations.

Mastering length units is essential because it allows you to create precise layouts that remain consistent even if you change your document's font size or page orientation.

Absolute Units

Absolute units are "fixed" measurements. They represent a specific physical distance regardless of the context, font size, or document class. These are most useful when you need to match a physical requirement, such as a 1-inch margin or a 10cm wide image.

Here are the most common absolute units used in LaTeX:

UnitNameDescription
ptPointThe standard TeX unit. Approximately 1/72.27 of an inch.
mmMillimeter1 millimeter.
cmCentimeter10 millimeters.
inInch25.4 millimeters.
pcPica12 points.
bpBig Point1/72 of an inch (standard PostScript/PDF point).

Example: Using Absolute Units

You can use these units with commands like \hspace (horizontal space) or \vspace (vertical space).

\documentclass{article}
\begin{document}

This sentence has a \hspace{2cm} large gap in the middle.

\vspace{1in}

This sentence appears exactly one inch below the previous one.

\end{document}

Note: If you forget to include a unit (e.g., writing \hspace{5} instead of \hspace{5pt}), LaTeX will throw an error: "Illegal unit of measure (pt inserted)." LaTeX always requires a unit, even if the value is zero.

Relative Units

Relative units are dynamic. They scale based on the current font or the surrounding environment. Using relative units is considered a best practice in typography because it ensures that your spacing looks balanced even if you change the font size from 10pt to 12pt.

The two most important relative units are:

  1. em: Historically the width of a capital 'M'. In LaTeX, it is equal to the current font size. If you are using a 12pt font, 1em is 12pt.
  2. ex: Roughly the height of a lowercase 'x'. It is useful for vertical adjustments that need to stay proportional to the height of the text.

Example: Creating Proportional Spacing

Using em for paragraph indentation or spacing ensures the document looks consistent across different font settings.

\documentclass[12pt]{article}
\begin{document}

\noindent This paragraph has no indent.\\
\hspace{2em} This line is indented by two characters relative to the font size.

\bigskip

{\small This is small text, so \hspace{2em} this gap is smaller than the one above.}

\end{document}

Built-in Document Lengths

LaTeX provides several "internal" lengths that represent parts of your page layout. Instead of guessing how wide your page is, you can refer to these variables directly.

  • \textwidth: The total width of the text block on a page.
  • \linewidth: The width of the current line. Inside a list or a narrow column, \linewidth is smaller than \textwidth.
  • \textheight: The total height of the text block on the page.
  • \parindent: The current value of the paragraph indentation.

Example: Scaling Images and Boxes

A common use case for these lengths is making an image or a box fit exactly to the width of the page.

\documentclass{article}
\usepackage{graphicx}

\begin{document}

% Scale an image to 80% of the text width
\begin{center}
    \includegraphics[width=0.8\textwidth]{example-image}
\end{center}

% Create a frame that is exactly the width of the text
\framebox[\textwidth]{This box spans the entire width of the page.}

\end{document}

Modifying and Calculating Lengths

Sometimes you need to change a default length (like the space between paragraphs) or perform basic math (like making a box half the width of the page minus 1cm).

Setting Lengths

Use \setlength{\lengthname}{value} to set a new value for an existing length.

% Set paragraph indentation to 0
\setlength{\parindent}{0pt}

% Increase the space between paragraphs
\setlength{\parskip}{1em}

Calculations

While you can use decimals (e.g., 0.5\textwidth), more complex math requires the calc package. This allows you to use addition and subtraction within length specifications.

\documentclass{article}
\usepackage{calc}

\begin{document}

% A box that is half the text width minus 1 centimeter
\begin{minipage}{0.5\textwidth - 1cm}
    This content is in a precisely calculated column.
\end{minipage}

\end{document}

Rubber Lengths (Glue)

In LaTeX, some lengths are "flexible." These are called rubber lengths or glue. They can stretch or shrink to fill the available space. The most common rubber length is \fill.

Example: Pushing Content

You can use \hfill (horizontal fill) or \vfill (vertical fill) to push text to the edges of the page.

\documentclass{article}
\begin{document}

Left Side \hfill Right Side

\vfill

This text will appear at the very bottom of the page.

\end{document}

Best Practices and Common Pitfalls

  1. Use em for text-related spacing: When adding space between words or sentences, use em. It ensures the spacing remains aesthetically pleasing if you change the document font size later.
  2. Use \linewidth instead of \textwidth for boxes: If you are inside a list (like itemize), \textwidth still refers to the full page width, which might cause your content to bleed into the margins. \linewidth correctly adjusts to the indented width of the list.
  3. Avoid hardcoding centimeters for margins: Use the geometry package to handle page dimensions. It's much cleaner than manually setting lengths.
  4. The "Zero" Rule: Remember that even if a length is zero, you should usually include a unit if you are using a command that requires a length, though many modern compilers are becoming more forgiving of 0.
  5. Watch for "Overfull \hbox": If you set a length (like an image width) to be exactly \textwidth but place it inside a paragraph with an indentation, the image will stick out into the right margin. Use \noindent before the image or wrap it in a center environment to avoid this.

By understanding how LaTeX measures space, you move from just writing text to truly designing a document. Start by using \textwidth for your images and em for your custom spacing to see how much more flexible your documents become.