1Obtaining LyX and LTEX - Economicsresearch.economics.unsw.edu.au/scho/teaching/Latex and lyx...

11
A Guide to LyX and L A T E X Ashley Cheng A Guide to LyX and L A T E X Ashley Cheng Based off “A Quick Guide to LyX” by Jessica Moses 08 October 2011 Many economists (and academics in mathematics-heavy disciplines) use a program called L A T E X to create documents. In fact, this is the program that creates most of your exam papers. The beauty of L A T E X lies in its ability to create professional looking documents and display text and mathematics together in a clean and presentable way. The drawback to L A T E X is that there is a fairly steep learning curve. However, once you get the hang of the basics, you will find that L A T E X is much more powerful than Microsoft Word or any other document creation package. The end result also looks pretty cool. There is a program with a gentler learning curve but, in my opinion, it doesn’t offer the same flexibility as L A T E X. This is LyX. LyX makes the L A T E X environment more ’user-friendly’ by taking away most of the coding. However, the cost of doing that is reduced flexibility in how your document looks. Lyx is a good place to start but I highly recommend using L A T E X for the long term. The purpose of this document is to give you a short introduction to L A T E X and an associated program, LyX. I am deeply indebted to my colleague, Jessica Moses, who is responsible for the majority of the material on LyX included in this document. 1 Obtaining LyX and L A T E X 1.1 Obtaining LyX LyX is available as freeware. 1. Go to http://www.LyX.org/Download 2. Pick the relevant version for your computer (Mac/Windows) and download 3. Install 1.2 Obtaining L A T E X L A T E X is available as freeware. There are a number of program which help you code in L A T E X. I recommend using the Protext package. 1. Go to http://www.latex-project.org/ftp.html 2. Pick the relevant version for your computer (Mac/Windows) and download 3. Install 1 of 11

Transcript of 1Obtaining LyX and LTEX - Economicsresearch.economics.unsw.edu.au/scho/teaching/Latex and lyx...

A Guide to LyX and LATEX Ashley Cheng

A Guide to LyX and LATEXAshley Cheng

Based off “A Quick Guide to LyX” by Jessica Moses08 October 2011

Many economists (and academics in mathematics-heavy disciplines) use a program calledLATEX to create documents. In fact, this is the program that creates most of your exampapers. The beauty of LATEX lies in its ability to create professional looking documentsand display text and mathematics together in a clean and presentable way.

The drawback to LATEX is that there is a fairly steep learning curve. However, once youget the hang of the basics, you will find that LATEX is much more powerful than MicrosoftWord or any other document creation package. The end result also looks pretty cool.

There is a program with a gentler learning curve but, in my opinion, it doesn’t offerthe same flexibility as LATEX. This is LyX. LyX makes the LATEX environment more’user-friendly’ by taking away most of the coding. However, the cost of doing that isreduced flexibility in how your document looks. Lyx is a good place to start but I highlyrecommend using LATEX for the long term.

The purpose of this document is to give you a short introduction to LATEX and an associatedprogram, LyX. I am deeply indebted to my colleague, Jessica Moses, who is responsiblefor the majority of the material on LyX included in this document.

1 Obtaining LyX and LATEX

1.1 Obtaining LyX

LyX is available as freeware.

1. Go to http://www.LyX.org/Download

2. Pick the relevant version for your computer (Mac/Windows) and download

3. Install

1.2 Obtaining LATEX

LATEX is available as freeware. There are a number of program which help you code inLATEX. I recommend using the Protext package.

1. Go to http://www.latex-project.org/ftp.html

2. Pick the relevant version for your computer (Mac/Windows) and download

3. Install

1 of 11

A Guide to LyX and LATEX Ashley Cheng

2 Using LyX

This section is the work of my good friend and colleague, Jessica Moses. The credit forthis work goes to Jess. I accept responsibility for any mistakes.

2.1 First Steps

When you first open the program, you should see a screen which looks like this.

In order to start a new file, either press the button indicated, or file → new. You shouldget a plain pink screen.

