Archive for the ‘latex’ Category

Let’s Learn LaTex: Part 2

Monday, February 22nd, 2010

Last week I have shown you the bare bone basics of LaTex. We learned how to create a simple hello world document, and the usual boilerplate code that goes into the preamble. Lets move on to slightly more complex ideas.

I want to start with the meta-information you can add to your document. Each paper you create will likely have a title, author and creation date. Latex has special commands you can use to insert that info into the preamble:

\title{My Document}
\author{Lukasz Grzegorz Maciak}
\date{\today{}}

Note that the date command will accept anything as an argument – you don’t have to format your input in some special way. In the sample above I am using a latex command \today{} in order to add the current date to the document. This means that your work will have the date of the day when it was last compiled. If you go back to your work, make changes and re-compile it, the date will be adjusted automatically.

This information will now be used throughout the document for things like headers and footers and etc. It won’t be displayed at the top of your document unless you actually want to though. If you do just put \maketitle somewhere below the \begin{document} like this:

\documentclass[10pt, letterpaper]{article}
 
\title{My Document}
\author{Lukasz Grzegorz Maciak}
\date{\today{}}
 
\begin{document}
\maketitle
 
Lorem ipsum dolor sit amet ...
\end{document}

This should produce something like this:

Use \maketitle to display title information above the document

By the way, notice how each paragraph in the sample above is indented. This is automatic. In my source code I simply leave a blank line between paragraphs (a bit like in this blog) and LaTex does the rest. If you don’t like the indentation, you can always suppress it by adding \noident at the begging of a paragraph.

Starting documents this way is nice, but there are cases where it is appropriate to have the title, author and date on a separate page (for the sake of this tutorial let’s call that special page a “title page”. Also for the sake of this tutorial imagine me saying “title page” in Dr. Evil’s voice why making air-quotes gesture why Seth Green rolls his eyes in the background). Let me show you how it’s done:

\documentclass[10pt, letterpaper, titlepage]{article}

Yep, it’s that easy. Just pass a single optional argument to your \documentclass{} and you are done.

Our next step would be to add some structure to our document – for example, lets say you want to break it up into sections and subsections like this:

\section{This is a section}
\subsection{This is a subsection}
\subsubsection{This is a subsubsection}

Here is an output sample with some lorem ipsum added in between the section headings to make it look more like a real document – just so that you can get the sense of the relative font sizes and spacing:

LaTex Sections

That’s exactly how you use these. Sections are not environments – you don’t need to open and close them – they are one shot commands. Note the numbering that was added to my document. It is automatic, and inserting new subsection somewhere in the text will automatically re-enumerate everything at compile time so you don’t even have to think about it.

By default LaTex supports seven levels of document subdivision: part, chapter, section, subsection, subsubsection, paragraph and subparagraph. They all work in the same way, main difference being the formatting. For example, the chapter command (which is only defined in report and book document classes) will force a page break and will make the title take up half the page. Paragraph and subparagraph on the other hand don’t usually get numbered but you can change that if you want.

You can suppress the numbering on any of these elements by adding an asterisk after the command name but before the arguments like so:

\section*{Non numbered section}

Now that we have the whole document broken down into logical parts, the next step would be to generate table of contents no? Once again, all you need is a simple single word command called \tableofcontents. You can put it wherever you want in your document. I usually include it at the beginning like so:

\begin{document}
\maketitle
\tableofcontents
\newpage

Note that I also used \newpage command that I believe should be quite self-explanatory. It forces a page break so that my table of contents is on a page of its own accompanied only by the document title information:

Table of Contents

You now know how to structure your papers in LaTex. I bet you are dying to find out how to do basic font formatting operations such as making your text bold, italic and etc. It is actually incredibly easy and can usually be accomplished using one of the “text” commands like the ones below:

\textit{This text is italic} \\
 
\textsl{This text is slanted} \\
 
\textbf{This text is boldface} \\
 
\texttt{This text is in typewriter font} \\
 
\textsc{This text is in small caps} \\
 
\emph{This text is emphasized}

Note how I used the \\ an a line break at the end of each line. This is basically the LaTex way of saying <br;> or Shift+Enter (in Word) – a mid paragraph soft line break. Here is a sample output:

The text formating commands

Note that you can nest these commands like this:

\textbf{\textit{\texttt{Boldface. italicized typewriter font}}}

There are also several formatting environments that let you change the size of your font appropriately (think <big> and <small> tags from HTML). They are as follows:

\begin{tiny}This text is tiny\end{tiny} \\
 
\begin{scriptsize}This text is script size\end{scriptsize} \\
 
\begin{footnotesize}This text is footnote size\end{footnotesize} \\
 
\begin{small}This text is small\end{small} \\
 
\begin{normalsize}This text is normal size\end{normalsize} \\
 
\begin{large}This text is large\end{large} \\
 
\begin{Large}This text is upper-case Large\end{Large} \\
 
\begin{LARGE}This text is all-caps LARGE\end{LARGE} \\
 
\begin{huge}This text is huge\end{huge} \\
 
