Vladar's Blog

LaTeX page geometry and layout

Since we already figured out the document structure, fonts management, and text control, it's a good time to get to page properties and layout.

Contents


Document Class

You start dealing with the page geometry as soon as you type the first command in the new document:

\documentclass[10pt,twoside,twocolumn,openany]{book}

There are many classes1 for LaTeX documents, but for our purposes, we will deal mostly with the book class, though article or report might be suitable for the shorter documents. There is also the memoir class2, based on the book class, providing even more options to stylize the output.

Important \documentclass options:


Geometry

To gain more control over the page geometry, you can employ the geometry package.3

You can either declare the desired options during the package loading (\usepacakge[options]{geometry}) or later, by calling the \geometry{options} command. To switch the geometry mid-document, \newgeometry{options}, \restoregeometry, \savegeometry{name}, and loadgeometry{name} commands will be of use.

Important geometry package options:

There are too many options in the geometry package to list them all here, so don't forget to check out its documentation.


Debug

Besides the draft option of \documentclass, you can use the showframe package4 or the showframe option of the geometry package to draw the layout diagram over all pages of your document.

Call the \layout{} command of the layout package5 to print out the current layout dimensions of your document:

latex-layout.png


Landscape pages

The lscape6 and pdflscape7 packages provide the landscape environment that allows you to create rotated landscape pages in a document with portrait orientation. The difference between these two packages, is that pdflscape will rotate the landscape page the correct way up when viewed on the screen.

\usepackage{pdflscape}

\begin{landscape}
  LANDSCAPE PAGE CONTENTS
\end{landscape}

Penalties

Penalties8 in LaTeX are values that help to automate the text formatting by discouraging certain unwanted behaviors, like line and page breaks at bad points. Here are just some of these with the default values assigned for reference:

Increasing these values (to about 300-500) could lead to cleaner automatic text layout, but setting them too high (10000 or more), effectively forbidding this behavior altogether, might lead to strange results.

Fitting the last line

While club lines (also called "orphans") can easily be dealt with by using \break or \clearpage commands, fitting the last line of a paragraph on the same page to avoid "widow" lines could be far trickier.

One of the ways to "compress" the page was already brought up previously. One alternative solution you should be aware of, is that by putting \looseness=-1 at the end of a paragraph, you can reduce its length enough to make it one line shorter. If this doesn't help, you may instead make the current page one line taller by calling \enlargethispage{\baselineskip}.


Columns

While twocolumn option we encountered earlier is good enough for the most cases, you surely also want to have some more advanced tools in your repertoire.

Balancing the columns

In twocolumn mode, if the second column of the last page is under-full, you can balance it with the first column, by calling the \balance command from the balance package.9

Changing the number of columns

While you can change one column layout to two columns and back by invoking the \twocolumn and \onecolumn commands, this will start a new page. If you want to change the number of columns mid-page or have more than two columns, you should look into the multicol package.10 The basic usage is self-explanatory:

\begin{multicols}{num}
  [header][length]
  MULTICOLUMN TEXT
\end{multicols}

Optional header argument is the text that will span over all columns at the start of the environment, and the second optional length argument could be used to override the \premulticols value (see below) when determining if there is enough space on the page for the environment.

You can configure the output with following variables:

📝 NOTE: The multicols environment balances the columns by default. If you don't want balanced columns, use multicols* (starred version) instead.

By nesting one multicols environment inside the other one, you can achieve some interesting results:

\lipsum[1]

\begin{multicols}{2}
  \lipsum[2]
\end{multicols}

\begin{multicols}{2}
  \columnseprule=1pt
  \begin{multicols}{2}
    [\section{This long title spread over both columns of the inner multicol environment}]
    \lipsum[3-4]
  \end{multicols}

  \columnseprule=0pt
  \lipsum[5]

  \centering
  \includegraphics[width=0.6\linewidth]{pugknight}
\end{multicols}

latex-multicols.png

Variable column widths

If you need your columns to be of uneven width, the vwcol package11 and its eponymous environment will come to your aid.

  \begin{vwcol}[options]
    MULTICOLUMN TEXT
  \end{vwcol}

Using the options argument, you will control the appearance of the environment. The other way to do it, is to pass the same options in the \vwcolsetup{options} command.

The main option that should always be present is widths={}, which will determine the number and size of the columns. For example, widths={0.75, 0.25} will produce a two-column environment with the first column being three times wider than the second one. The other possible options are:

\section{Variable width columns}

\hrule

\begin{vwcol}[widths={0.6,0.4}, siderule]
  \lipsum[1-3]
\end{vwcol}

