8 LaTeX Tutorial

This tutorial is intended to introduce a component of the iPoIDE project of statlive.org. It can be used with a current version of Emacs and a TeX distribution, we recommend TeXLive.
  1. View demo document
  2. Writing a paper, step by step
  3. Guides and reference cards

Demo doc

If you do not already have a document with sections and content, start by downloading an example file. You can use this to see the features of the emacs LaTeX modes and to see how to put together a basic document.
  1. In your shell
     wget http://potis.org/tutorial/guide.zip
  2. unzip
    unzip guide.zip
    This produes a directory called 'guide-to-latex'
Open a file ending with the extension ".tex" . If you have your own document, follow allong using your file name instead of the demo file from the guide. Note: avoid capital letters and spaces in file names.
C-x C-f guide/demodoc.tex
The first thing to notice is the syntax highlighting and indentation. As your document gets long, you may appreciate a feature that, to my knowledge, is unique to this system. Press the 'TeX' button at the right of the tool bar. This will shortly replace code sections with the rendered mathematical symbols. This greatly cleans up your document. However, if you use the cursor motion keys (not the mouse) to enter the region of the mathematics, you will find that the section expands, and you once again have direct access to edit the underlying code.

Typing Mathematics

Typing mathematical constructs is made easy by Math mode, which is enabled by default on your DVD.

One of the most useful shortcuts is

` 
(on the top left side of the keyboard, with ~)

Try pressing:

`a 
All the Greek letters, upper and lower case, as well as some other objects, are bound this way.

Open a file (on your computer or on your desk), LaTeX ref card (it is two pages) and emacs. Resize windows so that you can see all necessary windows.

Look at a file containing math and use math mode and the ref card to reproduce the content.
These features are part of AUCTeX.

Step by step paper

The following guides you through a basic article class paper, step by step in your version of emacs. In this step by step guide, you will open a blank document, insert an article environment, write a basic paper in that environment, add a graphic, and add a bibliography.

