Categories
LaTeX Publishing

LaTeX Basics

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

This is an excerpt from Better Books with LaTeX the Agile Way. You can get a copy here.

This article will cover the basics of LaTeX so that you can make adjustments to your existing texts, understand the 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. In Overleaf, the text in the affected line will be displayed in a different color. In the template files, you will find several 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 selecting it and pressing CTRL + b (in Overleaf), or by surrounding it with the command \textbf{Your Text}.
  • Italics formatting. You can italicize your text by either selecting it and pressing CTRL + i (in Overleaf), or by surrounding it with the command \textit{Your Text}.
  • Small caps. You can change your text into small capitals by surrounding it with the command \textsc{Your Text}.
  • Em dashes. Em dashes are used to connect two related sentences and can also be used instead of parentheses or commas. 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 Overleaf is not a “what you see is what you get” editor.
  • Empty line. If you want to force an empty line (recommended only in exceptional cases), you can use ˜\\ (tilde followed by two backslashes). Possible applications are in the front matter; for example, in this book, I have used it for the title and half-title page, as well as the publisher page, and the preface and foreword (for the signature at the bottom).
  • New page. Pages are handled automatically by LaTeX. It tries to be smart in terms of positioning paragraphs and pictures. Sometimes, (ideally, at the very end when polishing the final text), it is necessary to add a page break. For that, simply add a \newpage. If you want to insert a page break in your e-book version, too (for example, after chapters or special pages like the title or publisher page), use \nextpage instead.
  • Quotation marks. In the standard computer character set, there are several types 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). Refrain from using ”…” (or “…”) to prevent issues with the LaTeX compilation.
  • Horizontal line. For a horizontal line, simply use \hrule:_____________________________________________
  • Underlined text. It is generally not recommended to use underlined text in books.
  • 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}https://www.lode.de
  • Special characters. If you need special characters or mathematical formulas, there is a whole body of work on that subject. Check out tug.ctan.org/info/symbols/comprehensive/symbols-a4.pdf for 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}  

The result will look like this:

  • Your first item.
  • Your second item.
  • Your third item.

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 beginning the label 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 which will show the correct chapter and section number even after making changes. For example, if you have defined a section \section{Chapters and Sections}\label{c14_chaptersandsections:sec}, you could write We will discuss chapters and sections in Section \ref{c14_chaptersandsections:sec} which results in the document as “We will discuss chapters and sections in Section 14.4.”

Tables

In LaTeX, tables are like images and put into the figure environment (\begin{figure}…\end{figure}). As such, they have a caption, label, and positioning. Drawing a table like Table 11.3 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 Word and LaTeX} 
\label{c1_comparisonwordlatex:fig} 
\end{figure}  

This table 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 (this is the preferred method for an 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, and \hrule outside of them.

E-Book / PDF Specific Content

To activate content only for either HTML output (e-book) or for PDF output (print), simply surround the code with a \ifxetex{} …\else{}…\fi{} construction. Having this compatibility allows you to generate PDF files with XeLaTeX, and also produce HTML documents with pdfLaTeX when you switch compiler settings.

For example, when generating an HTML file, you cannot include PDF files or vector graphics. Instead, you have to rely on JPG and PNG image files. Another application would be if you want to minimize the size of an existing image file for an e-book. A code might look like this:

 
 
\begin{figure}[H]\centering 
\ifxetex 
\adjustbox{max width=.95\columnwidth, max height=.4\textheight}{ 
\input{images/philosophy-hierarchy.tex} 

\else 
\includegraphics{images/philosophy-hierarchy.png} 
\fi 
\label{c1_ontology-epistemology:fig} 
\end{figure}  

This includes a vector graphic (images/philosophy-hierarchy.tex for the PDF output, and a PNG image file for the HTML output.

Yet another example is using different texts for the PDF (designed for print) and the HTML output (designed for an e-book release). The conditional clause allows you to show medium-specific text, dates, or formatting:

 
 
\ifxetex{} 
2016, First Edition 
\textsc{ISBN} 978-3-945586-21-1 
Printed on acid\hyp{}free, unbleached paper. 
\else{} 
E-book created \today 
\textit{PS: If you want to rate this book, please always add a short text comment. Did you like it? What can be improved? Who would you recommend it to? Without a text comment, your star rating will not be counted on the Amazon website!} 
\fi{}  

This shows the edition number, ISBN, and paper quality for the PDF output, and the creation date and a note about how to rate the book for the HTML output.

A further example is footnotes. As e-books do not have pages in the traditional sense, your footnotes would end up in the appendix of the book at the end. Given that we do not want the reader to jump back and forth, one approach is to simply include the footnote in parentheses if the output is not set to XeLaTeX (print):

 
 
A popular assumption is that the same words convey the same meanings. This is generally only correct if both conversation partners belong to a common \textit{language network}, i.e., that they define their terms either among themselves or through close acquaintance\ifxetex{}.\footnote{Interesting\else{} (interesting\fi{} to note here is the theory that every person in the world is connected to every other person by approximately seven intermediate connections\ifxetex{} \citep[cf.][]{Travers69anexperimental}.}\else, \cite[cf.][]{Travers69anexperimental}).\fi{}  

This shows “Interesting to note here is the theory that every person in the world is connected to every other person by approximately seven intermediate connections [cf. Travers and Milgram1969].” as a footnote for the PDF output, and “[…] close acquaintance (interesting to note here is the theory that every person in the world is connected to every other person by approximately seven intermediate connections, [cf. Travers and Milgram1969]).” for the HTML output.

The last example is handling references. In a printed book, you can add a quotation page at the end to list the sources of individual quotes. This is possible because you can quickly jump to the end of the book and back using a page number, while in an e-book, you have no fixed page numbers and have to rely on links:

 
 
\ifxetex{}\label{epicurus-philosophy-quote}\else{}\citep[p.~53]{epicurus}\fi{}  

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 regular parentheses. This can be accomplished with the \ifxetex{} command. In the generated book, it looks like this (this is a footnote).

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

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 the image 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_cover1:fig} 
\end{figure}  
  • At the core is the image, included with \includegraphics{filename}. It inserts the image specified by the “filename.”
  • 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 an 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 with its label.
  • LaTeX automatically numbers each figure, so in the text, we can later refer to it with \ref{c1_cover1: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.

TikZ Graphics

For graphics, you can also include other TeX files that contain graphics or commands that use the inbuilt TikZ graphics generator. Due to its flexibility, I even recommend using TikZ to replace any of your existing diagrams that are PNG or JPG images, because:

  • TikZ graphics can very easily be changed (for example, in translations or for 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, they are sent directly to the printer, so 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 to regenerate the TikZ file. If this does not work for some reason, you can also force their re-creation by deleting the tikz-cache directory and then recreating it by clicking on the second icon at the top left (New Folder). This removes 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 TikZ 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 has more than 1,100 pages of documentation of each command and corresponding examples.
  • Check out the example TikZ pictures from my two books Philosophy for Heroes: Knowledge and Philosophy for Heroes: Continuum in the tex-images directory of the template.
  • Send us descriptions/drafts of the graphics you need. We can help you to create any graphic at affordable rates. Contact us at mail@lode.de.

This is an excerpt from Better Books with LaTeX the Agile Way. You can get a copy here.

Leave a Reply