\begin{Huge}This text is all-caps HUGE\end{Huge} \\

And here is some output. Note that the normalsize is the “default” font size as set in your \documentclass:

Font sizing environments

That’s all for now folks. Next time I will show you how to make lists, tables and maybe how to import packages and insert graphics to your documents.

The other day someone asked me how far am I planning to go with these tutorials (ie. how in depth are they going to get). I actually don’t know. I guess I’ll see whether or not people like these. If they do, I can try pushing on definitely even after I am out of my comfort zone. In that case we will be all learning something new each time. On the other hand if the LaTex posts remain dead (ie. no one ever comments on them) I might wrap them up after I show you guys all the basics and reach some good closing point. We shall see.

Let’s Learn LaTex: Part 1

Monday, February 15th, 2010

Few days ago Travis gave me an idea fro a new series of posts. He mentioned recommending my blog to a friend who was trying to learn LaTex. Unfortunately, my blog is not the best learning resource. It is more like a collection of “hey, look what I found out today” type posts. Some of which have proven to be quite useful to the inhabitants of the interwebs. For example my post on the quirky way in which LaTex processes figure numbers still gets several thank-you comments per month, despite being over 3 years old.

I tend to bring up LaTex in random conversation on this blog all the time. In most cases I assume most of my readers know what the hell am I talking about. But this is often not the case. In fact, I can probably safely guess that majority of my regulars have never really used my favorite typesetting tool. So I figured that I might as well sit down and write a few introductory LaTex posts. Perhaps I can teach bunch of you something new. If not, it may still be an interesting exercise – and perhaps it will bring in some Google juice as well via LaTex related queries.

First off I should probably tell you why you should use LaTex instead of Microsoft Word or other word processing tool. Only I already did that. So I will just refer you to my earlier post in which I illustrate how LaTex is superior to office.

Next I should probably tell you how to install and configure the damn thing. Only I already did that too. Here is one of my posts that shows you how to install a full LaTex suite on windows. The ProTex package I linked to in the article contains Texnic Center – a decent, easy to use IDE that makes compiling tex files into PDF just a matter of pressing a button. I’m fairly confident that since you are reading this blog, you can probably locate the right icon on the toolbar by yourself. So I will not be showing you how to actually run LaTex commands from the CLI or how to use the IDE. I will jump right into teaching you the language itself.

LaTex basically works like a markup language. I always compare it to HTML because it is what most people are familiar with. Just like in HTML the text you type out will be rendered as a wall of text. LaTex will ignore most white space, save for spaces that separate words and newlines that tell it to start a new paragraph. Any other formatting or structural organization must be added to the file using specially formatted commands. HTML uses the greater and less then characters as escape sequences. Anything enclosed in them becomes a HTML tag and is not rendered, but rather interpreted as a rendering instruction. In latex our escape character is backslash (“\”). Any word prefixed with this character will be interpreted as a LaTex formatting command. For example:

\newpage

LaTex command can take attributes, which are usually enclosed in curly braces {} like this:

\textbf{this is an attribute}

Om case you were wondering, the “bf” in textbf stands for “bold face”. The command above therefore can be used to make your text bold.

Optional arguments are enclosed in square brackets []. These are usually switches and toggles that modify the commands, while the mandatory argument in curly braces is usually some sort of input that will be processed by the command (but not always). If there are several optional arguments they are written as a comma separated list. The order usually does not matter as LaTex is smart enough to figure to match them up. Here is another example:

\documentclass[letterpaper, 10pt]{article}

First I’d like to show you how a LaTex environment looks like. Environments are a bit like HTML tags – they come in pairs. First command opens the environment, and the other one closes it. Anything you type inside an environment is subject to some special formatting rules. The simplest environment you have to learn is of course:

\begin{document}
    This is where you type your document.
\end{document}

The document environment is essentially the equivalent of the HTML body tag. It denotes where your document starts and where it ends. Anything above it is the preamble – which works a bit like the head section of a HTML document. That’s where you put settings, meta-information, credits, comments and etc.

Speaking of comments – before I forget I should tell you that LaTex uses percent sign % to denote a comment. If you need to use the % sign in your text you will have to escape it with backslash like this:

To escape \% use \\ % and this is a comment

You escape the backslash the same way – with a backslash. There are no multi-line comments – you just have to deal with it.

That’s pretty much it. 99% of time you will not need to use anything beyond these basic constructs. From this point on, it is just a matter of learning bunch of basic commands. The beauty of LaTex is that most of the aesthetic stuff is already preconfigured for you. Unlike a basic HTML page, a basic LaTex document looks good “out of the box” so most of the time you don’t have to do anything beyond some basic tweaking of the standard template.

Similarly to HTML, LaTex tries to separate content from presentation. The presentation is usually controlled by document class files (.cls) which are a little bit like CSS (but don’t look too much into that parallel – it’s a whole different ballgame). These files describe how the text is supposed to flow on the page, default margin settings, how and where the page numbers are displayed, whether or not the pager should be printed as if it was to be bounded in a book and etc. You can override most of these settings via simple latex commands in your document of course. But if you don’t, then changing from one style to the other becomes just a matter of changing a single line in the preamble.