See the following nice guide on writing essays when it is time to generate real content for your paper: essay guide. For a superb comprehensive tutorial on written English see Dr. Syntax at Grinnell College-- a must for statistical communication or the Purdue Online Writing Lab (OWL). For my Math 103 students, see Laura Taalman's example file: latexsample.tex

  1. Open a new document:
    C-x C-f
    Call your new document josh.tex (or hon.tex, or matt.tex, you get the idea. Note: avoid capital letters and spaces in file names. )
  2. Insert an environment:
    C-c C-e
    Accept the defaults by pressing return in the minibuffer as you are prompted. Place your cursor between
    \begin{document}
    and
    \end{document}
    and start writing your paper. Follow demodoc.tex to see how to do basic things. This file and many other help files are on your dvd. In the Newton lab, demodoc.tex is in
    d:/texmf-doc/doc/english/guide-to-latex/
    The version on the DVD includes \usepackage{palatino}. This is a font that is not available in TeX Live, so unless you have installed it, you need to comment this line out if you want to typeset the example document. You can comment regions out by selecting the region and commenting it with
    M-x comment-region 
    or by placing the comment character
     %
    at the start of the line you want to comment out.
  3. Typing Math:
    See above.
  4. Including Graphics:
    You will need the graphicx package, so place the line
    \usepackage{graphicx}
    after the \documentclass line and before the \begin{document} line. This region is called the 'preamble' and is where you place instruction to the TeX system regarding how to process your document. For more information on the graphicx package, see section 6.1.1 of this more detailed guide for writing papers: More on including graphics. In general, use Emacs to help you. For graphics,
    C-c C-e figure
    This will insert a figure environment. Inside the figure, place
    \includegraphics{yourfile}
    (.png, .jpg, .pdf is not necessary)

    In R, you have a number of choices for how to export graphics. PDF works well, since the document you produce using these instructions will be a PDF file.

  5. Including R output: In R,
    library(xtable)
    	  help(xtable)
    	
    for exporting R output in LaTeX code. This also lets you export tables. To make a table by hand, again, insert environment:
    C-c C-e table
    Type
    C-c C-e
    and then press TAB twice to see available environments.
  6. Insert Sections/subsections/subsubsections:
    C-c C-s
    then choose the level of nesting for your section break.
  7. Typeset your paper:
    To typeset your paper, first make sure you are in Pdf mode. Go to the Command menu, select Texing Options, and be sure there is as checkmark next to PDF mode. You can do this from the keyboard with
    	  C-c C-t C-p
    	
    Observe the minibuffer and the status line where you will see Tex-Pdf mode enabled and PDFLaTeX in the status bar above the minibuffer. Toggle as needed. If you use a png (graphs) or jpeg (photographs), you may need to set the dimensions, as indicated in the above link.
  8. Bibliography.
    This is often the hardest part of writing a paper, and is one place where good organization pays great dividends in the future.
    1. Create a new document which will hold your bib entries:
       C-x C-f stat.bib
      Choose a good name. It must end in .bib. Note: avoid capital letters and spaces in file names. If you plan well, this single file is where you will keep all your bibliographic entries: for your dissertation, for your presentations, your journal articles, for everything you write from now on. You will want to place this file in version control.
    2. Add an entry:
      We will start with a complicated. You can click on the Entry-Types menu to see the available types. By far the most common will be Article in Journal. But we will start with a citation to this tutorial.
      C-c C-e M
      (C-c C-e for bib tex entry is a common a shortcut you will use often enough that you should learn it.) You are placed in a correct template containing all the parts your entry might have. In this case they are all optional, thus in red. C-j to move down to the Author field. Enter
      Panayotis Giannakouros and Lihua Chen
      C-j to each of the next fields entering the title
      Guide to UT Newton Lab DVD
      January
      2007
      and the url for how published:
      http://potis.org/tutorial
      Avoid citing web pages, if you can.
    3. Clean up the entry:
      C-c C-c
      Notice that the unused fields were deleted. Also, you are prompted for a key, in this case a rather long key composed of the author and title. If entering references by hand, you might want a shorter key, but you will probably want to use RefTeX, in which case citations will be automatic.
    4. Add the bibliography to your document:
      Add the lines \bibliography{stat} and \bibliographystyle{plain} just before the \end{document} tag.
    5. Cite references in your paper using RefTeX:
      Be sure you are in RefTeX mode.
      M-x reftex-mode
    6. Enter a citation to the bib entry we added:
      Move your cursor to a place in your document, after some text. Then issue this command:
      C-c [ 
      You will be prompted for a 'regular expression matching a set of entries in the bibliographic data base.' This means, type a few letters of the name of the author you want to cite. In our case type
      gia
      You see our entry pop up in your lower screen. This is the only match, so just press return. You now see a proper \cite{} tag for the reference. (Did you see 'no bibilography' instead of the prompt for a regular expression? Use C-x C-w to make sure the bib and the LaTeX file are in the same directory (I recommend "~/"). If it still fails, you used capital letters in your file name and are using Windows.)
      See the RefTeX manual for many more great things you can do with RefTeX and a list of bibliography styles for more on formatting.
  9. Typeset your document:
    1. Make sure the document is saved, and then go to Command->latex or press the button of a lion with PDF in red under it. You will see a message that says "there were unresolved citations."
    2. Run bibtex on your document:
      Go to Command->bibtex or click on the book button in the tool bar. If all went well, the output in the minnibuffer instructs you to run LaTeX agin to get the citations right.
    3. Run LaTeX again:
      Same as previous step, Run it twice and it will say
      LaTeX: successfully formatted {2} pages.
    4. View your output:
      If you have a new version of the DVD, you can go to the Command->view menu. Otherwise, find your pdf output file in Windows and double click on it.
      See the wikipedia page on BibTeX is good for more on BibTeX, such as name conventions and links to examples of the appearance of bibliography styles other than "plain."

    Step by step presentation

    Same as above but instead when prompted for document type, enter
    beamer
    rather than document. A beamer presentation is made up of frames.
    C-c C-e frame
    When you add constructs like lists, you will be given an extra prompt,
     overlay
    This controls when something will appear on the slide. Try it. For more complicated effects and setting background themes,
     wget http://statlive.org/~poti/math220/beamerexample.zip
    unzip beamerexample.zip

    Guides and Reference Cards

    Link to Ref card

    For a full, searchable, reference containing any construct you might want to insert see:
    Comprehensive list of symbols

    Once you become accustomed to using LaTeX for basic things, you will soon want to see how to do things in LaTeX, not just how to make a particular symbol. See the Visual FAQ for this.


    Last modified: Wed Jan 24 13:46:55 EST 2007