Chapter 02 – Formatting Text and Creating Macros

Here are the Code examples of Chapter 2. 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.

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}

Printing out special symbols

\documentclass{article}
\begin{document}
Statement \#1:
50\% of \$100 equals \$50.

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

Adjusting the font shape

\documentclass{article}
\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}

Choosing the font family

\documentclass{article}
\begin{document}
\textsf{\textbf{Get help on the Internet}}

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

Using declarations:

\documentclass{article}
\begin{document}
\sffamily\bfseries Get help on the Internet

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

Font commands:

% Used for Figure 2.6: Font commands
\documentclass{article}
\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}

Confining the effect of commands by braces

\documentclass{article}
\begin{document}
Besides from {\itshape italics}, words can be {\bfseries bold},
{\slshape slanted}, or typeset in {\scshape Small Caps}.
\end{document}

Exploring font sizes

\documentclass{article}
\begin{document}
\tiny We \scriptsize start \footnotesize very \small small,
\normalsize get \large big \Large and \LARGE bigger,
\huge huge, \Huge gigantic!
\end{document}

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}

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}

Creating a macro with arguments

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

Creating a macro with optional arguments

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

Creating a narrow text box

\documentclass{article}
\begin{document}
\parbox{3cm}{TUG is an acronym. It means \TeX\ Users Group.}
\end{document}

Producing common paragraph boxes

\documentclass{article}
\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}

Using mini pages

\documentclass{article}
\begin{document}
\begin{minipage}{3cm}
TUG is an acronym. It means \TeX\ Users Group.
\end{minipage}
\end{document}

Improving hyphenation

\documentclass{article}
\hyphenation{acro-nym}
\begin{document}
\begin{minipage}{3cm}
TUG is an acronym. It means \TeX\ Users Group.
\end{minipage}
\end{document}

Improving the justification

\documentclass{article}
\usepackage{microtype}
\begin{document}
\begin{minipage}{3cm}
TUG is an acronym. It means \TeX\ Users Group.
\end{minipage}
\end{document}

Breaking lines manually

\documentclass{article}
\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}

Creating ragged-right text

\documentclass{article}
\begin{document}
\parbox{3cm}{\raggedright
TUG is an acronym. It means \TeX\ Users Group.}
\end{document}

Creating ragged-left text

\documentclass{article}
\begin{document}
\parbox{3cm}{\raggedleft
TUG is an acronym. It means \TeX\ Users Group.}
\end{document}

Centering text

\documentclass{article}
\pagestyle{empty}
\begin{document}
{\centering
\huge\bfseries Centered text \\
\Large\normalfont written by me \\
\normalsize\today

}
\end{document}

Using environments for justification

\documentclass{article}
\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}

Displaying quotes

\documentclass{article}
\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}

Quoting longer text

\documentclass{article}
\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}

Vertical spacing between paragraphs

\documentclass{article}
\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}

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.