COMP110 Practical 6 LATEX (Part 1) 1 Introduction

Research papers in Computer Science are typically not written using a word processor like Microsoft Word or LibreOffice, but with a typesetting system called LATEX (pronounced LAY- tech). Word processors commonly have a graphical user interface, text appears as you type, what you see resembles more or less what a printout will look like (WYSIWYG), the quality of type- setting is mediocre, and text is saved in a proprietary binary format or pretend-standardised XML format which is not future proof. In contrast, LATEX is a procedural markup language and a system to turn text contained markup into documents ready for printing. Text and formatting commands are entered and stored into plain text files using your favourite text editor. Output, in the form of DVI, PostScript or PDF files, is produced by processing those text files, akin to compiling program files to produce executable code. The algorithms producing the output are quite sophisticated and the typesetting is of high quality. As in the case of program development, special editors exist (e.g. LyX, TeXnicCenter, TeXstudio and TeXworks for ; , , LyX, TeXstudio and TeXworks for ) which ease the burden of this write-process-look cycle, but their use is not strictly necessary. LATEX was originally developed by Leslie Lamport, and its first widely available version LATEX 2.09 appeared in 1985. The current version of LATEX, LATEX 2ε, was released in 1994. However, development of LATEX 2ε macro packages, that is, of collections of definitions of procedural markup used in LATEX documents, is ongoing and there are new releases every few month. Both LATEX and LATEX 2ε are based on the typesetting system TEX, developed by Donald E. Knuth from 1977 onwards. TEX is essentially stable since the release of version 3.0 in 1990. Since then only bug fixes have been added. The latest version is 3.1415926 (version numbers converge to π) and was released in March 2008, i.e. since then no further bugs have been discovered. TEX is the closest we have to a non-trivial bug-free program. This practical and several of the following practicals will introduce you to LATEX. The current practical focuses on the basics. The advantages of LATEX will become apparent when we move to more advanced features like cross-references, citations, and bibliographies in the other practicals. For a discussion of the advantages of LATEX and differences to word processors like MS Word see (Stack Exchange Contributors, 2015; 2016a;b). The following two module learning outcomes relate this practical: • To effectively use relevant software packages and appreciate different types of software; • To effectively use general IT facilities including organising your file store, taking advantage of access control and security features of operating systems. This document can be found at http://intranet.csc.liv.ac.uk/~ullrich/COMP110/notes/practical06. Some of the tasks below might be performed more quickly if you cut-and-paste from the PDF document.

1 2 LATEX on Scientific Linux

2.1 Basic LATEX commands

1. We will use the Department’s Linux systems to experiment with LATEX. Use Exceed to connect to one of the Linux systems, log in using your departmental account name and password, and open a web browser. 2. Download the file

http://intranet.csc.liv.ac.uk/~ullrich/COMP110/notes/small.

and store it in an appropriate place. By convention, files containing TEX or LATEX typesetting code end in ‘.tex’. 3. Open the file small.tex in a text editor on the Linux system and a have a look at it. 4. The command sequence that needs to be executed to generate PostScript or PDF from small.tex is depicted in Figure1 below.

create/edit view/print print/post print/post compile convert convert with with with dvips ps2pdf small.tex small.dvi small.ps small.pdf

convert with pdflatex

Figure 1: LATEX compile cycle (Greenberg, 2010)

To give it a try, open a terminal, change to the directory where you have stored the file small.tex and execute the following commands in sequence: latex small dvips -o small.ps small The command latex small creates a DVI-file small.dvi from small.tex. While process- ing, latex will display some diagnostic output in your terminal: This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011) restricted \write18 enabled. entering extended mode (./small.tex LaTeX2e <2011/06/27> Babel and hyphenation patterns for english, dumylang, nohyphenation, loaded. (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2007/10/19 v1.4h Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo)) No file small.aux. (/usr/share/texlive/texmf-dist/tex/latex/base/omscmr.fd) [1] (./small.aux) ) Output written on small.dvi (1 page, 2220 bytes). Transcript written on small.log. You can take a look at the DVI-file using a DVI-previewer like okular or xdvi. Execute okular small.dvi &