Normal typing can be done as usual - just type straight in to the program. One thingto note is that you cannot press enter more than once. The program is smart, and willautomatically start a new (indented) paragraph when you press enter. If you want morespace, hold down the control button when you press enter - a little red arrow should comeup. This indicates a new line, and you can use it as many times as necessary.

2 of 11

A Guide to LyX and LATEX Ashley Cheng

2.2 Writing Maths Using LyX

There are two ways of writing maths using LyX. The first is an inline formula - this isincluded in the body of your text, like so: 4x2 + 7 would be one example. To do this, useinsert → maths → inline formula. Numbered formulae are also used fairly often.

If you want to show your working and make sure everything is well aligned, I suggest usingEquation Array Environments, from the same menu. This ends up looking like this:

3x2 = 12

x2 =12

3= 4

∴ x =√

4

= 2

Note how everything is now nicely aligned, without having to play around. To make anew “line” of equations in the equation array environment, simply hit Ctrl + Enter.

The most important thing to remember is to always be typing inside the blue boxeswhen you write maths. Otherwise, none of the commands or mathematical functions willwork. Another point is that you cannot press space or enter while in one of these mathsboxes - it will do all the formatting for you. The only time you should press space is whenyou are trying to get out of a subscript - I will get to that later.

Most of the maths tools you will need are located in the bottom bar, as shown in thescreenshot below. I will walk you through the ones you will use more often. I suggest thatyou press the “pi” in the bottom right hand corner - this will activate a range of otherfunctions which you will be likely to use.

3 of 11

A Guide to LyX and LATEX Ashley Cheng

These cover most of the functions you will use. There are commands for ones you use moreoften; of particular note is using ˆ to get a superscript (to the power of something) andto get a subscript - again, these commands will only work in the blue boxes. To get out ofthe superscript or subscript, just press the space bar. If you wish to learn the commandsfor other things, such as Greek letters, most of them will pop up if you mouse over thebuttons down the button. Just type \(command). Most of them are pretty intuitive - forexample, the command for alpha is \alpha, fractions are \frac, and partial derivatives are\partial.

2.3 Previewing and Saving your work

Your actual output will not look like what you are typing in LyX. The program you typein is WYSIWYM - what you see is what you mean, not what you actaully get. So don’tfreak out if things look kind of weird! To preview your work, you can use the buttons here:

These will allow you to preview your work as a PDF file. Use the one on the left the firsttime you preview a document - after that, you can use the one on the right, which justrefreshes the previous preview.

LyX automatically saves your output as an LyX file, which allows you to go back andedit your work. To save as a PDF, simply click file → export → pdflatex.

And you’re done! Feel free to experiment, the program is wonderful and will get easier withtime. If you want to learn more or have further questions, LyX has great documentation.Check out the tutorial and the help menu, both are very useful.

Good luck!

4 of 11

A Guide to LyX and LATEX Ashley Cheng

3 Using LATEX

Unlike LyX, which is designed as a Graphic User Interface (GUI) environment, LATEXis designed to work by Code alone. As such, it may seem a little daunting at first(especially when you are unfamiliar with code-based programs). However, there is awealth of information about LATEX available on the internet. If you have a problem, youcan simply do a Google search of “latex {short description of your problem}” and you willalmost always find a solution. I also recommend the Wikibook on LATEX. That resourcehas helped me with many a problem.

For the rest of this LATEX tutorial, I shall assume that you are using the Protext systemand TEXworks.

3.1 First Steps

Starting a new document in TEXworks is easy. When you start the program, you shouldbe given a new document to work with (as in the picture below). Alternatively, you canuse the File → New or Ctrl+N.

Once you have got a new document, you now need to setup the document environmentfor LATEX to work in. Usually, this is done by a Style File. I have attached a simple StyleFile to this document. It has most of the things you need to get started. The basic Stylefile contains the following necessary items:

− The Document Class (in this case, an article)− Commands telling TEXworks to use certain additional packages− Some new Commands I found useful− Commands defining the Header and Footer− Page Numbering− Commands about the Bibliography

To begin with, you should not need to change anything (except perhaps the Headers andFooters). When you are more familiar with LATEX, you can start customising the StyleFile for your own needs.

To tell TEXworks to use your Style file, you will need to type the command: \include{Style}.

5 of 11

