Chapter 02 – Formatting Text and Creating Macros

Here are the Code examples of of this chapter. You can compile them online right on this web page by pressing the Typeset / Compile button. You can also edit them for testing, and compile again.

For a better view with the online compiler, I sometimes use \documentclass[border=10pt]{standalone} instead of \documentclass{article}. Instead of having a big letter/A4 page, the standalone class crops the paper to see just the visible text without an empty rest of a page.

Any question about a code example? Post it on LaTeX.org, I will answer. As forum admin I read every single question there. (profile link).

Creating a document with title and heading

\documentclass[a4paper,11pt]{article}
\title{Example 2}
\author{My name}
\date{May 5, 2021}
\begin{document}
\maketitle
\section{What's this?}
This is our second document. It contains a title and a section with text.
\end{document}

figure

Figure 2.1


Printing out special symbols

\documentclass[preview,border=10pt]{standalone}
\begin{document}
Statement \#1:
50\% of \$100 equals \$50.

More special symbols are \&, \_, \{ and \}.
\end{document}

figure

Figure 2.2


Adjusting the font shape

\documentclass[preview,border=10pt]{standalone}
\begin{document}
Text can be \emph{emphasized}.

Besides from \textit{italics}, words can be \textbf{bold},
\textsl{slanted}, or typeset in \textsc{Small Caps}.

Such commands can be \textit{\textbf{nested}}.

\emph{See how \emph{emphasizing} looks when nested.}
\end{document}

figure

Figure 2.3


Choosing the font family

\documentclass[preview,border=10pt]{standalone}
\begin{document}
\textsf{\textbf{Get help on the Internet}}

\texttt{https://latex.org} is a support forum for \LaTeX.
\end{document}

figure

Figure 2.4


Using declarations

\documentclass[preview,border=10pt]{standalone}
\begin{document}
\sffamily\bfseries Get help on the Internet

\normalfont\ttfamily https://latex.org\normalfont\ is
a support forum for \LaTeX.
\end{document}

figure

Figure 2.5


Font commands

% Used for Figure 2.6: Font commands
\documentclass[border=10pt]{standalone}
\usepackage{booktabs}
\renewcommand*\c{\ttfamily\textbackslash}
\begin{document}
\sffamily
\begin{tabular}{lll}
\toprule
\textbf{Command} & \textbf{Declaration} & \textbf{Meaning} \\
\midrule
\c textrm\{\ldots\}     & \c rmfamily   & \normalfont\rmfamily roman family      \\
\c textsf\{\ldots\}     & \c sffamily   & \normalfont\sffamily sans-serif family \\
\c texttt\{\ldots\}     & \c ttfamily   & \normalfont\ttfamily typewriter family \\
\c textbf\{\ldots\}     & \c bfseries   & \normalfont\bfseries bold-face         \\
\c textmd\{\ldots\}     & \c mdseries   & \normalfont\mdseries medium            \\
\c textit\{\ldots\}     & \c itshape    & \normalfont\itshape  italic shape      \\
\c textsl\{\ldots\}     & \c slshape    & \normalfont\slshape  slanted shape     \\
\c textsc\{\ldots\}     & \c scshape    & \normalfont\scshape  Small Caps shape  \\
\c textup\{\ldots\}     & \c upshape    & \normalfont\upshape  upright shape     \\
\c textnormal\{\ldots\} & \c normalfont & \normalfont          default font      \\
\bottomrule
\end{tabular}
\end{document}

figure

Figure 2.6


Confining the effect of commands by braces

\documentclass[border=10pt]{standalone}
\begin{document}
Besides from {\itshape italics}, words can be {\bfseries bold},
{\slshape slanted}, or typeset in {\scshape Small Caps}.
\end{document}

figure

Figure 2.7


Exploring font sizes

\documentclass[border=10pt]{standalone}
\begin{document}
\tiny We \scriptsize start \footnotesize very \small small,
\normalsize get \large big \Large and \LARGE bigger,
\huge huge, \Huge gigantic!
\end{document}

figure

Figure 2.8


Using macros for simple text

\documentclass{article}
\newcommand{\TUG}{\TeX\ Users Group}
\begin{document}
\section{The \TUG}
The \TUG\ is an organization for people who use \TeX\ or \LaTeX.
\end{document}

figure

Figure 2.9


Proper spacing after commands

\documentclass{article}
\usepackage{xspace}
\newcommand{\TUG}{\TeX\ Users Group\xspace}
\begin{document}
\section{The \TUG}
The \TUG is an organization for people who use \TeX\ or \LaTeX.
\end{document}

figure

Figure 2.9 – same output


Creating a macro with arguments

\documentclass[border=10pt]{standalone}
\newcommand{\keyword}[1]{\textbf{#1}}
\begin{document}
\keyword{Grouping} by curly braces limits the
\keyword{scope} of \keyword{declarations}.
\end{document}

figure

Figure 2.10



Creating a macro with optional arguments

\documentclass[border=10pt]{standalone}
\newcommand{\keyword}[2][\bfseries]{{#1#2}}
\begin{document}
\keyword{Grouping} by curly braces limits the
\keyword{scope} of \keyword[\itshape]{declarations}.
\end{document}

figure

Figure 2.11



Creating a narrow text box

\documentclass[border=10pt]{standalone}
\begin{document}
\parbox{3cm}{TUG is an acronym. It means \TeX\ Users Group.}
\end{document}

figure

Figure 2.12



Producing common paragraph boxes

\documentclass[border=10pt]{standalone}
\begin{document}
Text line
\quad\parbox[b]{1.8cm}{this parbox is aligned at its bottom line}
\quad\parbox{1.5cm}{center-aligned parbox}
\quad\parbox[t]{2cm}{another parbox aligned at its top line}
\end{document}

figure

Figure 2.13



Using mini pages

\documentclass[border=10pt]{standalone}
\begin{document}
\begin{minipage}{3cm}
TUG is an acronym. It means \TeX\ Users Group.
\end{minipage}
\end{document}

figure

Figure 2.14



Improving hyphenation

\documentclass[border=10pt]{standalone}
\hyphenation{acro-nym}
\begin{document}
\begin{minipage}{3cm}
TUG is an acronym. It means \TeX\ Users Group.
\end{minipage}
\end{document}

figure

Figure 2.15



Improving the justification

\documentclass[border=10pt]{standalone}
\usepackage{microtype}
\begin{document}
\begin{minipage}{3cm}
TUG is an acronym. It means \TeX\ Users Group.
\end{minipage}
\end{document}


Breaking lines manually

\documentclass[preview,border=10pt]{standalone}
\begin{document}
\noindent\emph{Annabel Lee}\\
It was many and many a year ago,\\
In a kingdom by the sea,\\
That a maiden there lived whom you may know\\
By the name of Annabel Lee
\end{document}

figure

Figure 2.16



Creating ragged-right text

\documentclass[border=10pt]{standalone}
\begin{document}
\parbox{3cm}{\raggedright
TUG is an acronym. It means \TeX\ Users Group.}
\end{document}

figure

Figure 2.17



Creating ragged-left text

\documentclass[border=10pt]{standalone}
\begin{document}
\parbox{3cm}{\raggedleft
TUG is an acronym. It means \TeX\ Users Group.}
\end{document}

figure

Figure 2.18



Centering text

\documentclass[preview,border=10pt]{standalone}
\pagestyle{empty}
\begin{document}
{\centering
\huge\bfseries Centered text \\
\Large\normalfont written by me \\
\normalsize\today

}
\end{document}

figure

Figure 2.19



Using environments for justification

\documentclass[preview,border=10pt]{standalone}
\usepackage{url}
\begin{document}
\noindent This is the beginning of a poem
by Edgar Allan Poe:
\begin{center}
\emph{Annabel Lee}
\end{center}
\begin{center}
It was many and many a year ago,\\
In a kingdom by the sea,\\
That a maiden there lived whom you may know\\
By the name of Annabel Lee
\end{center}
The complete poem can be read on
\url{http://www.online-literature.com/poe/576/}.
\end{document}

figure

Figure 2.20



Displaying quotes

\documentclass[preview,border=10pt]{standalone}
\begin{document}
\noindent Niels Bohr said: ``An expert is a person who has made
all the mistakes that can be made in a very narrow field.''
Albert Einstein said:
\begin{quote}
Anyone who has never made a mistake has never tried anything new.
\end{quote}
Errors are inevitable. So, let’s be brave trying something new.
\end{document}

figure

Figure 2.21



Quoting longer text

\documentclass[preview,border=10pt]{standalone}
\usepackage{url}
\begin{document}

The authors of the CTAN team listed ten good reasons
for using \TeX. Among them are:
\begin{quotation}
\TeX\ has the best output. What you end with,
the symbols on the page, is as useable, and beautiful,
as a non-professional can produce.

\TeX\ knows typesetting. As those plain text samples
show, TeX's has more sophisticated typographical algorithms
such as those for making paragraphs and for hyphenating.

\TeX\ is fast. On today's machines \TeX\ is very fast.
It is easy on memory and disk space, too.

\TeX\ is stable. It is in wide use, with a long history.
It has been tested by millions of users, on demanding input.
It will never eat your document. Never.
\end{quotation}
The original text can be found on
\url{https://www.ctan.org/what_is_tex.html}.
\end{document}

figure

Figure 2.22



Vertical spacing between paragraphs

\documentclass[preview,border=10pt]{standalone}
\usepackage{parskip}
\usepackage{url}
\begin{document}
The authors of the CTAN team listed ten good reasons
for using \TeX. Among them are:

\TeX\ has the best output. What you end with,
the symbols on the page, is as useable, and beautiful,
as a non-professional can produce\ldots

The original text can be found on
\url{https://www.ctan.org/what_is_tex.html}.
\end{document}

figure

Figure 2.23


This code is available on Github. It is licensed under the MIT License, a short and simple permissive license with conditions only requiring preservation of copyright and license notices.

Go to next chapter.