2 in the terminal window to do so. The second command that you have executed, namely dvips -o small.ps small, has created a PostScript-file small.ps from small.dvi. You can take a look at the PostScript- file using a PostScript-previewer like okular, evince, or gv. Execute

evince small.ps &

in the terminal window to do so, and compare the DVI-file with the PostScript-file. 5. We can also generate a PDF-file from small.tex. We can either use pdflatex instead of latex to directly process a LATEX-file to generate PDF or we can use ps2pdf to convert the PostScript-file that we already have into PDF. Execute pdflatex small ps2pdf small.ps small2.pdf in the terminal window to try both alternatives. The first command will produce a file small.pdf, while the second command will produce a file small2.pdf (without the op- tional second argument, ps2pdf would also produce a file small.pdf, overwriting the file generated by pdflatex in the previous command). PDF-files can be previewed using okular, evince, or acroread. Compare small.pdf and small2.pdf using

evince small.pdf & evince small2.pdf &

6. PDF is the preferred format for the distribution of documents prepared with LATEX. It gives you a high-level of certainty that any receiver of a PDF-file will see your document in exactly the layout you intended it to be and that a printout will also conform to exactly that layout (the same cannot be said for Microsoft Word). PDF-previewers are available for all major platforms, including mobile devices. However, if you prefer to make your documents easily accessible via the web, you can also convert LATEX-files to HTML. There are a number of LATEX-to-HTML converters available. One of them is latex2html. Execute

latex2html -split 0 small.tex

in the terminal window (Note that ‘0’ is the number zero; you can use latex2html -h to find out what the -split 0 option does). You will see that a subdirectory small has been created in the directory where you have stored small.tex. In the subdirectory are several files, including small.css and small.html. Execute

firefox small/small.html &

to have a look at the result of the conversion.

7.L ATEX has its own rules for deciding the length of blank spaces. In particular, LATEX will put an extra amount of space after a period ‘.’ if it considers that the period marks the end of a sentence. It regards a period as the end of a sentence if it is preceded by a lowercase letter. If a period is preceded by an uppercase letter then LATEX assumes that this a period that follows the initials of somebody’s name.

3 This heuristic does not always make the right decision, e.g., in ‘Mr. Smith’ or in ‘Proc. IJ- CAI’, the period does not end a sentence but indicates an abbreviation. To overwrite LATEX’s assumption that some extra amount of space should be inserted in these cases, you have to put a backslash ‘\’ after the period, e.g., ‘Mr.\ Smith’ and ‘Proc.\ IJCAI’. If you want to make sure that ‘Mr.’ and ‘Smith’ are not separated by a line-break, use ‘Mr.Smith’˜ instead. Open the file small.tex in a text editor and insert the following text before the line \end{document}:

\begin{flushleft} Mr. Smith (without backslash)\\ Mr.\ Smith (with backslash)\\ Mr.~Smith (with unbreakable space)\\ Proc. IJCAI (without backslash)\\ Proc.\ IJCAI (with backslash) \end{flushleft}

Save the file, execute pdflatex small once in a terminal window, and open the file small.pdf in a PDF-previewer. See the (minute) difference in the amount of space af- ter the period depending on the presence or absence of the ‘\’. You will probably have to increase the magnification in your previewer to be able to see it.

8.L ATEX also determines by itself how to break up a paragraph into lines, and will occasion- ally hyphenate long words where this is desirable. However it is sometimes necessary to tell LATEX not to break at a particular blank space, e.g., there should never be line-break between ‘Example’ and ‘7’ in ‘Example 7’. The special character used for this purpose is ~. It represents a blank space at which LATEX is not allowed to insert a line break, e.g., ‘Example~7’. It is often desirable to use ~ in names where forenames are represented by initials, e.g., ‘U.~Hustadt’.