Wen you start an new document, the first thing you need to do is to tell LaTex what document class you want to use. That’s where that line I skipped comes into play:

\documentclass[letterpaper, 10pt]{article}

Here I’m telling LaTex that I want to use article.cls as my class file. Documentclass command also takes in several optional arguments. These arguments depend on the class you choose, but most of them relate to default font size (here 10 points), default paper size, page orientation, number of columns and etc. You can usually find the list of those in the documentation for your class file.

Your standard installation comes with 4 basic classes: article, report, book and letter. Their optional arguments are listed in million places all over the web – for example here. As you may imagine, each is geared towards different type of document. Article is for writing short articles destined to be published standalone or in a magazine or journal of some sort. Report is for longer scientific documents that need to be broken down into chapters. Book formats your document in preparation for binding, with alternate styles for even and odd pages, distinctive chapter pages and etc. Letter format on the other hand conforms to the customary rules for official letter writing. If you need something more specific you can usually obtain an appropriate document class file online. Foe example, if you need to publish your paper in IEEE standard you can grab their class file from their website. In most cases though, all you need is one of the basic classes.

So let’s put all this information together and create a simple hello world file:

\documentclass[10pt, letterpaper]{article}
\begin{document}
	Hello World
\end{document}

Once you type all of that in, simply hit the big PDFLatex button in your IDE (most of them have one) to generate a nice PDF file. It should look something like this:

Hello World Document

That’s it folks. That’s how you start a LaTex document. What? You thought it would be hard? No it is not. It’s easy! Pick a document class, start a document, type your stuff, end document. That’s all! Of course this is merely the tip of the iceberg. Next time I will show you more useful commands and environments, as well as introduce you to packages.

Vim LaTex

Monday, October 20th, 2008

I noticed that in the past I produced several posts about combining LaTex and Emacs. While I do like Emacs and respect it’s formidable power I do not consider myself an Emacs user. Most of my day-to-day work is done in Vim which brings different kind of editing power to the table. Emacs is a lisp based framework for building text editing applications – fully programmable, easily customizable and almost endlessly extensible. The power of Emacs comes from what it can do. Vim’s power stems from what it does – and that is providing very powerful and yet simple modal text editing environment. I wanted to say intuitive, but yeah – that would be a lie. Vim is powerful because it allows you to accomplish just about anything in 3-4 keystrokes by chaining and combining simple commands.

Using Vim makes editing text much simpler and easier. It removes a lot of key-presses and movements. Let me give you an example – in a typical coding session you may want to copy the current line, and paste it below. Assuming your cursor is in the middle of the line how would you go about accomplishing it? Typically you would do:

  1. Hit Home to skip to the beginning of the line
  2. Hold shift and hit End to highlight the line
  3. Press Ctrl+C to copy the line
  4. Hit End again to put the cursor at the end of the line
  5. Hit Enter to insert a new line
  6. Press Ctrl+V to paste the line

How do you do it in Vim?

  1. Press the following keys in a sequence: yyp

That’s it. You want to paste the line 3 times? Do yy3p. This is the power you get when you use Vim. Of course die-hard emacs fanboys will remind me that one could always use Emacs’ VIP mode which emulates vi’s modal editing features. And they would be absolutely right. As I said, Emacs is a framework for building editors rather than an editor. So when you launch it in VIP mode you are really using Vi with the lisp powerhouse underneath it.

In theory you could combine the neat features of AUCTex on Emacs and the power the VIP mode gives you to get the best of both worlds. But if you don’t need these features – if you are a Vim purist or if you just need a simple modal text editor I have a nice alternative for you. It’s called VimLaTex.

vim-latex.gif

While it does not have the nice rendering features of AUCTex, VimLaTex is a must-have if you are planning to use Vim to edit LaTex files. The package itself is tiny – you just drop few hundred KB into your .vim directory and add few lines to your .vimrc.

The suite will add bunch of useful menus to your toolbar:

croppercapture100.jpg

These menus are your standard IDE like features – for example options for inserting packages, or snippets of code for special environments and etc.. A lot of the entries come with predefined key-bindings which make your life much easier. For example:

  1. To compile your file do: \ll
  2. To view compiled file do: \lv

You can set the preferred format for both options above by invoking :TTarget. It works like a charm both on Windows and Linux when I tried it.

VimLaTex also uses an interesting concept of placeholders. Whenever you use it to generate snippet of code, it will put funny looking character sequences inside of it. They may look silly until you realize how useful they are. For example let’s say you want to insert a figure:

\begin{figure}[h]
    \centerline{\psfig{figure=«eps file»}}
    \caption{«caption text»}
    \label{fig:«label»}
\end{figure}«»

The placeholders are there to make your life easier. When you press Ctrl+J vim will jump to the first «» delimited sequence and select it. This way you can just type over it. Press Ctrl+J again (while still in insert mode) and you will jump to the next one. How awesome is that?

If you are a Vim lover who also happens to use LaTex or a LaTex user who loves Vim definitely check this one out.