Vladar's Blog

LaTeX: text spacing and decoration

Continuing with the LaTeX text formatting, here's a quick guide for spacing and decoration.

Contents


Units

Going further, you will need to get acquainted with the units of length used in LaTeX. The most popular ones are presented below. You could see the full list here.

pt points: 1/72.27 (~0.0138) inch or ~0.3515 cm
in inches (72.27pt)
cm centimeters (28.4pt)
mm millimeters (2.84pt)
ex height of the "x" glyph in the current font
em width of the "M" glyph in the current font

📝 NOTE: There is also a px (pixel) unit, (equal to 1bp or 1/72in), but its usage is not recommended for printed materials.


Lengths

Common length macros you will probably need at some point:

\baselineskip baseline spacing in a paragraph
\baselinestretch \baselineskip multiplier, set with \renewcommand{\baselinestretch}{multiplier}\selectfont or \linespread{multiplier}\selectfont
\linewidth line width in the current environment
\columnwidth column width
\columnsep distance between columns
\tabcolsep separation between columns in a tabular environment
\parindent paragraph indentation
\parskip spacing between paragraphs
\topmargin top margin
\evensidemargin even pages margin
\oddsidemargin odd pages margin
\paperwidth page width
\paperheight page height
\textheight height of the text area
\textwidth width of the text area
\unitlength units of length in picture environment

You can change these values by using the following commands:

\setlength{\length}{value} or \length=value set \length to the new value
\addtolength{\length}{extra} add extra to the \length's current value
\newlength{\mylength} or \newdimen\mylength create a new length macro
\dimexpr value\relax calculate a length expression
\settowidth{\length}{sample text} set \length to the width of the sample text
\settoheight{\length}{sample text} set \length to the height of the sample text
\settodepth{\length}{sample text} set \length to the depth of the sample text (length between the baseline and the bottom of the text)
\the\length print out the \length value

For example, let's say, you want the base line spacing to be 0.2in:

\setlength{\baselineskip}{0.2in}

Spacing

Presented below are some frequently used spacing commands.

\ (backslash-space) to place more than one space in a sequence
~ fixed-width non-breakable space
\quad 1em-width space
\qquad 2em-width space
\hspace{w} horizontal space
\hspace*{w} horizontal space, preserved at the start or the end of the line
\vspace{h} vertical space
\vspace*{h} vertical space, preserved at the top or the bottom of the page
\smallskip insert a small vertical space between paragraphs
\medskip insert a medium vertical space between paragraphs
\bigskip insert a big vertical space between paragraphs

Fills

\hfill horizontal space that fills the rest of the line
\vfill vertical space that fills the rest of the page
\stretch{n} rubber length with n times \fill units of stretchability

\hfil and \vfil commands do the same, but have a lower stretching priority.1

Rules

Indentation

By default, LaTeX does not indent first paragraph after section headers. You can change this behaviour by using the indentfirst package.2


Hyphenation

While you can set the points where the word will split to new line manually by using manual hyphenation \- command, this should rarely be used. Instead, load the babel package3 for the particular language.

\usepackage[english]{babel}

Fine-tuning

Configuring such parameters as character protrusion, font expansion, adjustment of interword spacing and additional kerning, and hyphenatable letterspacing (tracking) is available through the microtype package.4 Just loading it with the default values should suffice for most use cases.

Another useful trick, especially if you have a block of text that just must fit on a single page, is to use savetrees package,5 that will subtly "compress" the text, sacrificing its attractiveness to fit the desired volume.


Color

Going further, you will need to know how colors are defined in LaTeX. I recommend using the xcolor package6 for this. You can either define your own colors (\definecolor{mycolor1}{rgb}{1,0.5,0.25} or \colorlet{mycolor2}{definition}), or use the predefined ones, which include the following collections (Chapter 4: Colors by Name):

You can also mix a couple of colors together. Here's a couple of examples:


Coloring the text

You can change the text color by using \textcolor{colorname}{text} or {\color{colorname}text} commands. Likewise, you can set the color of the page \pagecolor{colorname}. To change the background of one line you can use \colorbox and \fcolorbox commands, described in the following section.

By using the contour package,7 you can also draw contour around the text. For example, this could be useful if you want to display a black text on black background:

\noindent\colorbox{black}{\contour{white}{Note:~}} $\leftarrow$
This will be displayed as black-on-black with white contour around the text.

latex-colors


Boxes

Since we have already covered some of the box commands present in LaTeX, let's overview the rest.

Single-line boxes

Paragraph boxes

You might notice that the commands above work only for a single line of texts and do not accept line breaks. To print multiple lines of text, you can use the \parbox[position][height][inner-pos]{width}{text} command:

You will still need to insert manual line breaks:

\fbox{\parbox{\linewidth}{line 1\\line 2}}

The pbox package8 allows you to create a paragraph box which width will fit its contents — \pbox with arguments identical to \parbox, but width is the maximum width of the box. Another benefit is that it will automatically break long lines for you.

The \minipage environment provides the most flexibility in terms of its possible content:

\begin{minipage}[position][height][inner-pos]{width}
  Whatever you want.
\end{minipage}

Save boxes

You can store text in a box register to print it out later.

Modifiers

Further reading

The following packages provide additional kinds of boxes you might be interested in:


That's all for now. Next time we'll proceed to page geometry and layout.

Discuss this post on Reddit

end illustration

  1. https://en.wikibooks.org/wiki/TeX/hfill

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

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

  4. https://www.ctan.org/pkg/microtype

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

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

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

  8. https://ctan.org/pkg/pbox

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

  10. https://ctan.org/pkg/graphicx

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

  12. https://www.ctan.org/pkg/framed

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

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

#gamedev #guide #latex #software #ttrpg