Latex; Basics

Dr. Tom V. Mathew

4 October 2006

First file

is like a programming and it works on the principle of what you want is what you get (WYWIWYG) unlike the what you see is what you get (WYSIWIG). Why latex? Let the experts speak.http://www.andy-roberts.net/misc/latex/latexvsword.html

Compilation 1

This section will tell how to get you first tex page and its out put in pdf format.
  1. First create a file, say vim file.tex as follows.
    \documentclass{article}
    
    \begin{document}
    
    My First Tex File
    
    \end{document}
    
  2. Then compile this file as follows:
    pdflatex file.tex
    
  3. This will generate file.pdf which will produce file.pdf which can be viewed using xpdf, kghostview, or Adobe's Acrobat reader and threre by print.

Compilation 2

The above is latest quick way of using latex. The traditional way of using latex is as follows. This is recommended for several resons.
  1. First create a file, say vim file.tex as above.
  2. Then compile this file as follows:
    latex file.tex
    
    This will generate file.aux, file.log and file.dvi. Ignore .aux and .log file for now.
  3. The dvi file can be viewed using xdvi command as:
    xdvi file.dvi
    
  4. The dvi file can be converted to ps (postscript) file using 'dvips command
    dvips file.dvi -o samp01.ps
    
  5. This can be viewed using some post script viewer like say kghostview or ghostview. (this step is optional)
    kghostview file.ps
    
  6. The postscript file can be converted to pdf file using 'ps2pdf' command.
    ps2pdf file.ps
    
    which will produce file.pdf which can be viewed using xpdf, kghostview, or Adobe's Acrobat reader and threre by print.
  7. Sometime you would like to publish your document into a web page. This done by 'latex2html' command as follows
    latex2html file.tex
    
    This will create a folder by name 'file' and inside you will see a file callex index.html. This can be viewed by any browser, say:
    firefox file/index.html
    
The essential command can be summerised as
vim file.tex
latex file.tex
xdvi file.dvi
dvips file.dvi -o file.ps
ps2pdf file.ps

Formating

Title, sections

Save the following into a file.tex and complie and get the out put.

\documentclass{article}

\begin{document}
\documentclass{article}

\begin{document}

% The % symbole is comment line and text beyond this is not compliled

% The four lines below sets a title
%
\title{First Document}
\author{Myself}
\date{\today}
\maketitle


The title field can be replaced with text of your choice. Similarly author and
date. Notice that the \verb'\today' command give today's date. This is the date
this document is complied.

\section{First}

This is my first section. I am typing some dummy text here which will be
repeated and don't bother about the content.  I am typing dummy text here which
will be repeated and don't bother about the content.

\section{Second}

Although not required, to get a good source code, it is good idea to keep each
sentenses separated by \% indicating comment.
%
For, example this is sepated from the previous sentence by \% sign. This will
be treated as one paragraph.
%
This is another line, part of the same paragraph.

But this is a different paragraph since it is separated from the previous by a
bew line character.
%
This forms my second section.
%
The next will be my last section.

\section{Last}

This is my last section.
%
After, this, I saved my file and complied to get a pdf file.

\end{document}

Look at the pdf out put of the document. Who did the formating?

Equation

In line equations

In line equation is produced by giving $ sign before and after an equation. Few simple equation are given below.
\documentclass{article}

\begin{document}

A simple equation with english alphabets $z=x+y$,
another simple equation with greek alphabets $\alpha=\beta+\gamma$,
and an equation with some special notation $\sum z=\frac{\delta}{y^2}$.

\end{document}
This will produce equations like this: A simple equation with english alphabets $z=x+y$, another simple equation with greek alphabets $\alpha=\beta+\gamma$, and an equation with some special notation $\sum z=\frac{\delta}{y^2}$.

A simple equation with english alphabets $z=x+y$, another simple equation with greek alphabets $\alpha=\beta+\gamma$, and $\sum z=\frac{\delta}{y^2}$. Note that the equation number is generated automatically.

equation Environment

