LaTeX Basics

This is an excerpt of the book “Better Books with LaTeX.” The book comes with a LaTeX template you can use to easily create your own books.

This section will cover the basics of LaTeX so that you can make adjustments to your existing texts, understand the existing commands in the template, and get a head start for when you want to learn more complex commands on your own.

  Basic Formatting

  • Comments. If you want to just add a comment to a file without it being printed, add a % (percentage) sign in front of it. The text in the affected line will be displayed in blue in Overleaf . In the template files, you will find a number of such comments as well as deactivated commands (for example, in lib/bookformat.tex the different options for the book size).
  • Bold formatting. You can make your text bold by either marking it and pressing CTRL + B (in Overleaf ), or by surrounding it with the command \textbf{}.
  • Italics formatting. You can make your text italic by either marking it and pressing CTRL + I (in Overleaf ), or by surrounding it with the command \textit{}.
  • Small caps. You can change your text into small capitals by surrounding it with the command \textsc{}.
  • Em dashes. Em dashes are used to connect two related sentences. There is no space before or after the em dash. Within the template, use the command \emdash{} instead of using the dash you copied over from your text file. This will also take care of issues relating to line breaks.
  • Paragraphs. Paragraphs are handled automatically by leaving an empty line between each paragraph. Adding more than one empty line will not change anything—remember it is not a “what you see is what you get” editor.
  • Empty line. If you want to force an empty line (recommended only in special cases), you can use ˜\\ (tilde followed by two backslashes).
  • New page. Pages are handled automatically by LaTeX. It tries to be smart in terms of positioning paragraphs and pictures. Sometimes it is necessary to add a page break, though (ideally, at the very end when polishing the final text). For that, simply add a \newpage.
  • Quotation marks. In the normal computer character set, there are more than one type of quotation marks. It is required to change all quotation marks into  (two back ticks at the beginning and two single ticks at the end) and refrain from using ”…” (or …) altogether. This is because Word’s … uses special characters, and ”…” do not mark the beginning and end of the quotation.
  • Horizontal line. For a horizontal line, simply write \hrule.
  • Underlined text. It is generally not recommended to use underlined text.
  • URLs. For URLs you need a special monospaced font. Also, for URLs in e-books, you want to make them clickable. Both can be accomplished by putting the URL in the \url environment, for example “\url{https://www.lode.de}”.
  • Special characters. If you need special characters or mathematical formulas, there is a whole body of work on that subject. It is not in the scope of this book to provide you a comprehensive list.

 Lists

  • Itemized list. To create a bullet point list (like the list in this section), use the following construct:
     
    \begin{itemize} 
      \item Your first item. 
      \item Your second item. 
      \item Your third item. 
    \end{itemize}
          
    
  • Numbered list. To create a numbered list, replace itemize with enumerate:
     
    \begin{enumerate} 
      \item Your first item. 
      \item Your second item. 
      \item Your third item. 
    \end{enumerate}
          
    

    The result will look like this:

    1.
    Your first item.
    2.
    Your second item.
    3.
    Your third item.

 Verbatim Text

Sometimes, you may want to simply use text in a verbatim way (including special characters and LaTeX commands). For this, simply use the \lstlisting environment: \begin{lstlisting} …\end{lstlisting}. For example, I put the itemize and enumerate listings above into a \lstlisting block. If I did not, LaTeX would have displayed the list as a list, instead of displaying the code.

 Chapters and Sections

LaTeX uses a hierarchy of chapters, sections, and subsections. There are also sub-subsections, but for the sake of the reader, it is best to not go that deep. If you come across a situation where it looks like you need it anyway, I recommend thinking over the structure of your book rather than using sub-subsections.

In terms of their use in the code, they are all similar:

  • \chapter{Title}\label{c1_chaptername:cha}
  • \section{Title}\label{c1_sectionname:sec}
  • \subsection{Title}\label{c1_subsectionname:sec}

When using these commands, obviously replace the title, but also the label. For the label, I recommend to have it start with c, followed with the current chapter number, an underscore, and the chapter, section, or subsection in one word and lowercase, followed by either “:cha” or “:sec” to specify what kind of label it is. These labels can then be used for references like we used previously for the images. For example, if you have defined a section “\section{Chapters and Sections}\label{c1_chaptersandsections:sec},” you could write “We will discuss chapters and sections in section \ref{c1_chaptersandsections:sec}” which results in the document in “We will discuss chapters and sections in section 3.4”.

  Tables

In LaTeX, tables are like images and put into the figure environment. As such, they have a caption, label, and a positioning, as we discussed above with the images. Drawing a table like Table ?? (see Chapter ??) requires a bit of coding:

 
 
\begin{figure}[H]\centering 
\ifxetex 
\resizebox{\textwidth}{!}{% 
\begin{tabular}{p{3cm}|p{6cm}|p{6.5cm}} 
\else 
{\begin{tabular}{c|c|c} 
\fi 
\hline 
&\textbf{Word}&\textbf{LaTeX}\\\hline 
Editor&‘‘what you see is what you get’’&source file is compiled\\\hline 
Compatibility&dependent on editor&independent of editor\\\hline 
Graphics&simple inbuilt editor, mouse-based&powerful but complex editor, text-based\\\hline 
Typography&optimized for speed&optimized for quality\\\hline 
Style&inbuilt style&separate style document\\\hline 
Multi-platform&only via export&possible with scripting\\\hline 
Refresh&some elements need manual refresh&everything is refreshed with each compile\\\hline 
Formulas&basic support needs external tools&complete support\\\hline 
 
\end{tabular}} 
\caption{Comparison of \textit{Word} and \textit{LaTeX}} 
\label{c1_comparisonwordlatex:fig} 
\end{figure}
 

This table from the beginning of the book has the familiar figure, label, caption, and centering commands. The actual table is configured with the tabular environment. Following the tabular command, you configure the columns in curly braces. Each column is separated with a vertical line and the “p{…}” entry specifies the width of the column. With “{p{3cm}—p{6cm}—p{6.5cm}},” you would have three columns with 3cm width for the first column and 6cm and 6.5cm width for the two others. Alternatively, you can use “c” instead of “p” and leave out the curly braces with the width. Then, LaTeX simply calculates the required widths automatically (the preferred way for the e-book).

Then, for each line of the table, simply write “content of the first cell&content of the second cell&content of the third cell\\\hline”. Please be sure to use \hline in tabular environments, while \hrule outside of them.

  Footnotes

Finally, for footnotes, there is the command \footnote. You can place it anywhere you like, LaTeX will then automatically add the number of the footnote at that place, and put the footnote text into the footer area. For e-books, it is recommended to just use normal parentheses. This can be accomplished again with the \ifxetex command.

 
 
\ifxetex 
    .\footnote{This  is a footnote.} 
\else{} 
    (this is a footnote). 
\fi{}
 

In the generated book, it looks like this (this is a footnote).

  Images

As in Word, in LaTeX, images are separate from the text. Images are usually packaged together with a caption and a label to reference it from the text. These three entities are packaged together into a figure. The figure itself configures the size of the image as well as where it should be put. Let us look at a code sample:

 
 
\begin{figure}[H] 
  \centering 
  \ifxetex 
    \adjustbox{max width=.95\columnwidth, max height=.4\textheight}{ 
      \includegraphics{images/ebookLatex_Cover.png} 
    } 
  \else 
    \includegraphics{images/ebookLatex_Cover.jpg} 
  \fi 
  \caption{The cover of this book.} 
  \label{c1_cover:fig} 
\end{figure}
 

Let us go through this line by line.

  • At the core is the image, included with \includegraphics{path to file}. It inserts the image specified by the “path to file.”
  • In the example, there are two versions of \includegraphics, one for the print output (\ifxetex), one for the e-book. This way, we include either the larger, lossless PNG version, or a smaller, lossy JPG version of the cover.
  • For the e-book, we do not need to provide any image size as the first output will be a HTML file with no boundaries.
  • For the print edition, we have a clear boundary, namely the page width.
  • With the \adjustbox command, we can adjust the image size according to the page width (\columnwidth) and page height (\textheight).
  • In this case, in order to align with the caption and be able to show two images on one page, we want an image to be generally at most 95% of the page width and 40% of the page height.
  • Below this \ifxetex – \else – \fi construct, there is the caption and the label.
  • LaTeX automatically numbers each figure, so in the text, we can later refer to it with “\ref{c1_cover:fig}” which prints out the number of the figure.
  • Finally, all these commands are centered with the \centering command and surrounded with the figure environment.
  • The [H] instructs LaTeX to try to place the image exactly where it is in the LaTeX code.

Figure 3.1: The cover of this book.

In Figure 3.1, you can see the result of the command.

Instead of graphics, you can also include other TeX files that contain graphics (or commands to draw graphics, see Chapter 3.8).

 TikZ Graphics

For graphics, you can use the inbuilt TikZ graphics generator. Due to its flexibility, I even recommend images you already have for a number of reasons:

  • TikZ graphics can very easily changed (especially for for example translations or making corrections).
  • TikZ graphics are small and flexible. They can be easily scaled to any size and are directly integrated into your project (no time-consuming editing in an external graphics program necessary).
  • TikZ graphics look better. As vector graphics are sent directly to the printer, we need not worry about readability.

If you want to create a TikZ graphic, simply create a new LaTeX file in the tex-images folder and include it with \input (replacing \includegraphics) where you want to.

Then, do a “recompile from scratch” by clicking on the top right corner of the preview window (showing Warning or Error) to regenerate the TikZ file. If “up-to-date and saved” is shown, delete the tikz-cachedirectory and recreate it. This deletes the generated cached image files that do not show up in Overleaf itself.

For the format of the file itself, it is a series of commands surrounded by the \begin{tikzpicture}…\end{tikzpicture} environment. Discussing all the commands is beyond the scope of this book, so I recommend three options:

  • Check out the PGF manual at https://www.ctan.org/pkg/pgf. It is more than 1100 pages full with documentation of each command and corresponding examples.
  • Check out the few example TikZ pictures from my two books Philosophy for Heroes: Knowledge and Philosophy for Heroes: Continuum in the tex-images directory.
  • Send us descriptions / drafts of the graphics you need. We can help you at affordable rates. Contact us at [email protected]
2018-10-23T22:05:07+00:00Tags: |