\hrule

\begin{vwcol}[widths={0.4,0.6}, siderule]
  \lipsum[4-6]
\end{vwcol}

\hrule

latex-vwcol.png


Grid system

The grid-system package12 provides a simple but powerful platform for breaking the page area into an arbitrary number of variable width cells. It does so by using combinations of Row and Cell environments:

\begin{Row}[options]
  \begin{Cell}{number}
    FIRST CELL
  \end{Cell}

  \begin{Cell}{number}
    SECOND CELL
  \end{Cell}
\end{Row}

The only available option for the Row environment is cellsep (equal to 1.75em by default), specifying the horizontal distance between the columns.

The number argument of the Cell environment declares the ratio of the page width the cell will span over. For example, if there are two cells in the row, declared as {Cell}{2} and {Cell}{1}, the first one will be two times wider than the second one.

\begin{Row}
  \begin{Cell}{1}
    \subsection{1/3 width cell}
    \lipsum[1]
  \end{Cell}

  \begin{Cell}{2}
    \subsection{2/3 width cell}
    \lipsum[2-4]
  \end{Cell}
\end{Row}

\begin{Row}
  \begin{Cell}{3}
    \subsection{3/5 width cell}
    \lipsum[5]
  \end{Cell}

  \begin{Cell}{2}
    \subsection{2/5 width cell}
    \lipsum[6]
  \end{Cell}
\end{Row}

latex-grid.png


Shaping text

Wrapping text around figures

If you have a small illustration or a short paragraph, you may want your main text to beautifully wrap around it. This is possible with the wrapfig2 package,13 which provides a set of environments for different types of content:

📝 NOTE: For starred versions of the environments that have them, the star is placed at the end of the environment definition, not in the name.

The parameters:

The \includeframedtext command inside the wraptext environment allows to easily decorate the text:

\includeframedtext[width]{contents}[options]

📝 NOTE: It seems that radius=0pt doesn't work, but you can use a very small value (radius=1sp) to achieve sharp corners.

\begin{wrapfigure}[-1]{r}*% the star turns line count to line correction value
  \includegraphics[width=0.45\linewidth]{pugknight}
\end{wrapfigure}

\lipsum[1-2]
\vspace{\baselineskip}

\begin{wraptext}[-1]{l}% don't need the star with the wraptext environment
  \vspace{-\baselineskip}% bring the text one line higher
  \includeframedtext{%
    \lipsum[3]
  }[backgroundcolor=white, fontstyle=\em]
\end{wraptext}

\lipsum[4-5]

latex-wrapfig.png

Shaped paragraphs

Finally, to fit a paragraph into an arbitrary shape, you can employ the shapepar package.14 It implements three main commands to shape the paragraph:

The optional scale argument, if given, scales the shape so one unit of the shape equals the scale, and the shape is filled with white space after the paragraph text.

Here's an overview of the default shapes included in the package. Even more shapes are provided in separate *shape.def files. There are also corresponding commands to use each shape.

Shape Command Description
\squareshape \squarepar Square
`\circleshape \circlepar Circle
\CDshape \CDlabel Circle with circular hole
\diamondshape \diamondpar Diamond (♦)
\heartshape \heartpar Heart (♥)
\starshape \starpar Five-point star
\hexagonshape \hexagonpar Hexagon
\nutshape \nutpar Hexagon with circular hole
\rectangleshape{height}{width} Rectangle shapes

Creating your own shapes is possible as well.

\twocolumn

\lipsum[1]

\shapepar[0.88\linewidth]{\circleshape}
\lipsum[2]

\lipsum[3]

\cutout{r}(-0.25\linewidth, 6\baselineskip)\shapepar{\hexagonshape}
\lipsum[4]

\lipsum[5]

latex-shapepar.png

Discuss this post on Reddit


  1. https://www.ctan.org/pkg/classes

  2. https://www.ctan.org/pkg/memoir

  3. https://www.ctan.org/pkg/geometry

  4. https://ctan.org/pkg/showframe

  5. https://www.ctan.org/pkg/layout

  6. https://www.ctan.org/pkg/lscape

  7. https://www.ctan.org/pkg/pdflscape

  8. https://en.wikibooks.org/wiki/TeX/penalty

  9. https://www.ctan.org/pkg/balance

  10. https://www.ctan.org/pkg/multicol

  11. https://www.ctan.org/pkg/vwcol

  12. https://www.ctan.org/pkg/grid-system

  13. https://www.ctan.org/pkg/wrapfig2

  14. https://www.ctan.org/pkg/shapepar

#gamedev #guide #latex #software #ttrpg