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
\rule[h]{w}{t}
- horizontal ruleh
- (optional, defaults to0
) rule position above the baselinew
- rule width (length)t
- rule thickness
\hrulefill
- fill the rest of the line by a horizontal rule\dotfill
- fill the rest of the line by dots\strut
- a rule with no width but a certain height to ensure that the line has at least0.7\baselineskip
height and at least0.3\baselineskip
depth.
Indentation
\indent
- indent a non-indented paragraph (put at the start of the paragraph)\hspace{\parindent}
- this will do the same\noindent
- remove an indentation from the paragraph (put at the start of the paragraph)
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):
- base colors (always available): black, blue, brown, cyan, darkgray, gray, green, lightgray, lime, magenta, olive, orange, pink, purple, red, teal, violet, white, yellow
- dvipsnames -
\usepackage[dvipsnames]{xcolor}
- svgnames -
\usepackage[svgnames]{xcolor}
- x11names -
\usepackage[x11names]{xcolor})
You can also mix a couple of colors together. Here's a couple of examples:
\color{colorname!50}
- 50% tint ofcolorname
\color{colorname1!70!colorname2}
- 70% ofcolorname1
+ 30% ofcolorname2
\color{-colorname}
- complementary color ofcolorname
\color{rgb:colorname1,3;colorname2,5}
- mix ofcolorname1
andcolorname2
in 3/5 proportion
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.
Boxes ↩
Since we have already covered some of the box commands present in LaTeX, let's overview the rest.
Single-line boxes
\mbox{text}
- single-line box big enough to hold itstext
\makebox[width][pos]{text}
- extended version with optional parameters:width
- (optional) fixed width of the boxpos
- (optional) text position in the box:c
- centerl
- flushleftr
- flushrights
- spread
\fbox{text}
- same as the\mbox
, but draw a frame around the box\framebox[width][pos]{text}
- same as the\framebox
, but draw a frame around the box\colorbox{bgcolor}{text}
- set the background color fortext
\fcolorbox{framecolor}{bgcolor}
- set the frame and background colors fortext
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:
position
- (optional) vertical alignment of the parbox:t
- align the top text line of the parbox to the baselinec
orm
- (default) align the center of the parbox to the baselineb
- align the bottom text line of the parbox to the baseline
height
- (optional) fixed height of the parboxinner-pos
- (optional) vertical text alignment inside the parbox:t
- align the text to the topc
- align the text to the centerb
- align the text to the bottoms
- stretch the text vertically (the text must contain vertical stretchable space, e.g.\vspace{\stretch{1}}
)
width
- fixed width of the parbox
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.
\newsavebox{\boxname}
- create a new box register\sbox{\boxname}{text}
or\savebox{\boxname}[width][inner-pos]{text}
- save text to it\usebox{\boxname}
- print it out into the document
Modifiers
\setlength{\fboxsep}{length}
- set the separation between the frame and the text\setlength{\fboxrule}{thickness}
- set the thickness of the frame\raisebox{lift}[height][depth]{text}
- shift the vertical position of the textlift
- vertical offset sizeheight
- (optional) set the height of the box (vertical size above the baseline)depth
- (optional) set the depth of the box (vertical size below the baseline)
Further reading
The following packages provide additional kinds of boxes you might be interested in:
fancybox
9 provides additional variety of framed boxes:\shadowbox
,\doublebox
,\ovalbox
,\Ovalbox
graphicx
10 will help you with geometric transformations:\resizebox{h-length}{v-length}{text}
- resize the text to a requested size\rotatebox[options]{angle}{text}
- rotate the text\scalebox{h-scale}[v-scale]{text}
- scale the text in proportion (1
is 100% scale)\reflectbox{text}
- same as\scalebox{-1}[1]{text}
graphicxbox
11 allows to add a background image:\graphicxbox[options]{graphic}{text}
and add a colored frame around it:\fgraphicxbox[model]{color}[options]{graphic}{text}
framed
12 provides a set of environments that can break across pages:framed
- ordinary frame boxoframed
- frame box with open top/bottom at page breaksshaded
andshaded*
- box with colored background (defined by\definecolor{shadecolor}{model}{specs}
beforehand) with or without bleeding into margin areas accordinglysnugshade
andsnugshade*
- same as previous, but with a tight fit around the textleftbar
- thick vertical line in left margintitled-frame
- frame with a title-bar (\begin{titled-frame}{Title}
); requires definingTFTitleColor
andTFFrameColor
color variables beforehand
mdframed
13 andtcolorbox
14 present even more complex and versatile solutions for fancy box designs
That's all for now. Next time we'll proceed to page geometry and layout.
Discuss this post on Reddit