9. You can find a PDF-file of Greenberg’s introduction to LATEX(Greenberg, 2010) at

http://www.ctan.org/tex-archive/info/simplified-latex/simplified-intro.pdf

Download the file, read chapter 2, and experiment with fonts (described in sections 2.1 and 2.4), lists (described in section 2.2), and tables (described in section 2.3). In the following practicals we will consider labels and references (chapter 4) and citing and bibliographies (chapter 3). Another very good introductory text by Oetiker, Partl, Hyna, and Schlegl(2015) can be found at

http://mirrors.ctan.org/info/lshort/english/lshort.pdf

In addition, there is a two-page quick reference sheet by Winston Chang that is very helpful for beginners:

http://wch.github.io/latexsheet/latexsheet-a4.pdf

4 2.2 TeXstudio

10. There are a range of specialised editors / IDEs for LATEX. One of them is TeXstudio. TeXstudio is not only available for Linux but also for MS Windows and MacOS X. On Linux, use ‘Applications→Office→TeXstudio’ to start TeXstudio. TeXstudio has a menu bar with entries ‘File’, ‘Edit’, etc and a toolbar with entries ‘New’, ‘Open’, etc that allow easy access to LATEX related commands. There are also four panes that initially will be empty. The leftmost pane is the ‘structure’ pane, the top middle pane is the ‘editor’ pane, the bottom middle pane is the ‘message’ pane, and the rightmost pane is the ‘preview’ pane. 11. Use ‘File→Open...’ and the file selector to load the file small.tex into TeXstudio. In the editor pane you now see the content of small.tex. In the structure pane you see the section structure of the LATEX document. You can use the small triangles in the struc- ture pane to unfold the section structure so that subsections become visible (provided there are subsections). Clicking on a section or subsection title will move the view in the editor pane to the corresponding part of the document and highlight the title. At the moment your document is not long enough for that to be particularly helpful, but still, give it a try. 12. By default, the editor pane does not show you line numbers. Use ‘Options→Configure TeXstudio’ to open the settings. Click on the ‘Show Advanced Options’ check box in the bottom left corner and confirm the message that TeXstudio shows in response. In the left menu you now got a much longer list of TeXstudio elements that you can configure. Click on ‘Adv. Editor’. Under ‘Appearance’ you have an entry ‘Show Line Numbers’ that is currently set to ‘No Line Numbers’. Change that to ‘All Line Numbers’. Click on ‘OK’ in the bottom right, which will close the settings. 13. Below the ‘Bibliography’ menu entry you find the ‘Build & View’ button on the toolbar. Clicking the ‘Build & View’ button will process the LATEX file and display it in the preview pane. Click the ‘Build & View’ button to see whether it works. You are probably shown a pop-up message about shortcuts, read the message, click on the checkbox next to ‘Do not show this message again’, then click ‘OK’. In the preview pane you should now see a preview of the PDF file that the compilation process has produced. Above the preview pane there are several buttons that allow you to manipulate what you see. Using the right-most buttons adjust the magnification of the preview: ‘Fit to Text Width’ might be a good choice. What exactly the ‘Build & View’ button does is determined via ‘Options→Configure TeXstudio→Commands’ and ‘Options→Configure TeXstudio→Build’. Have a look at these settings but do not change anything. 14. The ‘Build & View’ process will have produced some output that is shown in the message pane. Switch between the ‘Messages’ and ‘Log’ tab. If everything worked as expected, then the ‘Messages’ tab shows you which command was used to compile your document and that the corresponding process was completed successfully. The ‘Log’ tab will be empty. 15. Let us see what happens if we introduce an error into our document. Insert the following text before the line \end{document}:

\nonexistentmacro{arg}

