Understanding how LaTeX reads our input
Before we continue writing, let's look at how LaTeX understands what we type:
- Besides simple alphabet characters, we can directly type (or copy and paste) accented characters, such as ä, ü, and ö, as well as further characters from other languages, such as Greek or Russian.
- A space in the input code will appear as a space in the output document. Several consecutive spaces are treated as one space.
- An end of a line in the source code is treated as a space.
- An empty line in the source code is treated as a paragraph break.
There are some characters with special meanings:
- A backslash,
\
, starts a LaTeX command or a LaTeX macro. - Curly braces and square brackets are used for command arguments.
- A dollar sign,
$
, starts and ends math mode, which we will explore in Chapter 9, Writing Math Formulas. - A percent sign,
%
, tells LaTeX to ignore the rest of the line.
Let's expand upon that last point: the percent sign introduces a comment. Everything following a percent sign until the end of the line will be ignored by LaTeX and won't be printed out. This enables you to insert notes into your document. It's often used in LaTeX templates to inform the user of what the template does or requires the user to do at a certain point. Note that the end of the line, normally behaving as a space, will also be ignored after a percent sign.
Easing experimenting by trial and error
If you want to disable a command temporarily, it may be favorable to insert a percent sign instead of deleting the command. That way, you're able to undo this change easily by removing the percent sign.
If that is how the percent sign works, what should we do if we want to write 100% in our text? And what about the other special symbols? Let's figure out how to solve that issue in the next section.
Printing out special symbols
Common text mostly contains uppercase and lowercase letters, digits, and punctuation characters that you can simply type into your editor. However, some characters are reserved for LaTeX commands and cannot be used directly. We already encountered such characters, including the percent sign and curly braces. To fix this issue, there are LaTeX commands to print such symbols.
We will write a very short example printing out an amount of dollars and a percent number, along with some other symbols:
- Create a new document and enter the following lines:
\documentclass{article} \begin{document} Statement \#1: 50\% of \$100 equals \$50. More special symbols are \&, \_, \{ and \}. \end{document}
- Click the Typeset button to compile the document.
- Check out the output:
By putting a backslash before a special symbol, we turned it into a LaTeX command. The only purpose of this commend is to print out that symbol.
Printing the backslash
You may be wondering how to print a backslash. The command for printing a backslash is \textbackslash
. If you would like to know what \\
might be used for, it is used as a shortcut for a line break. That may seem a bit odd, but line breaks occur frequently whereas backslashes are rarely needed in the output, therefore this shortcut has been chosen.
There's a wealth of symbols that we can use for math formulas, chess notation, zodiac signs, music scores, and more. We don't need to deal with those symbols for now, but we shall return to that subject in Chapter 9, Writing Math Formulas, when we will need symbols to typeset math formulas.
Now that we know how to enter pure text, let's find out how we can format it.