The equation environment will create an equation similar to above, but not in-line. The equation will be placed next line, justified middle, places some space above and below; and auotmatically generate an equation number. To get the equation below, use the source code in:
\begin{equation}
\Pi_{i=0}^{n^{2}}=\frac{\alpha^{\sqrt{y}}}{\sqrt{y-x}}
\end{equation}

\begin{displaymath}
\Pi_{i=0}^{n^{2}}=\frac{\alpha^{\sqrt{y}}}{\sqrt{y-x}}
\end{displaymath} (1)

eqnarray Environment

The eqnarray environment is similar to the equation; but it can support equation spanning to many lines.
\begin{eqnarray}
z = (x+1)+(y-2) \\
  = x+y+1-2 \\
  = x+y-1\\
\end{eqnarray}

$\displaystyle z = (x+1)+(y-2)$     (2)
$\displaystyle = x+y+1-2$     (3)
$\displaystyle = x+y-1$     (4)

Note that this environment has generated equation on all lines. However, the aligment of the equation is not proper. See the follwoing example which shows a nice alignment as well numbering for only once.
\begin{eqnarray}

\Pi_{i=0}^{n^{2}}&=&\frac{\alpha^{\sqrt{y}}}{\sqrt{y-x}}\\

&=&e^{25\beta}-1\nonumber\\

&=&\Delta+\Gamma\nonumber

\end{eqnarray}
This code will produce the follwing output.
$\displaystyle \Pi_{i=0}^{n^{2}}$ $\textstyle =$ $\displaystyle \frac{\alpha^{\sqrt{y}}}{\sqrt{y-x}}$ (5)
  $\textstyle =$ $\displaystyle e^{25\beta}-1$  
  $\textstyle =$ $\displaystyle \Delta+\Gamma$  

Table

Simple table

A simple table is given below.
\documentclass{article}

\begin{document}
\begin{tabular}{|c|c|}
Col One & Col Two \\ \hline
1 & 11 \\
2 & 22 \\
3 & 33 \\ \hline
\end{tabular}
\end{document}
This will produce a table like this:
Column One Column Two
1 11
2 22
3 33
The effect of hline is not clear in an html file.

Table Environment

The Table [*] shows a simple table.

Table: Simple Table Showing the Layout and Syntax
Left Center Paragraph Right
1 12.3 This portions is in a paragraph mode 1234
22 17.1 Again the same 234
333 77.7 Text 2

The above text is produced by the following code. Please also note how the table is referred using a label.
The Table~\ref{qtTabA} shows a simple table.
%
\begin{table}
%
\caption{Simple Table Showing the Layout and Syntax}
%
\label{qtTabA}
%
\begin{center}
%
\begin{tabular}{|l|c|p{2in}|r}\hline
%
Left & Center & Paragraph & Right \\ \hline
1    & 12.3   & This portions is in a paragraph mode & 1234 \\
22   & 17.1   & Again the same & 234 \\
333  & 77.7   & Text & 2  \\\hline
\end{tabular}
%
\end{center}
%
\end{table} 
%

Multi column and cline

The Table [*] shows a table with multi column feature.

Table: Table Showing the Multi-Column Layout and cline
Different justification possible
Left Center Paragraph Right
1 12.3 This portions is in a paragraph mode 1234
22 17.1 Again the same 234
333 Total Text 2

The above text is produced by the following code. Note that the cline command will not be clear in html mode.
%
The Table~\ref{qtTabB} shows a table with multi column feature.
%
\begin{table}
%
\caption{Table Showing the Multi-Column Layout and cline}
%
\label{qtTabB}
%
\begin{center}
%
\begin{tabular}{|l|c|p{2in}|r}\hline
%
\multicolumn{4}{c|}{Different justification possible}\\ \hline
Left & Center & Paragraph & Right \\ \hline
1    & 12.3   & This portions is in a paragraph mode & 1234 \\
22   & 17.1   & Again the same & 234 \\ \cline{2-3}
333  & \multicolumn{2}{l|}{Total Text} & 2  \\\hline
\end{tabular}
%
\end{center}
%
\end{table}



Prof. Tom V. Mathew 2006-10-04