2
Looking at the Basics

2.1 The Process of LaTeX

For those who chose to skip the introduction chapter and just want to get into the heart of the topic, LaTeX is a typesetting program and is an extension of the original program TeX written by Donald Knuth.

But then what is a typesetting program?

To answer this, let us look at the various stages in the preparation of a document using computers.

1.
The text is entered into the computer by the user or some script.
2.
The input text is formatted into lines, paragraphs and pages.
3.
The output text is displayed on the computer screen.
4.
The final output is printed .

For word processors 1 1 think of Microsoft Word, Apple Pages basically, we call them “What You See Is What You Get” or WYSIWYG. all these operations are integrated into a single application package. But a typesetting language like LaTeX is concerned only with stage 2 mentioned above. So to typeset a document using LaTeX we type the text of the document and the necessary formatting commands in a text editor and then compile it. After, compilation, the document can be viewed using a previewer or printed using a printer.

TeX is also a full programming language , so by learning the quirks and the nuances of the language, people can write code for additional features such as:

In fact LaTeX itself has a significant collection of extra features, and the collective effort is continuing, with more and more people writing extra packages.

2.1.1 A Small Example

Let us see LaTeX in action by typesetting a short document. We will start with our text editor 2 2 Make sure your TeXStudio is open and all its settings configured. and type in the lines below exactly as shown in the example below:

latex
\documentclass{article} \begin{document} This is my \emph{first} document prepared in \LaTeX. \end{document}

Be very careful with the \ character (called the backslash). We use this character to denote something is a command and is different from / (called slash)

Once you have written the text, save the file onto the hard disk as myfile.tex. 3 3 Instead of myfile you can use any name you wish, but be sure to have .tex at the end as the extension. The process of compiling this and viewing the output depends on your operating system. We describe below the process of doing this in GNU/Linux.

At the shell prompt, 4 4 This is for people who have Linux, and now thinking about it, it is infinitesimally unlikely someone will have Linux installed on their computer who just started university. type

latex
latex myfile

You will see a number of lines of text scroll by in the screen and then you get the prompt back. To view the output in screen, you must have the X Window running. So, start X if you have not done so, and in a terminal window, type

A window comes up showing the output below

This is my first document prepared in LaTeX.

Now let us take a closer look at the source file 5 5 that is, the file you have typed, which is a .tex file .

The first line \documentclass{article} tells LaTeX that what we want to produce is an article.

If we want to write a book, this must be changed to \documentclass{book}. We will look into these a bit later.

The whole document we want to typeset should be included between \begin{document} and \end{document}. Think of this as a wrapper which contains all the text we want to print out to the user.

Any part which is before \begin{environment} is called the preamble .

Looking back to our example, this is just one (1) line. Now compare this line in the source and the output. The first three words (This is my) are produced as typed. Then \emph{first}, becomes first in the output. 6 6 It is a common practice to emphasise words in print using italic letters Therefore, \emph is a command to LaTeX to typeset the text within the braces in italic .

Following this, the next three words come out without any change in the output. Finally, the input \LaTeX comes out in the output as LaTeX. Therefore, as we see, our source is a mixture of text to be typeset and a couple of LaTeX commands \emph and \LaTeX. The first command changes the input text in a certain way and the second one generates new text.

Now call up the file again and add one more sentence given below.

latex
This is my \emph{first} document prepared in \LaTeX. I typed it on \today.

What do you get in the output? What new text does the command \today generate?

2.1.2 What is the Point of LaTeX

So, why all this trouble and why not simply use a word processor?

The answer lies in the motivation behind LaTeXḊonald Knuth says that his aim in creating LaTeX is to beautifully typeset technical documents especially those containing a lot of Mathematics. It is very difficult to produce complex mathematical formulas using a word processor. 7 7 as the author of this document who had to write many technical documents in Word can attest.

If you want your document to look really beautiful then LaTeX is the natural choice.

2.2 Basics of Typesetting

We have seen that to typeset something in LaTeX, we type in the text to be typeset together with some commands. Words must be separated by spaces 8 8 does not matter how many. and lines maybe broken arbitrarily.