A Guide to LyX and LATEX Ashley Cheng

Now you can begin your document. You need to tell TEXworks to do so; you will alsoneed to tell it where to end the document. To do this, use the Commands:

\begin{document}

YOUR DOCUMENT GOES IN HERE

\end{document}

Now you are ready to begin constructing your document.

3.2 Writing Text in LATEX

When you want to start writing text, you can simply input it directly into the code. Thereis NO spell-checker so I recommend that you check the spelling using MS Word or someother spell-checker. LATEX will automatically wrap the lines for you so don’t worry aboutthat. To tell LATEX to start a new line, simply use the \\. A example of this is in thepicture below. LATEX will not allow you to use the \\ more than twice in a row.

Whilst this part of LATEX may seem frustrating, in fact, it is LATEX’s greatest asset. BecauseLATEX does not follow your spacing in the code, you can use your code to space out yourworking. An example of this is included in the picture below.

LATEX also has a few quirks.Firstly, a few character and symbols have specific purposes inLATEX. For example:

− \tells LATEX that a command is being used. To get LATEX to display this, you haveto use the command: \textbackslash

− { and } are also used by LATEXin executing commands. To get these to display, youneed to use: \{ and \} respectively

− Other special symbols include: “# $ % ˆ & { } ˜” which are displayed using \#\$ \% \textasciicircum{} \& \ \{̃} respectively.

Secondly, LATEXhas specific environments for different tasks. I will talk about thecommonly used ones in turn. Thirdly, LATEX is quite picky about how commands aredisplayed. You need to match up brackets/parentheses/braces and commands are case-sensitive. However, don’t let this get you down. LATEX has many benefits which outweightthese costs once you get used to the language.

You may also want to use the Line Numbers function in LATEX which is found using:Format → Line Numbers.

3.3 LATEX’s maths Environment

The main purpose of Donald Knuth in creating LATEX was to create a mark-up andprogramming language which could display mathematics in a attractive and precise fashion

6 of 11

A Guide to LyX and LATEX Ashley Cheng

that other programs could not. As a result, LATEX’s maths environment is extremelypowerful.

To start, we need to engage the maths environment. There are two types of mathsenvironments:

− The In-Line environment; and− The Displayed Maths environment (DME)

The In-Line maths enviroment allows you to display maths next to texts and in the sameline. To engage the In-line environment, use the $ key. Then type the mathematics andend with another $.

There are a number of ways to engage the DME:

− Using the $$ (Maths) $$ will allow you to type 1 equations in the DME.− Using \begin{equation} will allow you to type equations where the equations are

numbered. To end the environment, use \end{equation}.− Using \begin{align} will allow you to type multiple equations with equation numbers

and will allow you to align the equations with the & symbol. To end the environment,use \end{align}.

− Using \begin{equation*} and \begin{align*} will allow you to use the Equation andAlign environments without having the equation numbers. Ending this environmentis exactly like before. Use To end the environment, use \end{equation*} and To endthe environment, use \end{align*}.

− The Equation Array environment is also available and works in exactly the samemanner as described by Jess above. To engage Equation Array, use \begin{eqnarray}and end with \end{eqnarray}. You can also use eqnarray* if you do not want theequation numbers.

Once you have engaged the maths environment, you can begin typing the mathematics.The picture below shows how the In-line environment, the Equation, and the Alignenvironment work.

The Wikibook has a great section of Mathematics and Advanced Mathematics. Irecommend you look at this. For convenience, I will explain a couple of the commonmaths commands.

− Greek letter are employed using \letter name. For example: \alpha creates α. Forcapital letters, capitalise the first letter, like so: \Delta creates ∆. Only certainletters have Capital equivalents.

− Superscripts (including powers) are created by using: baseˆsuperscript. Forexample: xˆ2 creates x2

− Subscripts are created using: base subscript. For example x 1 creates x1− Fractions are created using: \frac{X}{Y} where the X is the numerator and Y is

the denominator. For example \frac{1}{2} creates 12 . Note that the numerator

and denominator can be complex function as illustrated in the picture below.Furthermore, fractions can be nested.

7 of 11

A Guide to LyX and LATEX Ashley Cheng

