Archive for the ‘Tutorial’ Category

TeX for the Layman

Tuesday, September 20th, 2005

TeX is a typesetting program originally written by Donald Knuth in the late 70’s and early 80’s. It’s become a very popular system among computer scientists, mathematicians and physicists because of its syntax for typesetting equations. TeX is a mark-up language which means that you insert tags which tell the TeX program what you want it to do. TeX text files are then compiled into a binary file, usually a DVI, which can be easily converted into html, pdf, postscript, txt and even images; indeed, the Wikipedia uses TeX to generate png’s of math equations, just look at their Calculus page.

Get TeX/LaTeX:
If you’re running Windows you need to get a distribution of TeX called Miktex, which is based off of the LaTeX project. LaTeX is a continuation of the TeX project, spearheaded by Leslie Lamport. Miktex has excellent documentation and a graphical installer. If you’re running a UNIX-like operating system, you need to get a distribution of TeX like teTeX. If you run OS X, you can either use teTeX or TeX on Mac OS X.

Write the code:
Before we get to typesetting, let’s talk about your editing environment. Word Processors such as Microsoft Word, and OpenOffice’s Writer are not optimal for this type of editing. First of all, they are WYSIWYG (What You See Is What You Get) editors which means that they’re going to typeset the text for you. Now, this doesn’t mean you couldn’t work on this project in one; indeed, you can save your work as plain text files and everything should work just fine. But an unobtrusive text editor such as gVIM, GNU EMACS, XEMACS, jEDIT, or Notepad is optimal. You won’t be tempted to center and bold things, you most likely won’t fit as many characters (letters) into one line, and word processors in my experience are quite a bit less stable than basic test editors. I think Allin Cottrell makes a much more persuasive argument than I ever could.

So fire up your editor of choice (in my case gVIM), and create a new file which we’ll call ‘foo.tex’. Now that you have a blank text file open, we’ll go ahead and make a header which will make it rather obvious what the file is if we forget four years from now. Oh, the ‘%’ sign that I have in the header means that both it and whatever comes after on that line is a comment, which means that TeX will ignore it when we finally take our TeX source file and compile it into a finished document.

Next we need to declare a few things:

We set the documentclass to Report, which dictates the general form that our document will take, and then we tell TeX we want to print onto letter paper (normal printer paper). Next we tell TeX which packages we want to use. We’re using the times package, which lets us use the Times scalable font (you can also choose from palatino (\usepackage{palatino}, helvetica (\usepackage{helvet}), and new century (\usepackage{newcent}). I’ll get to this later, but I may as well point out now that if you use the ‘pdflatex’ command, the Time scalable font will be used by default. A scalable font is a font which is described by equations, which can vary their size depending on what you request. Bitmap fonts are static images which get blown up or shrunk down depending on the size you need. Think of trying to print an 8×10 photograph on a poster. It’s going to be ugly.

Next we set the book’s title and the author’s name. These will show up on the cover page.

So \begin{document} tells TeX that we’re done tweaking settings and the actual document is beginning. First we run \pagestyle{headings} so that we get the Chapter #: ChapterName in the heading of every page in a given chapter. Then since most first part of the document is going to be a title page, we run \maketitle and then because we don’t want the page number to show up we run \thispagestyle{empty}. Next we want a table of contents so we run \tableofcontents, and again we don’t want to print the page number, so we put in another \thispagestyle{empty}. Finally we’re ready to start our first chapter. In this case I’m calling the chapter, “Those Crazy Aphids”, which will display at the beginning of this chapter and at the top of the page throughout it. Finally we run \setcounter{page}{1}, which sets this to our first page.

We’re finally able to type our story! The only thing to note at this point is that to break the paragraphs, you need a double carriage return (have a space between paragraphs). If you want to end a paragraph and go to the next line without having any indentation, you need to put a “\\” at the end of the line.

Is what I’d recommend at this point, though, is that you store each chapter in its own file, and use the \input{file} command like so:

And you also need to end the file with a:
\end{document}

Putting it all together:


Download:
chapter1.tex
chapter2.tex

Generate the document:
Good job! Pretty powerful, yet simple, eh? Well, let’s go ahead and generate the final document. TeX itself uses the dvi format, which stands for DeVice Independent. Remember how I said Prof. Knuth wrote TeX way back in the late 70’s and early 80’s? Well, back then most vendors sold incompatible computer systems and Prof. Knuth, genius that he is, wrote this format in such a way that it didn’t matter. We can also generate pdf’s very easily.

At your prompt issue a command to generate that type of document:

  • dvi: latex foo.tex
  • pdf: pdflatex foo.tex
  • postscript: dvips -Ppdf foo.dvi
  • html: latex2html foo.tex
  • View: dvi | pdf | ps | html

note: for html you need to download latex2html from here.

Erudition:
If you’ve enjoyed this and would like to learn more, there are some very good resources on the web:

Having trouble? See a problem with my tutorial? Well, that’s what comments are for!