5 Click ‘Build & View’. The message pane should automatically switch to the ‘Log’ tab. There you should see that your file small.tex contains an error, in which line of the file the error occurs and what the error is. In the editor pane, the corresponding line has been highlighted in red and there is a small indicator to the left of the line number in the form of a red ‘do not enter’ sign. Fix the error by removing the line that you have just added. Then click ‘Build & View’ again. Everything should revert to normal.

16. A nice feature of TeXstudio is the ‘LaTeX’ menu. It allows easy access to LATEX macros. In the editor pane, position the cursor on the line following \begin{document}. Use ‘LaTeX→\tableofcontents to insert the macro \tableofcontents into small.tex. Click ‘Build & View’. In the preview pane you see that you now have a table of contents for your document appearing at the beginning of it. Also, notice that the message pane shows you that pdflatex has been executed twice. This was necessary since the first execution creates the table of contents but writes it to the auxiliary file small.aux and only the second execution incorporates the table of contents into the PDF file that is generated.

17. There are several additional ways in which TeXstudio makes the use of LATEX macros easier. LATEX supports a lot different character sets and symbols that are accessed via macros. To the left of the structure pane you see a vertical list of symbols, clicking on one will show even more symbols in the structure pane. Click on the symbol ‘λ’. This will show you the Greek alphabet in the structure pane. Place the cursor before \end{document} in the editor pane. Click on the ‘$$’ symbol to the left of the editor pane. This will add two ‘$’ symbols and places the cursor between them. Now, try to select the greek letters for the word χαιτω. Click on ‘Build & View’ once you are done to see whether you get the correct result. 18. Besides the various menus, TeXstudio supports completion. In the editor pane, place the cursor before \end{document} towards the end of the file on an empty line. Type \begin{e, as you type you see that TeXstudio suggests various possible macros that would complete what you have typed so far. You can use the arrow keys to move between the suggested completions and pressing return will insert the currently selected completion into your document. Select \begin{enumerate}. Note that TeXstudio not only inserts that particular text but the whole ‘skeleton’ of an enumerate-environment and places the cursor after the first \item. Type some text, e.g., ‘My first item’. Process the file and see how it has changed. Use ‘Edit→Undo’ (several times) to revert back to the version of the document before inserting the enumerate-environment. 19. Another interesting feature of TeXstudio is that it offers a close integration of editor and previewer (sometime called ‘inverse search’ and ‘forward search’). In the previewer, look for the string ‘quoted text’. Click on that string while at the same time holding down the CTRL-key. In the editor pane you should see that the line containing the string ‘quoted text’ is now highlighted in blue. Similarly, in the editor pane, look for the word ‘Remember’. Click on that word while at the same time holding down the CTRL-key. Now, in the previewer the line containing that word should be highlighted in yellow. 20. Use ‘Help→User manual’ to learn more about TeXstudio.

6 2.3 LyX

LyX is a ‘document processor’ that tries to combine the power of TEX/LATEX with a user interface that is similar to that of word processors like MS Word. If you are uncomfortable with directly entering LATEX code, then this approach might be right for you. On Linux, use ‘Applications→Office→LyX’ to start LyX. In LyX, use ‘Help→Tutorial’ to learn how to use LyX. The documents that you produce in LyX are saved in a special LyX for- mat. If you want to save a document as LATEX source, you have to use ‘File→Export→LaTeX (pdflatex)’. It is also possible to import LATEX document via ‘File→Import→LaTeX (plain)’ though this does not always lead to a perfect result. It is best to use LyX to create documents from scratch.

3 LATEX on MS Windows

LATEX and TeXworks are not only available on our Linux systems but also on our Windows systems. For the following exercises make sure that all the files you are working on are stored on the file server (i.e. the H: drive under Windows or (a subdirectory of) your home directory under Linux). This will allow you to access those files under both Linux and Windows on any machine within the department.

3.1 TeXstudio

To start TeXstudio use Start→TeXstudio→TeXstudio. You can then open the file small.tex or other LATEX files using File→Open. . . in the application. Alternatively, double clicking on a LATEX file in Windows Explorer should also open that particular file in TeXworks. Repeat the steps in Section 2.2 above in order to check that the Linux version of TeXstudio and the MS Windows version of TeXstudio work in the same way.

3.2 MiKTeX

Alternatively, you can use the MiKTeX software suite installed under Windows to work with LATEX. To do so, open a Command Prompt under Windows and change your directory to the directory where your LATEX related files are stored. You can then use the commands latex, dvips, ps2pdf, , makeindex as described in Section 2.1. latex2html will not work as it is only installed under Scientific Linux. To preview a file proceed as follows: 1. The DVI previewer under Windows is called ‘Yap’ (‘Yet Another Previewer’). To preview the file small.dvi, execute yap small.dvi in the command prompt. Alternatively, locate the file in a file manager and double click on it. This should automatically start Yap on the file. 2. The PostScript previewer under Windows is called ‘GSview’. Double clicking on a PostScript file in a file manager should automatically open the PostScript file using ‘GSview’. GSview may first point out that registration of this product is possible. If that is the case, then simply click on the button that indicates that you don’t want to do so. 3. Finally, as PDF previewer you can use Adobe Reader or MS Edge. Double clicking on a PDF file in a file manager will open it in MS Edge. By right-clicking on a PDF file and following ‘Open with→ Reader DC’ you can preview it in Adobe Reader instead. If

7 you want to change the default previewer for PDF files to Adobe Reader, right-click on a PDF file, select ‘Properties’, then under ‘Open with’ click on ‘Change. . . ’ and select ‘Adobe Acrobat Reader DC’ among the options offered and click ‘OK’. To edit LATEX and LATEX-related files you can use Notepad++ or Atom. Both support syntax highlighting for LATEX files. Notepad++ has the nice feature that it allows you to fold-up and fold-down logical blocks of your LATEX file (just as TeXstudio does). However, it will be more convenient to use a specialised editor like TeXstudio to edit LATEX files.

4 Using LATEX at home

If you use Linux at home, then you just need to install the necessary packages, the package manager of the distribution that you use should make that easy. If you are using MS Windows or MacOS X at home, then you can download MiKTeX from http://miktex.org/ or TeX Live from http://www.tug.org/texlive/. You can download TeXstudio from http://www.texstudio.org/ and LyX from http://www. .org.

References

H. J. Greenberg (2010). A Simplified Introduction to LATEX. University of Colorado at Denver. http://mirrors.ctan.org/info/simplified-latex/simplified-intro.pdf [accessed 3 November 2015].

Tobias Oetiker, Hubert Partl, Irene Hyna, and Elisabeth Schlegl (2015). The Not So Short Introduction to LATEX 2ε: Or LATEX 2ε in 157 minutes (Version 5.05). http://mirrors.ctan. org/info/lshort/english/lshort.pdf [accessed 3 November 2015].

Stack Exchange Contributors (2015). Why should I use LATEX? TeX - LaTeX Stack Exchange, 7 November 2015. http://tex.stackexchange.com/questions/1756/why-should-i- use-latex [accessed 29 October 2016].

Stack Exchange Contributors (2016a). What are the advantages or disadvantages of using LATEXfor writing scientific publications. Academic Stack Exchange, 18 February 2016. http://academia.stackexchange.com/questions/5414/what-are-the- advantages-or-disadvantages-of-using-latex-for-writing-scientific-p [accessed 30 October 2016].

Stack Exchange Contributors (2016b). LATEXvs Word; improvements of LATEXover the years. TeX - LaTeX Stack Exchange, 19 June 2016. http://tex.stackexchange.com/questions/ 218567/latex-vs-word-improvements-of-latex-over-the-years [accessed 30 October 2016].

8