Another helpful hint is to use line spacing and parentheses (i.e. { and }) to help youorganise your maths.

Given some practice, you will find the LATEX maths environment very useful and powerful.

3.4 LATEX’s other environments

3.4.1 List Environments

LATEX has three list environments:

− itemize (note the American spelling)− enumerate− description

Itemize creates bullet points.Enumerate creates a numbered listDescription allows you to write your own list headings.

Example of Itemize:

\begin{itemize}

\item Item 1

\item Item 2

\item Item 3....

\end{itemize}

This will produce:

− Item 1

− Item 2

− Item 3....

8 of 11

A Guide to LyX and LATEX Ashley Cheng

Example of Description:

\begin{description}

\item[Label1] Item 1

\item[Label2] Item 2

\item[Label3] Item 3....

\end{description}

This will produce:

Label1 Item 1

Label2 Item 2

Label3 Item 3....

In my Style File, I have defined 3 new list structures:

− items− enumerate*− description*

These are exactly the same as their namesakes but are more compact. Try them out.

3.4.2 Text Formatting

You can format the way your text looks. For example:

− Using \textbf{your text here} makes the text inside the parantheses bold (e.g. yourtext here)

− Using \underline{your text here} underlines the text inside the parentheses (e.g.your text here)

− Using \emph{your text here} will make the text inside the paranthesese italicised(e.g. your text here)

− You can also next these commands. For example \textbf{\underline{your text here}} will make the text both bold and underlined (i.e. your text here)

Coloured text is obtained by using \color{declared Colour}{Your Text Here.}. Forexample, \color{red}{Your Text Here.} creates Your Text Here. A list of colours is locatedhere.

3.4.3 Graphics

Inserting Graphics into your document can help when displaying graphs or other pictures.To include a graphic, use the command \includegraphics{./picture}.

It is important that you picture is in one of the accepted formats. Usually, this is .JPG or.PNG. However, if you have other formats, you can try looking up how to include them.

Note, the name of your picture cannot have space AND your picture must be locatedin the same folder as your LATEX file. More information is here.

9 of 11

A Guide to LyX and LATEX Ashley Cheng

3.4.4 Document Structures

Sometime it is helpful to readers if you structure your text in a nice way. To help you,LATEX has some tools to organise your work. These are the \section type commands.

− \section{Section Heading Here} will start a new section and make the section headinglarger than the normal font.

− \subsection{Subsection Heading Here} will start a new subsection and make thesubsection heading larger than the normal font.

− Usually, sections and subsections are numbered in consecutive order in the followingfashion: Section 1.1, Section 1.2, Subsection 1.2.1, Subsection 1.2.2, etc...

− You can go all the way to subsubsections.− For Book and Report document classes, you can also have Chapters.

3.5 Compiling and Troubleshooting

Now that you’ve created your document, you will want to publish it as a PDF. To do this,you need to compile the document. To do this, you need to click on the Green “Play”button located in the top left. Make sure that the box next to the “Play” button has“pdfLaTeX+MakeIndex+BibTeX” selected. This is shown below:

You should see a whole section of text displayed in the Console window as LATEX compilesyour work. This is normal. Once the document finishes compiling, it will display a previewwindow for you. The finished PDF file will be located in the same folder as your LATEXcode.

If your code has some errors, the compiler will not work. It will stop and then tellyou where the error is. You will need to go and troubleshoot this. In the picture below, Ishow what happens when you have an error. In this case, the error is my forgetting to endmy align environment. To fix this, I first stop the compiler using the Red “Stop” button.Then, I fix the code and press “Play” again.

Notice how the error message tells me I have a “runaway argument” at Line 37. It meansthat there is an environment which has not been properly ended. The error is occuringin the section prior to Line 37. Notice at Line 37, I haven’t put in a \{align*} to end thealign environment. I will need to put this in before the code will work.

10 of 11

A Guide to LyX and LATEX Ashley Cheng

3.6 From here...?

Now that you have the Basics of LATEX down, you can now experiment from here on out.I highly recommend that you use the Wikibook for help.

If you have questions, feel free to email me at [email protected]

Good Luck!

11 of 11