LaTeX:Basics

LaTeX
About - Getting Started - Diagrams - Symbols - Downloads - Basics - Math - Examples - Pictures - Layout - Commands - Packages - Help

This article will describe the basics of LaTeX including how to create your first file and how to work with formatting, comments, and packages.

First LaTeX Document

To create your first LaTeX document, start by opening a new file in TeXnicCenter. All done?Start TeXnicCenter if you haven't already done so. To open a new file in TeXnicCenter, click File in the upper left, then click New. A blank document should open. Type the following lines (or copy and paste them):

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

Note that it is crimped brackets surrounding the words "article" and "document" in the above example, not parentheses. As you will see, crimped brackets(braces) are used for mathematical signs like \frac{x}{y} to make the fraction $\frac{x}{y}$. You will use this in countless ways when typing LaTeX. These braces indicate mandatory arguments that must be included with the command. Brackets ([ ]) are optional arguments, that do not have to be written with the command. For example, \sqrt[n]{x} produces $\sqrt[n]{x}$. The [n] does not have to be included if no root is needed, so you can just use \sqrt{x} to produce $\sqrt{x}$. However, the {x} must always be included.

Save the document (press Ctrl-S or click File, then Save) as 'Hello' (don't include the quote marks in the name) in a folder of your choice.

The file will appear in your folder as 'Hello.tex.' We will call this the source file. In order to create a finished LaTeX document, we need to compile the source file. First, make sure that the bar near the top of the TeXnicCenter window reads "LaTeX => PDF"; if it doesn't, change it so that it does. Then to compile the file, do any one of the following:

  • Select "Build" -> "Current File" -> "Build" from the top menu bar.
  • Click on the "Build Current File" icon (it's the down-pointing arrow over the stack of papers, just to the right of the "LaTeX => PDF" bar).
  • Press Ctrl-F7.

You'll see some system messages scroll in the "Output" panel at the bottom of the window, and then eventually you should (hopefully) see the line "0 Error(s), 0 Warning(s), 0 Bad Box(es), 1 Page(s)". This means that your document (a PDF file) is ready. To view your document, do any one of the following:

  • Select "Build -> "View Output" from the top menu bar.
  • Click on the "View Output" icons (it's the magnifying glass over the sheet of paper, just to the right of the "Build Current File" icon from above).
  • Press F5.

This will open up Adobe Reader and show your finished document. You can print the document from Adobe Reader if you like.

For GNU/Linux

Open any Tex editor and save the text below into a file hello.tex,

\documentclass{article}
\begin{document}
Hello, world!
\end{document} 
  1. Next open terminal and change directory to where the file is saved(for example cd ~/playground if it is in the playground folder).
  2. Next, pdflatex hello.tex and if you see no errors ("0 Error(s), 0 Warning(s), 0 Bad Box(es), 1 Page(s)"), you have successfully done it.
  3. A hello.pdf file was created in the directory, open it with a pdf-reader such as evince or xpdf.

Style

You'll develop your own style of using LaTeX. The sections below outline some tips that you'll find helpful.

Space

In LaTeX, you can use as much space as you like--LaTeX won't see it when it compiles. If you put in three spaces, it will only see one. If you put in 6 empty lines, it's the same as one. Therefore, this:

\documentclass{article}
\begin{document}
This is a sample document.

It illustrates how \LaTeX\ treats space.
\end{document} 

results in the same as this:

\documentclass{article}
\begin{document}
This        is     a        sample document.




It illustrates how \LaTeX\ treats space.
\end{document} 

Generally, write so that it's easy for you to read the source code. Don't pour in tons of space, but leave enough that you can easily find parts of the source file later.

TeXnicCenter (and most editors you might use for LaTeX) treats line numbering differently than your typical text editor. If you just type and type text without ever hitting <Enter> so that your text scrolls on to the next line, TeXnicCenter sees all that text on the next line as just a continuation of the previous line. This is no big deal if you never make a mistake, but when an error occurs when you compile, you usually have to find the mistake in your source file by line number. If you have a 4 or 5 line 'line', finding the error can be a real headache. (You can see what line you are on in TeXnicCenter by looking at the bottom towards the right.)

Just as LaTeX doesn't see tons of extra space, it won't see space that isn't there. One primary example of this is that LaTeX will only see linebreaks if there is an empty line or if you use the \newline command. Hence, this:

\documentclass{article}
\begin{document}
This is a sample document. It illustrates how \LaTeX\ treats space.
\end{document} 

will produce the same result as:

\documentclass{article}
\begin{document}
This is a sample document.
It illustrates how \LaTeX\ treats space.
\end{document} 

Comments

You can leave yourself (or later users of your source files) notes by using %. Anything in a line after % is ignored when the file is compiled. Try this:

$\documentclass{article}
\begin{document}
%You won't see this in the final document.
You do see this.
\end{document} 

You'll see that comments are grayed out in TeXnicCenter. Comments are very useful to guide other readers of your source file, and to guide you in case you'll come back to a file later. You'll find comments throughout our sample files.$

Including Packages

If you have commands that you have created or a specific layout you like to use, you don't have to always copy and paste all that LaTeX code into the top of each LaTeX source file. Instead, you can create your own style files to include in your document. Read about Packages to learn how to create and include files of your own. Beginners should hold off on this until they've become more comfortable with LaTeX.

See Also