- LaTeX Cookbook
- Stefan Kottwitz
- 1171字
- 2021-07-09 21:24:57
Writing a short text
While LaTeX is great for big documents, it's just as useful for smaller ones, and you get all the features to work with. Writing down homework or producing a seminar handout, for example, doesn't need book-like chapters, and the layout would not be very spacious. So we will choose a document class that suits the task at hand best.
There are class bundles that cover commonly used document types. Every LaTeX installation contains the base bundle with standard classes. There are class files for articles, books, reports, letters, and more. It is stable stuff; it has not really been changed for many years. If you don't care about the latest style, this can be sufficient. It would even run on a ten-year-old LaTeX installation.
In this recipe, we will use a class of the KOMA-Script bundle. This is a set of classes that were originally designed with the aim of replacing the standard classes and providing more features. In contrast to the rather static base bundle, KOMA-Script has been extensively developed during recent years. It became feature-rich and got an excellent user interface. Parts of its functionality are provided in packages that can be used together with other classes as well. You can identify KOMA-Script classes and packages by the prefix scr
. This prefix stands for script, which was the initial name of this bundle.
How to do it...
We will start with a complete small document, already using various features. This can be your template, into which you can add your own text later on.
While we go through the document step by step, you may open the complete code directly with your editor, so you don't need to type it. It is contained in the code bundle available at the book's page https://www.packtpub.com and at the book's page http://latex-cookbook.net. Perform the following steps to create a small document:
- Start with a document class. We will use the KOMA-Script class
scrartcl
, with A4 paper size, a base font size of 12 pt, and interparagraph spacing instead of the default paragraph indentation:\documentclass[paper=a4,oneside,fontsize=12pt, parskip=full]{scrartcl}
- Begin the document:
\begin{document}
- Let LaTeX print a table of contents using this command:
\tableofcontents
- Start a section without numbering:
\addsec{Introduction}
- Add some text:
This document will be our starting point for simple documents. It is suitable for a single page or up to a couple of dozen pages. The text will be divided into sections.
- Start an automatically numbered section with text:
\section{The first section} This first text will contain
- Add a bulleted list using an
itemize
environment. Each list item starts with\item
. Using\ref{label}
, we will refer to labels, which we will create later:\begin{itemize} \item a table of contents, \item a bulleted list, \item headings and some text and math in section, \item referencing such as to section \ref{sec:maths} and equation (\ref{eq:integral}). \end{itemize}
- Continue with text and start another numbered section:
We can use this document as a template for filling in our own content. \section{Some maths}
- Set a label so that we can refer to this point whenever we want to refer to this section:
\label{sec:maths}
- Continue with the text. We will start using some mathematical expressions in the text. We mark this by enclosing them in parentheses with a prefixing backslash, that is,
\( … \)
.When we write a scientific or technical document, we usually include math formulas. To get a brief glimpse of the look of maths, we will look at an integral approximation of a function \( f(x) \) as a sum with weights \( w_i \):
- Write a mathematical equation using the
equation
environment. Again, place a label by using the following commands:\begin{equation} \label{eq:integral} \int_a^b f(x)\,\mathrm42z9v2bx \approx (b-a) \sum_{i=0}^n w_i f(x_i) \end{equation}
- End the document:
\end{document}
- Compile the document. The first page of the output will look like this:
How it works...
In the first line, we loaded the document class scrartcl
. In square brackets, we set options for specifying an A4 paper size with one-sided printing and a font size of 12 pt. Finally, we chose to have a full line between paragraphs in the output so that we can easily distinguish paragraphs.
Note
The default setting is no space between paragraphs and a small indentation at the beginning of each paragraph. Uncheck the parskip
option to see it. We chose a paragraph skip because many people are used to it when working with e-mails, while indentation costs line space, which is a precious resource on small electronic devices.
Without further ado, we began the text with a table of contents. While numbered sections are started by the \section
command, we can start unnumbered sections by using the starred version \section*
. However, we used the KOMA-Script command \addsec
for the first unnumbered section. That's because, in contrast with \section*
, the \addsec
command generates an entry in the table of contents.
An empty line tells LaTeX to make a paragraph break.
As bulleted lists are a good way to clearly present points, we used an itemize
environment for this. All environments start with the \begin
command and are ended with the \end
command.
An equation
environment has been used to display a formula that is automatically numbered. We used the \label
command to set an invisible anchor mark so we could refer to it using its label name by the \ref
command and get the equation number in the output.
Small formulas within text lines have been enclosed in \( ... \)
, which provides inline math mode. Dollar symbols, $ ... $
, can be used instead of \( ... \)
, which makes typing easier. However, the use of parentheses makes it easier to understand where the math mode starts and where it ends, which may be beneficial when many math expressions are scattered throughout the text.
For further information on math typesetting, refer to Chapter 10, Advanced Mathematics, specifically to the recipe Finetuning a formula.
See also
The part of the document before the \begin{document}
command is called the preamble. It contains global settings. By adding a few lines to our document preamble, we can improve our document and modify the general appearance. Chapter 2, Tuning the Text, starts with additions that are beneficial for small documents as well. They enable the direct input of accented characters and unicode symbols, and they improve justification and hyphenation.
In Chapter 3, Adjusting Fonts, you can find recipes for changing the fonts of either the whole document or of certain elements.
For further customization tasks, such as modifying page layout, and adding a title page, refer to the recipe Designing a book in the current chapter. We will take a look at such settings on the occasion of a book example.
- TypeScript Blueprints
- Learning PostgreSQL
- Learning Spring 5.0
- Web交互界面設(shè)計與制作(微課版)
- Android Development with Kotlin
- 從程序員到架構(gòu)師:大數(shù)據(jù)量、緩存、高并發(fā)、微服務(wù)、多團隊協(xié)同等核心場景實戰(zhàn)
- Python Game Programming By Example
- Swift 3 New Features
- Python應(yīng)用輕松入門
- 青少年P(guān)ython編程入門
- 程序員修煉之道:通向務(wù)實的最高境界(第2版)
- 響應(yīng)式架構(gòu):消息模式Actor實現(xiàn)與Scala、Akka應(yīng)用集成
- 小型編譯器設(shè)計實踐
- 零基礎(chǔ)學Python編程(少兒趣味版)
- Java程序設(shè)計與項目案例教程