The end of a paragraph is specified by a blank line in the input. In other words, whenever you want to start a new paragraph, just leave a blank line and proceed. For example, the first two (2) paragraphs above were produced by the input:

latex
\noindent We have seen that to typeset something in \LaTeX, we type in the text to be typeset together with some \LaTeX\ commands. Words must be separated by spaces (does not matter how many) and lines maybe broken arbitrarily. \noindent The end of a paragraph is specified by a \emph{blank line} in the input. In other words, whenever you want to start a new paragraph, just leave a blank line and proceed.

If you noticed the first line of each paragraph starts without an indentation from the left margin of the text. This is caused by the command \noindent. To allow indentation, simply remove the command.

There is an easier way to suppress paragraph indentation for all paragraphs of the document in one go, but such tricks can wait.

2.2.1 The Use of Spaces

You might have noticed that even though the length of the lines of text we type in a paragraph are different, in the output, all lines are of equal length, aligned perfectly on the right and left. LaTeX does this by adjusting the space between the words.

In traditional typesetting, a little extra space is added to periods which end sentences and LaTeX also follows this custom. But how does LaTeX know whether a period ends a sentence or not?

It assumes that every period not following an upper case letter ends a sentence.

But this does not always work, for there are instances where a sentence does end in an upper case letter. For example, consider the following:

Carrots are good for your eyes, since they contain Vitamin A. Have you ever seen a rabbit wearing glasses?

The right input to produce this is

latex
Carrots are good for your eyes, since they contain Vitamin A\@. Have you ever seen a rabbit wearing glasses?

We use the command \@ before the period to produce the extra space after the period.

On the other hand, there are instances where a period following a lowercase letter does not end a sentence. For example [20]:

The numbers 1, 2, 3, etc. are called natural numbers. According to Kronecker, they were made by God; all else being the work of Man.

To produce this (without extra space after etc.) the input should be:

latex
The numbers 1, 2, 3, etc.\ are called natural numbers. According to Kronecker, they were made by God; all else being the works of Man.

Here, we use the command \. 9 9 that is, a backslash and a space here and elsewhere, we sometimes use to denote a space in the input, especially when we draw attention to the space

There are other situations where the command \(which always produce a space in the output) is useful.

For example, type the following line and compile it:

latex
\documentclass{article} \begin{document} I think \LaTeX is fun. \end{document}

We get:

I think LaTeXis fun.

What happened to the space we typed between LaTeX and is?

You see, LaTeX gobbles up all spaces after a command. To get the required sequence in the output, change the input as I think LaTeX is fun.

Again, the command (\) comes to the rescue.

2.2.2 Quotations

Have you noticed that in typesetting, opening quotes are different from closing quotes?

Look at the LaTeX output below:

latex
Note the difference in right and left quotes in 'single quotes' and ''double quotes''.

This is produced by the input

Note the difference in right and left quotes in ‘single quotes’ and “double quotes”.

If your keyboard does not have a left quote key, you can use \lq command to produce it. The corresponding command \rq produces a right quote.

2.2.3 Dashes

In text, dashes are used for various purposes and they are distinguished in typesetting by their lengths;

Let’s have a look at the following TeX output:

X-rays are discussed in pages 221–225 of Volume 3—the volume on electromagnetic waves.

This is produced from the input:

latex
X-rays are discussed in pages 221--225 of Volume 3---the volume on electromagnetic waves.

Please observe that:

2.2.4 Accents

Sometimes, especially when typing foreign words in English, or just writing documents in non-english, we need to put different types of accents over the letters. The table below shows the accents available in LaTeX.

Each column shows some of the accents and the inputs to generate them.

ó \'o ó \'o ô \^o õ \~o
ō \=o ȯ \.o ö \"o c c \c c
u o \u o v o \v o H o \H o d o \d o
\b o t oo \t oo
Table 2.1: Some accent modifier commands.

The letters i and j need special treatment with regard to accents, since they should not have their customary dots when accented.

The commands \i and \j produce dot-less i and j as i and j .

Él está aquí

we need to type:

latex
\'{E}l est\'{a} aqu\'{\i}

Some symbols from non-English languages are also available in LaTeX, as shown in the table below:

