Software for Research, Part 4: LyX and LaTeX

1 Comment

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:


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: \( E = mc^2    \)
m = \frac{m_0}{\sqrt{1-\frac{v^2}{c^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 presentation, without rewriting a line of text or changing a formatting style. This type of visual representation 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.

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 Zotero for reference management. To install, just download it from Firefox and register for an account on the Zotero site. There is also a plugin called LyZ which allows you to insert citations directly from Zotero. I also find the Zotero-quicklook plugin useful.
You will need to configure LyZ to send references to LyX, this involves setting the correct "lyxpipe" in the LyZ settings. Try:
/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/
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/ $$n $$o $$t

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.

Collaboration with LaTeX
Sharing your work for editing or collaborative editing is essential for any document preparation system. Unfortunately, people have become accustomed to very convoluted ways of achieving this. The usual pathway involves emailing document versions and then aggregating edits. This is the least efficient means of editing a multi-author document. Nevertheless, LyX does support this form of editing using a typical "track changes" mechanism. However, LaTeX documents are essentially plain text so this allows for much more advanced forms of versioning and editing. For example, LyX has inbuilt version management that stores the complete history of a document and allows changes to be merged. This can also be achieved with tools such as FidusWriter

  • ShareLaTeX

  • fiduswriter.jpg
    These tools allow for any user to share a LaTeX document via the web. They do this in somewhat different ways, however.
    ShareLaTeX allows real-time collaboration (multiple editors simultaneously) and supports advanced LaTeX documents including custom styles and templates. However, it is closed source and still requires the users to have some familiarity with LaTeX as code editing is required.
    FidusWriter (currently beta) takes a different approach. It assumes that the document has a central editor who will collate changes and then uses a change tracking mechanism to aggregate changes. It also displays the document using an editor which hides the code but allows output of the document to a TeX file. This works very well despite being in beta and being open source, you can also install FidusWriter on your own web server. At the moment it lacks custom styles but the developers are very reponsive and are working on new features. In the future it looks like it will be an excellent platform for taking a LaTeX file, sharing it for edits and then pulling that file back to a local editor. I know this may not necessarily be the exact way the authors plan for it to be used but seems the most logical for integration with LyX.

    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.


    Francesco Giorlando

    I agree there are some disadvantages to using LyX instead of "pure" LaTeX. The main advantage of LyX is when one is working with very large documents, it's easier to keep cross references and figures managed as well as working with multi-part documents.
    However, I have started using ShareLatex ( more for small documents, real time collaborative editing is great for scientific papers.

    27-08-2014 4:23 am