About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-12-13: Ebook sale (12th December 2024 – 1st January 2025): cybercrime fiction short stories Unsocial Media and Smile for the Camera free; crime fiction short story The Briefcase free; short story noir crime fiction I’ve Heard the Mermaid Sing free; crime fiction novel The Private Enemy US$1.99; illustrated children’s story The Foolish Hedgehog US$0.99.


4. Creating a Simple Document

Having installed and tested the software, let's now look at how to actually write the source code. The very first line of any document that you create must have the command:

\documentclass[<option-list>]{<class-name>}

This tells LaTeX what type of document you want to create (for example an article, a technical report or correspondence). The \documentclass command takes one mandatory argument, <class-name>, that specifies the class file.

There are many class files available, and some publishers, institutions or journals provide their own custom classes (for example, the jmlr class for the Journal of Machine Learning Research). Popular classes include memoir (for books and reports) and those supplied in the KOMA-Script bundle (for books, reports, articles and correspondence). There's also beamer (for presentations) as well as classes for typesetting exams, flashcards, concert programmes etc. For simplicity, this book will concentrate on three of the KOMA-Script classes scrartcl (for articles), scrreprt (for technical reports, theses etc) and scrbook (for books).

We'll start with a very simple document, so let's use the scrartcl class file. In this case the very first line of the source code should be:

\documentclass{scrartcl}

The \documentclass command also takes an optional argument, <option-list>, which should be a comma separated list of options to be passed to the class file. This allows you to override the class file defaults. For example, the scrartcl class file by default uses A4 paper, but if you are in the USA you will probably want to use letter paper. This can be achieved using the option letterpaper. So you would need to edit the above line to:

\documentclass[letterpaper]{scrartcl}

Let's change another option. The normal font size is 11pt by default, but we have the option to change it, so let's use 12pt:

\documentclass[letterpaper,12pt]{scrartcl}

You can also change your document so that it is in a two-column format using the twocolumn option:

\documentclass[letterpaper,12pt,twocolumn]{scrartcl}

After deciding what type of document you want, you now need to specify the contents of the document. This is done inside the document environment. The document is started with the command:

\begin{document}

and ended with

\end{document}

(LaTeX stops reading the file when it reaches the above line, so anything occurring after it is ignored.)

My source code now looks like:

\documentclass[12pt]{scrartcl}

\begin{document}

\end{document}

Every document you create must have this form. You can't simply start typing the document text. You must first specify your class file, and then place the contents of the document inside the document environment.

So far so good, but at the moment we have an empty document, so we won't get any output. Let's now put some text into our document:

\documentclass[12pt]{scrartcl}

\begin{document}

This is a simple document.
Here is the first paragraph.

Here is the second paragraph. As you
can       see it's
a rather
short paragraph, but not as short as the previous one.

\end{document}

Top Five Mistakes Made by New Users

I first started teaching LaTeX in 1998, and these are the most common errors I've seen when people start learning LaTeX:

  1. Missing out the backslash \ at the start of one or more of the commands.

  2. Using a forward slash / instead of a backslash \.

  3. Forgetting \end{document}.

  4. Misspelling “document” (in \begin{document} and \end{document}).

  5. Missing a closing brace }.

If you encounter any problems when you start out, go through that check list first. Then check Appendix B. Common Errors.

Whenever you start a new document, always type in the \documentclass, \begin{document} and \end{document} commands first (Figure 4.1). Then move your cursor between the \begin and \end lines and type the document text (Figure 4.2).

Figure 4.1: Starting a New Document: always type these three lines first.
 
Figure 4.2: Starting a New Document: move the cursor inside the document environment and start typing the document text.

 

Exercise 1: Simple Document

Try typing the code in the above example into TeXWorks or the editor of your choice (see the previous section if you can't remember what to do.) You can also download a copy of this file, but I recommend that you try typing it in to give yourself some practice.

Things to note while you are typing: firstly, when you press the return character at the end of a line this end-of-line character is converted into a space in the output file. So the fact that I have some very ragged lines in my source code has no effect on the final result. (Note that some front-ends will reformat your lines as you type.) Whereas a completely blank line will be converted into a paragraph break (\par has the same effect).

Secondly, multiple spaces are converted into a single space, so the large gap between the words “can” and “see” is no different from having a single space.

Once you have typed up your source code, save your file (called, for example, exercise1.tex), and run PDFLaTeX as described in §3.1. TeXWorks. If all goes well, TeXWorks should display the resulting PDF file in a new window, usually alongside the window containing the source code.

Notes:

  1. Each paragraph automatically starts with an indentation in the PDF.

  2. There is no blank line between the paragraphs in the PDF document. (See what happens if you add the KOMA-Script class option parskip=full:

    \documentclass[12pt,parskip=full]{scrartcl}

    and rebuild the PDF.)

  3. Move the mouse over one of paragraphs in the PDF viewer and pop-up the context menu (usually a right mouse click). Select Jump to Source. The window containing the source code should now gain the focus and the line of code matching the typeset line you clicked on in the PDF should now be highlighted.


This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-00-5).

© 2012 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs