Latex – 1 – Basics

I hope now you have installed Latex on your computer along with an editor (i.e. : TexStudio). So let’s put our hands on the very basics of Latex.

Syntax

Since Latex uses a markup language to define the document structure, it consists of lots of similarities with markup languages. Latex translates your .tex file, which is a text file, into a high quality output file, similar to HTML.

Example source code would look like:

\documentclass[a4,12pt]{article}
\title{Introduction to Latex}
\author{Praneeth Nilanga Peiris}
\date{21 May 2014}
\begin{document}
   \maketitle
   Hello world! This is my first Latex document.
\end{document}

Explanation

Each command starts with a ‘\‘ (back slash),with it’s name. Then the parameters are passed into that using one or more{}” (curly braces). In some cases, options to the tag (similar to attributes in HTML) are passed using ‘[]‘ (square brackets).

The document starts from \begin{document} and ends from \end{document} (similar to <HTML> and </HTML> in HTML). Hence, \begin{document} and \end{document} denotes the scope of the document.

There are some empty commands such as \maketitle, which doesn’t request any parameters (similar to <br/> in HTML)

Spaces

Just like HTML, Latex ignores extra white spaces (spaces and tabs). But an empty line denotes a start of a new paragraph.

If you type this :

It does not matter whether you
   enter one or several             spaces
 after a word.
 
 An empty line starts a new
 paragraph.

The output still would be :

It does not matter whether you enter one or several spaces after a word.
An empty line starts a new paragraph.

Reserved Characters

As every computer language, Latex also has a set of reserved characters. They are :

# $ % ^ & _ { } ~ \

If you need those characters in your document, you should use a ‘\‘  prior to that. For example :

\# would output #

\% would output %

But you cannot use \\ to get \, since it denotes a line break. You have to use \blackslash for that purpose.

Groups

A group is enclosed by ‘{‘ and ‘}’ where the range/scope of the commands put between those braces is limited to them.

{\bf
This is bold.
}
This is no longer bold.

Environments

Environments in LaTeX have a role that is quite similar to commands, but they usually have effect on a wider part of the document. Their syntax is:

\begin{environment_name}
text to be influenced
\end{environment_name}

Example :

\begin{center}
Whatever you put here will be center aligned. It  can either be a text, image, table, figure etc.
\end{center}

Commands

LaTeX commands are case sensitive, and take one of the following two formats:

  • They start with a  ‘\’ and then have a name consisting of letters only. Command names are terminated by a space, a number or any other “non-letter”.
  • They consist of a  ‘\’ and exactly one non-letter.

Some commands need an argument, which has to be given between { } after the command name. Some commands support optional parameters, which are added after the command name in []. The general syntax is:

\commandname[option1,option2,…]{argument1}{argument2}…

Most standard LaTeX commands have a switch equivalent. Switches have no arguments but apply on the rest of the scope (i.e. the current group or environment). A switch should (almost) never be called outside of any scope, otherwise it will apply on the rest of the document.

%\emph is a command with argument, \em is a switch.
\emph{emphasized text}, this part is normal % Correct
{\em emphasized text}, this part is normal % Correct
 
\em emphasized text, this part is normal % Incorrect
\em{emphasized text}, this part is normal % Incorrect

Comments

% is used to comment a single line. When a % is found, it ignores the rest of the current line, the line break, and all whitespace at the beginning of the next line.

Example :

This is an % stupid
% Better: instructive <----
example: Supercal %
             ifragilist %
icexpialidocious

In order to use multi-line comments, you have to use a separate package called verbatim. You have to import that package by adding the following line before \begin{document} command. I will explain about packages in further articles.

\usepackage{verbatim}

Then you can use \begin{comment} and \end{comment} to insert multi-line comments as follows.

This is
\begin{comment}
something invisible, but
\end{comment}
an example for comments.

Compiling

Compiling a Latex document is another tedious task which I’m not going to explain here. It needs the Latex tool chain to handle various types of file types and your Latex IDE will take care of that part. If you’re eager to know, please see this.


Hope you find this article helpful. Will be there soon with more.

Leave a Reply