Software for Research, Part 4: LyX and LaTeX

One of the perennial jobs for researchers, is producing production quality documents. This task often becomes more complicated than it should be due to the tools applied to it. Just as applying a blunt chisel to a good piece of wood will produce an amateurish result, using a blunt program will produce a substandard document (or require far more effort from the artisan than having used the right tool to begin with!).

So now let me introduce the sharpest document production tool:

Contents

What LaTeX does
You'll notice that the rendering of this text looks weird, that's because it's been handed off to the LaTeX processor running on this website, it uses a type of markup code (similar to HTML), so that the following are produced by this code:

E = mc^2:
m = \frac{m_0}{\sqrt{1-\frac{v^2}{c^2}}}:

This is obviously a very flexible and powerful way to produce typographically correct formulae, but that's just the beginning of LaTeX (pronounced lay-tech, as the X is a chi). LaTeX is a typesetting language, it used widely used to produce manuscripts, documents and even whole books. It is also widely supported by tools and interpreters that make working with LaTeX easier, in fact you can work with LaTeX without even learning any syntax.

This is where the program called LyX comes into the picture. LyX is a cross-platform program designed to produce documents utilising LaTeX. It allows for precise document specification and excellent document layout. However, of more importance to the researcher, it separates the tasks of content creation from layout. Therefore, the same document can be used to produce a typeset PDF or a cross-linked web site, or even a presenatation, without rewriting a line of text or changing a formatting style. This type of visual represenation is called WYSIWYM (What You See is What You Mean) rather that WYSIWYG (What You See is What You Get).

The image above shows a document in LyX and a PDF render of that document (viewed in Skim) side by side. As you can see, the formatting has been applied to the text and image in LyX. Essentially the document is defined with different 'styles', for example "Heading" or "Section", which the 'layout' file then interprets to produce the output, this layout file includes information regarding fonts, page sizes and margins, how to treat hyphenation etc. This allows for very powerful formatting and journals can publish their own layout files to to permit easy formatting.

Installation
LyX can be downloaded from the web for free. Different version are available , for OS X I recommend the Carbon version. You will also need to install the TeX engine which does the work of producing the document. The MacTex package is the easiest way to get this running, be aware however that it is a large download (2GB+).

Once you have installed it is useful to integrate the software with a PDF viewer and reference software. This requires a little bit of "glue" between the programs.
I hightly recommend the Firefox plugin /Users/your_username/Library/Application Support/LyX-2.0/.lyxpipe

Skim is an excellent PDF viewing and annotating program that integrates well with LyX. To set it up, just download and then go to
 Preferences>>Sync>>PDF-TeX sync support and enter >>Preset>>Custom: Command: /Applications/LyX.app/Contents/MacOS/lyxeditor Arguments: "%file" %line; /usr/bin/osascript -e 'tell application "LyX" to activate' 

LyX also needs to know how to speak with Skim, which is accomplished by the following setting in LyX Preferences
 >>Output, PDF command: /Applications/Skim.app/Contents/SharedSupport/displayline $$n$$o t 

Workflow
The workflow of using LyX is a little different to a traditional word processor. The first thing to grasp is that your document will look very bare on the screen until you render it to PDF (the two little eyes icon). I tend to keep my LyX occupying half the screen and Skim showing the PDF on the right. Then you can update your render (Skim will ask you if you want to update the document, click "Auto") and see changes quickly.
Navigating between the two views can be accomplished using the following methods:

Skim (PDF) to LyX: known as reverse search, press Command+Shift and click mouse, you will jump to the same spot in the LyX document

LyX to Skim: know as forward search, use the menu item Navigate>>Forward Search

A few key things to understand are that the LyX document is a 'live' document, it can reference and refer to other documents. For instance, if you have an image that you are working on, you can link it to the LyX document and then changes in the image will automatically be pulled in when you render to PDF. This extends much further, however, for instance you can have styles applied and even do stats straight from within a document using the knitr library.

Tips for Thesis writing
The following is a bit of a list of tips that can help in writing a large document with LyX, I will add others and more detail as I find out more!

• Use a Master Document and start with a nice style like Prof Miede's wonderful classicthesis style.
• Use cross references.
• Explore the knitr library for statistics.
• Use the Outline tool in LyX.
• Getting tables to fit page widths can be a little tricky, use the >>Insert>>Tex Code menu item and add \resizebox{\textwidth}{!}{ before your table and } after.
• Use the notes, change tracking and versioning features.