LaTeX, Part (5): Writing your first document

We’ve got the warning and very basic background out of the way, so let’s start making a TeX document!

First, open up Texmaker. If you left a shortcut on your desktop during installation, double-click it. If not, go to your Start Menu, click All programs, and then Texmaker. A window will open:

As in the screenshot, there are a bunch of different toolbars. You don’t really need any of them, especially for now. You can leave them if you want, but I would right click on one of them (as I have done) and un-check all those you don’t want. I have labeled everything in red. The only thing you absolutely need is the window that says “The content goes here,” but you can’t get rid of that anyway. I would also keep the “Messages / Log File” section, since that will tell you what errors you have had and where to find them. You’ll learn to love/hate that little log. I have never used the structure portion, though I imagine it’s useful for large documents. The “Math” and “Format” bars give you shortcuts to commands you would normally type. I would get rid of these for the time being, since the whole point of this exercise is for you to learn how to use LaTeX. You can always add the bars back later.

Now that your editor is ready to go, you need to make a new file. I would suggest making a folder on your Desktop and calling it “latex_trial” or something similar. The folder is handy because MiKTeX will generate multiple files, and it’s nice to be able to keep track of all of them. Now use the File -> New menu in Texmake to get a new file.

You’ll notice that th content window now has a white background and a green sidebar on the left. The side bar is where the line numbers go. Every time you hit the “Return” button you will make a new line, and that line will be given the next number.

Before we start writing the document, do a File -> Save As, then choose the folder you created as the destination. In typical computer-nerd fashion, name it “hello_world.”

Now that you are back in the main window, you’ll notice that there is a tab with “hello_world.tex” in it. If you were to open multiple TeX files simultaneously, they would go in different tabs and can be easily switched between. The “.tex” extension tells the operating system that “this file should be opened with whatever program opens .tex files,” in this case Texmaker. The extension also tells MiKTeX what to expect when it does stuff with the file, since MiKTeX deals with several file-types.

Alright, now click in the white space of the content window, and you’ll see the characteristic blinking vertical line indicating that you can start typing.

The three necessities

You’ll need, at the very least, these three things to make a TeX document:

  1. a command that says what kind of document it is (called “documentclass”); and
  2. the beginning of the body environment
  3. the end of the body environment

Commands in LaTeX generally start with a “” (the slash above your enter key, not the lower one). They’ll have this format:

command[options]{target}

To tell MiKTeX what kind of document you want it to make (remember that MiKTeX is the software that reads your input and generates your document), you use the command

documentclass{class},

where the “class” part in curly brackets will be whatever class type you choose. There are several standard options, such as “article,” “report,” “book,” and “letter.” You can also create your own when you get savvy enough, and there are plenty of other floating around on the Internet, though documentation for them (meaning “how to use them”) may be severely lacking.

So what’s the deal with classes? Basically, each class has different rules of formatting, spacing, etc. There are also some commands that are specific to certain classes. For example, book uses “chapters” while article uses “sections.” I’ll just be using the article class for this tutorial, since there is plenty of documentation on the other classes elsewhere if you are interested.

On the first line of your document, then, type documentclass{article}. Now all you’re missing is the “document” environment. This is where all of the content goes, just as the <body> tag in HTML define where the content of a webpage goes.

Environments begin with begin{type} and end with end{type}, where “type” is the specific environment. So let’s add this to the document. You should have something like this:

See the stuff in gray? Those are comments. Comments are used in pretty much every type of computer code. Things in comments will never show up in your final product. In LaTeX, all you have to do to make a comment is use a “%” sign. Everything that follows it on the same line will be commented out.

This brings us to the other thing that might be unfamiliar to you. I told you earlier that the numbers in the green sidebar are line numbers. Why do they leave a blank space every once in a a while?

This has to do with word wrapping, which can be turned off if you want. In a word processor this isn’t an issue, because word processors are designed to make printed material. A text editor, however, is made to manipulate text, including computer code. Since it’s main purpose is not a formatting one, text editors do a few things that make more sense for writing computer code. One of those things is that a “line” of letters, numbers, and symbols extends forever. It doesn’t come back around and start on the next line unless you tell it to. You only tell it to when you hit the “Enter” button. This is called a carriage return, just like it would be in old-school typewriters.

By default, word wrapping is turned on in Texmaker. This means that, instead of forcing you to scroll to the right in order to see everything you wrote, it “wraps” it down so that you can see everything at once. However, even though the wrapped text appears to be on multiple lines, it is still just on one.

Okay, now add some content. In between the begin and end{document} commands, type something like “Hello World!” Finally, a completed tex file! Now all you have to do is tell MiKTeX to build it. Fortunately, Texmaker has the ability to send your file right to MiKTeX, so all you have to do is go to the Tools menu (screenshot to the left) and click PDFLaTeX. Texmaker then sends your document to the PDFLaTeX application (part of MiKTeX), which generates a pdf document. Texmaker also has the handy View PDF option, which will send that newly created pdf to your pdf viewer and open it for you. Click that now. There it is, your first (extremely boring) TeX document!

In the screenshot, notice that I also circled in red the keyboard shortcuts F1, F6, and F7. You never have to go to the mouse to do this, you can simply hit those keys (above the row of numbers at the top of your keyboard) and you’ll get the same result. I made you do it the long way first, but here’s the shortest way: Just hit F1. This is the quick build thing that we assigned earlier to “PDFLaTeX + View PDF.” Remember? So hitting F1 is just like hitting F6 and then F7.

In summary, you have just learned the three commands necessary to build a TeX document. You learned about comments and word wrapping, and about using keyboard shortcuts to send your document to MiKTeX and Acrobat/Foxit for building and viewing.

As you must have guessed, there is a lot more to know. I’ll be continuing this tutorial soon, but in the meantime check out this document by Tobias Oetiker and Contributers. It is protected by the GNU license, and so can be modified and distributed by anyone. It’s called the “Not So Short Introduction to LaTeX.” The document goes from square one, like I did, and then tells you nearly everything you could want to know about making TeX documents. I found it extremely useful while I was learning, and I think you’ll find the same thing. And, as a side note, it was completely written using LaTeX.

<- Back to Part (4): Before you Begin | Continue to Part (6) ->

8 thoughts on “LaTeX, Part (5): Writing your first document

  1. Hello Adam

    Can’t texmaker do inverse checking? I just found this potential problem.

    If it cannot, it’s too bad.

  2. As sad as it makes me to say:

    I have no idea what inverse checking is.

    That said, there is another good editor I have used called TeXnic Center. I stopped using it when I began to use Linux, since TeXmaker is not cross-platform, but I liked it better than TeXmaker. Maybe it has what you’re looking for.

  3. Thank you

    Inverse checking is given a line in a dvi file, Miktex and the Latex editing software pulls out where the original code is.

  4. Hi,
    I installed all the required like as you mentioned above but when i tried to run (I mean quick build) it showing error in log is “Error : could not start the command”…can you please explain what is the problem……

  5. That was great man, im from Chile and i was having a really bad time getting started with all this. Great job!
    Although I have one questión. I downloaded the dictionary in Spanish but, i don’t know how to make it work.
    Any suggestions? (I followed every step you mention)

    1. Sorry, I haven’t fiddled with the dictionaries really at all. But the latest Texmaker has (I think) built-in dictionaries that might be better than Aspell. Worst-case, you could use something like Notepad++, which still has all of the syntax highlighting, and then do spell-checking with the Spanish Aspell there. In NP++, you have to select the part you want to spell-check, go to the Plugins menu and then Spell-checker. Hope that helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s