About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account


D.1 Text Editor and Terminal Approach

Creating a LaTeX document using a text editor and a terminal is an approach often favoured by UNIX-type users. If you have never used a terminal (i.e. you have only ever used point-and-click menu driven applications) then you will be better off using a front-end, in which case I suggest you look at the TeXnicCenter and WinEdt sections instead.

To begin, you will first need a text editor[TeX-friendly editors and shells]. There are a number available that are suited to using with LaTeX, some people advocate Emacs, others advocate vim, and there are various others such as NEdit. I prefer to use vim--I'm not overly keen on using the mouse, and I prefer being able to issue all commands via the keyboard (although there is a GUI version of vim). As with some other editors, it comes with syntax highlighting, regular expression search and replace, auto-insertion, and a brace matching mechanism which I find useful. If you are using version 7 of vim, there is an integrated spell checker, otherwise there is a spell checker plug-in called vimspell, so you can check your spelling as you type. If there is already a text editor that you are comfortable with, then stick with that, otherwise try out available editors, and decide which one you prefer.

When using the terminal and text editor approach, most people usually have at least two terminals open: one to run the editor, the other to run LaTeX. This means that you don't have to keep quitting the editor every time you want to LaTeX your document. Some editors allow you to run commands, but personally I don't like to use this approach. If your editor has a GUI interface, then you'll probably only need one terminal open.

Let's get started: start up your text editor. This is usually done by entering the name of the editor at the command prompt in your terminal. With most editors you can also specify the filename as well. If the file doesn't exist, a new one will be created when you save your document. Figure D.1 shows my terminal. The command prompt looks like [nlct@nlctltpc examples]$. It will be different for your system. I have typed vim sample1.tex at the command prompt. This will start vim with a new file called sample1.tex. (In this section, I will be using vim as the text editor, if you are not using vim, then substitute the editor of your choice.)

Figure D.1: Starting vim from a terminal
 
Image of a terminal

Once I have pressed the return key, my terminal looks like Figure D.2. Normally vim starts in visual command mode, which means that when you start typing text, it will be interpreted as part of a command. In order to type text into your file, you will need to enter input mode. There are a number of ways of doing this, but pressing i will do for nowD.1. Figure D.3 shows how my terminal looks when I am in input mode. I can now go ahead and type in my text (Figure D.4). To go back to the visual command mode, press the escape key (Esc). Now that you are back in the visual command mode, you can save your document, either using the command :w if you have already given your file a name, or :w <filename> (e.g. :w sample1.tex) if you started vim without specifying a file, see Figure D.5. When you want to quit vim you can do :wq to save and quit or :q! to quit without saving, but I suggest you don't do this just yet if you have another terminal available.

Figure D.2: Starting a new file in vim
 
Image of a terminal with vim running

Figure D.3: Input mode in vim
 
Image of a terminal with vim running in input mode

Figure D.4: Creating a sample document in vim
 
Image of a terminal with LaTeX code typed in vim

Figure D.5: Saving your document in vim (the file name should be omitted if the file already has a name)
 
Image of new file being saved in vim

Step 1 is now complete, and you are now ready to move on to Step 2: using LaTeX. Go to your other terminal (or quit your editor if you only have access to one terminal) and make sure that you are in the same directory as the file you just created. Typing ls at the command prompt will list the contents of your current directory. If you do this, you should see the file that you have just created (see Figure D.6). At the command prompt type[Makefiles for LaTeX documents]:

latex sample1.tex
as shown in Figure D.7. You can omit the .tex extension if you like, LaTeX will automatically add this if it has been omitted. If you prefer to use PDFLaTeX, type
pdflatex sample1.tex
instead (again the .tex extension may be omitted.) You should now see something like Figure D.8.

Figure D.6: Listing the contents of the current directory
 
Image of directory listing in a terminal

Figure D.7: Running LaTeX
 
Image of a terminal where user has typed the command
   to run LaTeX

Figure D.8: Running LaTeX
 
Image of a terminal showing normal LaTeX messages

Numbers appearing in square brackets, e.g. [1], indicate which page LaTeX is currently processing. In this case, there is only one page. The last line to appear on screen indicates that information about this LaTeX run has been written to the log file sample1.log, which you can look at using your text editor.

The most important thing to note is the penultimate lineD.2:

Output written on sample1.dvi (1 page, 248 bytes).
This means that the document has been successfully created, and is one page long.

If you have made a mistake in your source code, for example suppose you have missed the starting backslash in \documentclass, then the output will look something like:

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.1 d
     ocumentclass[a4paper]{article}
?
There are several things you can do at this point, but the easiest thing to do is to exit LaTeX by typing X followed by the return key. Go back to your editor, fix the mistake, save the document, and then try again. (If you do get an error message, check §B. Common Errors.) Note that it is important to always save your document before running LaTeX.

You can view the typeset output by loading the file sample1.dvi into a DVI viewer, such as xdvi or kdvi. To do this type

xdvi sample1.dvi
or
kdvi sample1.dvi
at the command prompt (see Figure D.9). You will then see the final output, as shown in Figure D.10.

Figure D.9: Load a DVI file into a DVI viewer
 
Image of a terminal where user has typed command to
  load DVI file into kdvi

Figure D.10: Viewing a DVI file in kdvi
 
Image of DVI file being viewed using kdvi

If you have used PDFLaTeX instead of LaTeX, you should have a file called sample1.pdf instead of sample1.dvi. You can view this using a PDF viewer, such as acroread or kpdf.

Some viewers, such as kdvi and kpdf will automatically reload the file whenever it is modified, in which case you may like to keep the viewer open, and as you keep editing and LaTeXing your document, the viewer will automatically reload the new versions. Some viewers, such as xpdf don't automatically reload, but have a reload facility, which you can use whenever you LaTeX your document.

If you like, you can convert your DVI file to PostScript using dvips. To do this, type the following at the command prompt in your terminal:

dvips -o sample1.ps sample1.dvi
(The .dvi extension may be omitted.) You can then view the PostScript file using ghostscript or one of its associated applications, such as ghostview or GSview. I have kghostview installed on my laptop, so to view the PostScript file, sample1.ps, I would need to type:
kghostview sample1.ps
(See Figure D.11.)

Figure D.11: Loading a PostScript file
 
Image of terminal where user has typed command to
   load PostScript file into kghostview



Footnotes

... nowD.1
For a complete set of available commands, see the vim manual at http://vimdoc.sourceforge.net/
... lineD.2
if you are using PDFLaTeX, it will have sample1.pdf instead of sample1.dvi

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