Troubleshooters.Com, T.C Linux Library and Litt's LyX LibraryPresent

Litt's LaTeX Laboratory

Copyright (C) 2005 by Steve Litt, All rights reserved. Material provided as-is, use at your own risk.


Introduction

LaTeX is a typesetting language considered by many to create the most beautiful and readable documentation. Authoring with LaTeX makes your document printable and readable on a huge variety of paper, paper sizes, operating systems, and digital formats.

LaTeX is a complex programming language. In my opinion it's harder to learn than Perl, C++ or C. If you really want to be effective using LaTeX or even LyX, you must master LaTeX. This document endeavors to give you the 10% of LaTeX knowledge you'll need for 90% of your work.

Hello World

Let's do a proof of concept with the simplest possible LaTeX document. Create the following file and save as hello.tex:

\documentclass{book}
\begin{document}
Hello, world!
\end{document}

The first line declares this document to be of class book. A document class is what describes the styles that govern a document. The second line declares that the document has begun, the third is its content, and the fourth declares the end of the document.

Delete any previous hello.dvi file with the following command:
rm hello.dvi
Now compile the document with the following command:
latex hello.tex
The output should look something like this:
[slitt@mydesk slitt]$ latex hello.tex 
This is TeX, Version 3.14159 (Web2C 7.4.5)
(./hello.tex
LaTeX2e <2001/06/01>
Babel <v3.7h> and hyphenation patterns for american, french, german, ngerman, b
asque, italian, portuges, russian, spanish, nohyphenation, loaded.
(/usr/share/texmf/tex/latex/base/book.cls
Document Class: book 2001/04/21 v1.4e Standard LaTeX document class
(/usr/share/texmf/tex/latex/base/bk10.clo)) (./hello.aux) [1] (./hello.aux) )
Output written on hello.dvi (1 page, 232 bytes).
Transcript written on hello.log.
[slitt@mydesk slitt]$

There are no errors or warnings. Toward the bottom it mentions that you have a log of the compile at hello.log, and the compiled document (they call it the output) is written to hello.dvi.

If you get errors or warnings, check that you didn't make a typo in hello.txt. If the latex command doesn't run at all, try latex2e, and if that doesn't run, it's possible you need to install your Linux distribution's LaTeX package.

Once you get the document compiled, it's time for your first refinement -- declaring your font size.

Declaring Your Font Size

We next add the font size  (12pt) in square brackets before the braces.

\documentclass[12pt]{book}
\begin{document}
Hello, world!
\end{document}

Once again, compile.
rm hello.dvi
latex hello.tex
xdvi hello.dvi
The result is the same document, except with a slightly bigger font. The book document class recognizes only three main fonts, 10pt, 11pt and 12pt. There are other document classes with wider font varieties, but in fact the vast majority of books look best in one of these three fonts.

Looking at the first line, we see the size argument in square brackets and the document class (book) in braces. Required arguments go in braces, while optional arguments go in square brackets. This is true throughout LaTeX, so you should get used to it.

Document Class Optional Arguments

The \documentclass keyword can take many optional arguments. Here's an example:
\documentclass[12pt,oneside,twocolumn,english]{book}
In the preceding, we've declared the main font to be 12 point, we've declared that this is a one sided document and that the print uses two columns, and that the language is English. As you can see, commas separate the optional arguments.

Making a Compile Loop

It was important that you manually created and compiled the proof of concept, but now that you've done so, repeatedly typing in three commands is not time effective. You need a small loop to do the delete, compile and view for you, so that every time you change the LaTeX file you can easily view the results.

This compile loop is a shellscript that will run on any Linux, Unix or BSD machine. If you're using Windows, please create an equivalent batch file.

#!/bin/sh

# ABORT IF NO ARGUMENT GIVEN
latexfile=$1
if test "$latexfile" = ""; then
echo "You must specify a file to compile!"
echo "Aborting."
exit
fi

# CALCULATE DVI FILENAME
fileprefix=${latexfile//.*/}
dvifile=${fileprefix}.dvi
echo
echo Repeatedly compiling $latexfile to $dvifile
echo

# REPEATEDLY COMPILE AND VIEW
echo q to quit, anything else to regenerate $dvifile from $latexfile
read var
until test "$var" = "q"; do
echo; echo; echo; echo;
rm -f $dvifile
latex $latexfile
xdvi $dvifile
echo q to quit, e to edit in LyX, anything else to regenerate Postscript
read var
done

As you start working with LaTeX files needing significant debugging, this script will save time and energy. Once again, however, if you cannot make this script work, you can always drop back to manually deleting, compiling and viewing.

Simple Paragraphs

Create the following paragraphs.tex:

\documentclass[12pt]{book}
\begin{document}
LaTeX is a typesetting language considered by many
to create the
most beautiful and readable

documentation. Authoring with LaTeX makes your
document printable and readable on a huge variety
of paper, paper sizes, operating systems, and
digital formats.

LaTeX is a complex programming language. In my
opinion it's harder to learn than Perl, C++ or C.
If you really want to be effective using LaTeX or
even LyX, you must master LaTeX. This document
endeavors to give you the 10\% of LaTeX knowledge
you'll need for 90\% of your work.
\end{document}

The preceding document contains two paragraphs of content. You'll notice that it has rather arbitrary line breaks. That's OK, because like HTML, LaTeX considers a single linebreak just another space. LaTeX paragraphis are separated by a double linebreak, like you see between the words formats. and LaTeX.

This would also be a good time to point out that certain characters in LaTeX have special meanings. Notice that the percent signs (%) have been escaped with backslashes (\)? In LaTeX, a percent sign comments out the remainder of the line, which is not what we wanted here. So we precede it with a backslash so that the percent sign prints literally.

The following is the output after compilation:

Screenshot of paragraph output

The upper right contains the page number, which is inserted by the book document class. Each paragraph is indented, but the paragraphs are not separated by vertical space. You can change any and all of these things, but you can see that the defaults are nice.

Headings

The essence of a book is organization. The LaTeX book document class offers the following heading hierarchy:

CAUTION

The "Paragraph" and "Subparagraph" headings are NOT paragraphs or parts thereof -- they are headings. In my opinion the naming is unfortunately confusing. However, calling "Paragraphs" "Subsubsubsection" or calling "Subparagraphs" "Subsubsubsubsection", while logically consistent, would be hard to read.

Just remember that the "Paragraph" heading is a heading, while the generic word "paragraph" refers to a block of text with a unified theme.

While we're discussing terminology, it should be mentioned that the entity called a "paragraph style" in most word processors is called an "environment" in LaTeX.

You can change the appearance of any environment, including the heading environments, but by default Part and Chapter begin on a new page, and if the book is 2 sided (printed on both sides), they begin on odd number pages even if that means leaving an entire page blank.

Section, subsection and subsubsection start new lines, and then the text under them begins on the following line.

Paragraph and Subparagraph start new lines, but their associated text follows them.
















Back to Troubleshooters.Com * Back to Linux Library * Litt's LyX Library