œ \oe Œ \oe æ \ae Æ \AE
r a \aa r A \AA
o \o o \o l \l L \L
ß \ss
¡ !` ¿ ?`
Table 2.2: Commands to print out control symbols

2.2.5 Special Symbols

We have seen that input \LaTeX produces LaTeX in the output and \ produces a space. Therefore, TeX uses the symbol \ for a special purpose, as briefly mentioned previously:

To indicate the program that what follows is NOT text to be typeset but an instruction to be carried out.

So what if you want to get \ in your output? 10 10 improbable as it may be Well, the command \ produces \ in the output.

As you have notices \ is a symbol which has a special meaning for TeX and cannot be produced by direct input. As another example of such a special symbol, see what is obtained from the input below:

latex
Maybe I have now learnt about 1% of \LaTeX.

You only get:

Maybe I have now learnt about 1

What happened to the rest of the line? You see, TeX uses the per cent symbol (%) as the comment character; that is a symbol which tells TeX to consider the text following as comments and NOT as text to be typeset. This is especially useful for a LaTeX programmer to explain a particularly sticky bit of code to others. But then, how do you get

a percent sign in the output? Just type \% as in

latex
Maybe I have now learnt about 1\% of \LaTeX.

which would give us:

Maybe I have now learnt about 1% of LaTeX.

The symbols \ and % are just two (2) of the ten (10) charcaters TeX reserves for its internal use. The complete list is:

text
~ # $ % ^ & _ \ { }

We have seen how LaTeX uses two of these symbols and others we will see as we proceed. Also, we have noted that \ is produced in the output by the command \textbackslash and % is produced by \%. What about the other symbols? The table below gives the inputs to produce these symbols.

Output Command Output Command
~ \textasciitilde & \&
# \# _ \_
$ \$ \ \textbackslash
% \% { \{
^ \textasciicircum } \}
a b v v m c 2
Table 2.3: Commands to print out additional control symbols.

As we can see from the aforementioned table that except for three (3), all special symbols are produced by preceding them with a \. Of the exceptional three, we have seen that änd äre used for producing accents. So what does \\ do?

It is used to break lines.

For example,

latex
This is the first line. \\ This is the second line.

produces

This is the first line. This is the second line

We can also give an optional argument to \\ to i ncrease the vertical distance between the lines. For example,

latex
This is the first line. \\[10pt] This is the second line.

gives

This is the first line. This is the second line

Now there is an extra 10 points of space between the lines. 11 11 1 point (1pt) is about 1 7 2 th of an inch.

2.2.6 Position of Text

We have seen up to now, that LaTeX aligns text in its own way , regardless of the way text is formatted in the input file. 12 12 To a certain extent LaTeX does NOT care about how many spaces there are between words or whether there are numerous spaces between paragrahs. Now suppose you want to typeset something like this

Management Centre Innsbruck Mechatronics Welcomes You!

This is to certify that you have chosen to become great engineers by the time your degree is finished!!

The Lecturer D. T. McGuiness

Which is produced by the following command:

latex
\begin{center} Management Centre Innsbruck Mechatronics\\[.75cm] Welcomes You! \end{center} \noindent This is to certify that you have chosen to become great engineers by the time your degree is finished!! \begin{flushright} The Lecturer\\ D. T. McGuiness \end{flushright}

Let’s have a look at the comments. Here, the commands:

latex
\begin{center} ... \end{center}

typesets the text between them exactly at the center of the page and the commands

latex
\begin{flushright} ... \end{flushright}

typesets text flush with the right margin. The corresponding commands

latex
\begin{flushleft} ... \end{flushleft}

places the enclosed text flush with the left margin. 13 13 Change the flushright to flushleft and see what happens to the output.

These examples are an illustration of a LaTeX construct called an environment , which is of the form

latex
\begin{name} ... \end{name}

where name is the name of the environment. We have seen an example of an environment at the very beginning of this chapter, namely the document environment.

2.3 Fonts

The actual letters and symbols 14 14 collectively called type. which LaTeX produces are characterized by their style and size . For example, in this lecturebook emphasized text is given in italic style and the example inputs are given in typewriter style. We can also produce smaller and bigger type.

A set of types of a particular style and size is called a font.

2.3.1 Type Style

In LaTeX, a type style is specified by family, series and shape. They are shown in the Table ??

Style Command
Family roman \textrm{roman}
sanf serif \textsf{sans serif}
typewriter \texttt{typewriter}
Series medium \textmd{medium}
boldface \textbf{boldface}
Shape upright \textup{upright}
italic \textit{italic}
slanted \textsl{slanted}
small cap \textsc{small cap}
Table 2.4: Different type-styles supported by LaTeX.

Any type style in the output is a combination of these three (3) characteristics. For example, by default we get roman family, medium series, upright shape type style in a LaTeX output. The \textit command produces roman family, medium series, italic shape type. Again, the command \textbf produces roman family, boldface series, upright shape type.

We can combine these commands to produce a wide variety of type styles. For example, the input:

latex
\textsf{\textbf{sans serif family, boldface series, upright shape}} \textrm{\textsl{roman family, medium series, slanted shape}}

which produces the following output:

sans serif family, boldface series, upright shape roman family, medium series, slanted shape

It may be possible that some of these type styles may NOT be available in your computer. In that case, LATEX gives a warning message on compilation and substitutes another available type style which it thinks is a close approximation to what you had requested.

We can now tell the whole story of the \emph command. We have seen that it usually, that is when we are in the middle of normal (upright) text, it produces italic shape. But if the current type shape is slanted or italic, then it switches to upright shape. Also, it uses the family and series of the current font. Thus

latex
\textit{A polygon of three sides is called a \emph{triangle} and a polygon of four sides is called a \emph{quadrilateral}}

A polygon of three sides is called a triangle and a polygon of four sides is called a quadrilateral

whereas the following input:

latex
\textbf{A polygon of three sides is called a \emph{triangle} and a polygon of four sides is called a \emph{quadrilateral}}

produces:

A polygon of three sides is called a triangle and a polygon of four sides is called a quadrilateral

Each of these type style changing commands has an alternate form as a declaration . For example, instead of \textbf{boldface} you can also type {\bfseries boldface} to get boldface.

Note that that not only the name of the command, but its usage also is different.

For example, to typeset:

By a triangle , we mean a polygon of three sides.

if we were to write:

latex
By a \bfseries{triangle}, we mean a polygon of three sides.

we would get:

By a triangle, we mean a polygon of three sides.

Therefore to make the declaration act upon a specific piece of text, 15 15 and no more the declaration and the text should be enclosed in braces . Table 2.5 completes the one given earlier, by giving also the declarations to produce type style changes.

Style Command Declaration
Family roman \textrm{roman} {\rmfamily roman}
sanf serif \textsf{sans serif} {\sffamily sans serif}
typewriter \texttt{typewriter} {\ttfamily typewriter}
Series medium \textmd{medium} {\mdseries medium}
boldface \textbf{boldface} {\bfseries boldface}
Shape upright \textup{upright} {\upshape upright}
italic \textit{italic} {\itshape italic}
slanted \textsl{slanted} {\slshape slanted}
small cap \textsc{small cap} {\scshape small cap}
Table 2.5: Different type-styles supported by LaTeX along with their declaration versions.

These declaration names can also be used as environment names. Therefore to typeset a long passage in, say, sans serif, just enclose the passage within the commands:

latex
\begin{sffmily} ... \end{sffamily}

2.4 Type Size

Traditionally, type size is measured in (printer) points. The default type that TeX produces is of 10 pt size. There are some declarations (ten, to be precise) provided in LaTeX for changing the type size. They are given in Table 2.6 :

Size Command Size Command
size \tiny size size \large size
size \scriptsize size size \Large size
size \footnote size size \LARGE size
size \small size size \huge size
size \normalsize size size \Huge size
Table 2.6: Different type-styles supported by LaTeX along with their declaration versions.

Note that the \normalsize corresponds to the size we get by default and the sizes form an ordered sequence with \tiny producing the smallest and \Huge producing the largest. Unlike the style changing commands, there are no command-with-one-argument forms for these declarations.