Friday, February 24, 2012

Writing basic math in LaTeX - inline math and math environment

Internet abounds with LaTex tutorials on how to write mathematics equations and simple symbols in LaTeX. It is sometimes not clear as to how the math environment works in general and how to differntiate between inline math equations and how to write them on a line of their own.

I earlier did a tutorial on how to write mathematical equations using Latex which covered the "equation" environment in LaTeX. Math environment is equally important and in this tutorial I am going to talk about how to use basic math environment in LaTeX.

Specifically, I am going to discuss how to do the following in LaTeX:
  1. How to initiate Math environment
  2. Writing inline math equations and writing equations in a separate line
  3. How to use frac for equations
  4. How to use paranthesis and brackets to enclose mathematical symbols and equations
  5. How to type powers and indices
  6. How to write matrices
Following video illustrates the step by step instructions to use math in LaTeX

The code used in this tutorial is here:



\title{Basic Mathematics with Latex by}


This is inline \[n\] math symbol.

For inline $n$ or \(n\) is used and for displayed math we can use $$n$$ or \[n\].

We start with $n$ elements and we continue to divide them in half leaving $\frac{n}{2}$ elements.

The power can be written using caret symbol, for instance $n^n$ results in n to the power n written nicely.

The indices could be written using underscore, for instance $n_i$ makes i an index of n.


$\left( \frac{n}{2} \right)$\\


$\left[ \frac{n}{2} \right]$\\



\begin{array} {c c c}

1 & 2 & 3\\

4 & 5 & 6\\

7 & 8 & 9





Sunday, February 5, 2012

How to create table in Latex from MS Excel files or other external databases

Latex can be used to create tables from external files such as MS-Excel files saved as comma separated values or similarly many other formats. Recently, I received a comment where a reader suggested that I should post a tutorial on how to create tables in Latex from external files. I searched around and found multiple ways to achieve this. I am going to talk about two of the most popular ways of doing it. First one is using pgfplotstable and the second one is using datatool package.

Specifiacally, we are going to learn the following in this tutorial:
  1. Using datatool package to load external files
  2. Usng MS-Excel files to create tables in Latex
  3. Using \DTLloaddb to set keys and use external files
  4. Using \DTLforeach to iterate the external file
  5. Using \pgfplotstable package
  6. Using \booktabs package
  7. Using pgfplotstabletypeset
  8. Styling each column of table
  9. Using style rules from booktabs package
The following video tutorial explains both methods of creating tables in Latex using external database files.

The code for both of the methods is given below for your reference:
The first method - suing datatool package
\textbf{Software} & \textbf{Manufacturer} & \textbf{Malware}
\cola & \colb & \colc}
The second method - using pgfplotstable package
col sep=comma,
columns/Software/.style={string type},
columns/num1/.style={string type},
columns/num2/.style={string type},
every head row/.style={
before row=\toprule,
after row =\midrule
every last row/.style={after row=\bottomrule}

Friday, January 27, 2012

LaTeX Tutorial: How to use Lists in Latex - itemize, enumerate, description, and inparaenum

LaTeX is a great tool for typesetting and it is more powerful than MS-Word. It has a steep learning curve and that is why I believe these video tutorials will help you guys. Today, I am going to talk about lists in LaTeX. LaTeX can produce both bulleted lists or unordered lists, and numbered lists or ordered lists. We will also see how to use lists with descriptions given to each list item.

Specifically, we are going to have an introduction of following topics in LaTex:
  • Lists in LaTeX: Itemize, enumerate, inparalist, and description
    • Itemized list in LaTeX for bullets
    • enumerated list in LaTeX for numbered lists
    • inparalist in LaTeX for lists wrapped around text
    • description lists in LaTeX for lists with description for each item
  • Nested lists in LaTeX
  • Using styles for numbering lists (roman, in parenthesis etc.)
  • Use of paralist LaTeX package
Here is the video for this tutorial:

If you have a question or comment please do not hesitate to leave a comment. I will answer to as many comments as I can.

The code for this tutorial is here:

\title{Creating Bullets and Lists with \LaTeX by}
\section{Bullets and Lists in \LaTeX}
\item First bullet is here
\item Second bullet is here
\begin {enumerate}
\item This is item number 1
\item This is item number 2
\begin {description}
\item [Chapter 1] This is the first desciption
\item [Chapter 2] This is the second description
There are three advantages of this method:
\item it is faster,
\item it is cost effective, and
\item it is efficient

Thursday, January 19, 2012

LaTeX Tutorial: How to write mathematical equations in LaTeX

Those who use LaTeX for their documentation related works, usually are from STEM (Science, Technology, Engineering, Mathematics) background. These people use equations more often than not. Therefore, I will introduce how to write equations in LaTeX today.

In this tutorial we will go over following features:

  • Latex amsmath package 
  • Latex equation environment 
  • Using Simple equations like x = y + z 
  • Using Summation in equations 
  •  Using Integration in equations 
  • Using Cases in equation (if condition based values of a variable) 
  • Using fractions to write multiple-row equations
Watch the following video to learn how to do these things:

The code used in this Video can be found below:

\title{Writing Equations with Latex by}
%x = y + z
%f(x) = x ^ 2
%f(x) = x_1 + x_2 + x_3 + ......+ x_n
%f(x) = \sum_{i=1}^{n} {x_i}
%f(x) = \int_{i=1}^{n}{x_i}
%5, \text{if X is divisible by 5}
%10, \text {if X is divisible by 10}
%-1, \text {otherwise}
X =
\frac{\substack{\sum_{i=1}^{n} {x_i}}}
{\substack{\sum_{i=20}^{50} {x_i}}}

Thursday, January 12, 2012

How to write an algorithm in Latex : Video Tutorial with sample algorithm

People in Computer Science and Mathematics department often write algorithms for their papers, thesis, and other research articles. In this tutorial I will explain how to write an algorithm in Latex using the algorithm and algorithmic package in Latex. I will explain the basics of an article and show how simple building blocks can be added to Latex to write a full fledged professional quality algorithm.

Specifically, we will learn the following in this tutorial:

  1. How to write an algorithm in Latex
  2. Use of algorithm and algorithmic package
  3. How to use loops in an algorithm
  4. How to use IF statements in an algorithm
  5. How to add caption to an algorithm
  6. How to label an algorithm to refer it in the document
The code for this algorithm is shown below and explained in the video. 

Here is the code used in this video:

\textbf{INPUT:} Set of Base Layer polygon $S_b$ and Set of Overlay Layer polygon $S_o$\\
\textbf{OUTPUT:} Intersection Graph ($V$,$E$), where $V$ is set of polygons and $E$ is edges among polygons with intersecting bounding boxes.
\STATE Parallel Merge Sort set $S_o$ of overlay polygons based on X co-ordinates of bounding boxes\footnotemark[1]
\FORALL{base polygon $B_i$ in set $S_b$ of base polygons}
\STATE find $S_x \subset S_o$ such that $B_i$ intersects with all elements of $S_x$ over $X$ co-ordinate
\FORALL{overlay polygon $O_j$ in $S_x$}
\IF {$B_i$ intersects $O_j$ over $Y$ co-ordinate}
\STATE{Create Link between $O_j$ and $B_i$}
\caption{Algorithm to create polygon intersection graph}

The output looks like the following:

Thursday, January 5, 2012

How to draw Reddit Alien in LaTeX using Tikz - Video tutorial and code

If you are not a Redditor you are a lucky person. It's a one way traffic scenario with no dead end. Once you subscribe to Reddit there is no way back and it is no good being there. I am expecting my colleagues to have an intervention for my Reddit addiction soon but till then I am all here to invest (read:waste) my precious time.

Since we got that out of our way, let us get back to business. Today, we are going to see how to draw a cartoon figure using LaTeX. The motivation behind this post is /r/latex on Reddit as I wanted that subreddit to have its logo drawn in LaTeX. I did not know it already was in LaTeX but it seems my work is appreciated there so I am going to contribute my 2 cents to the community.

Specifically, we will learn following things in this tutorial:

  1. How to work with Colors - defining RGB colors in LaTeX.
  2. How to use multiple layers to set order of document objects (send to back, bring forward like functionality).
  3. How to use Arcs in an effective way. 
  4. How to draw lines with multiple points 
  5. How to draw curved lines in LaTeX

Check out this video for explanation of the code. The co-ordinates might look crazy but after you go through the video it will be a cinch. Leave a comment if you have a question. The code is given below, so if you improve it please do let me know and I will post your code on here.
\tikzstyle{vrutt}=[draw=orangered, fill=orangered, circle,minimum height=0.5in, line width=5mm]
\tikzstyle{elli}=[draw, ellipse, minimum height=2.85in, text width=2.95in, text centered, line width=5mm]
\begin{pgfonlayer} {foreground}
\node [elli, fill=white] (face) {};
\node [below of=face,yshift=-4.1in, xshift=-2.0in] (base){};
\draw [line width=5mm](base) -- +(3.8in,0in);
\draw [line width=5mm] (4.3,-11.5) arc (-10:80:1.8);
\draw [line width=5mm] (-4.7,-11.5) arc (190:80:1.8);
\draw [line width=5mm](face.230) to[out=260, in=150] +(0.75in,-3.15in);
\draw [line width=5mm](face.310) to[out=280, in=30] +(-0.75in,-3.15in);
\node [vrutt, xshift=-5em, yshift=9mm] (lefteye) {};
\node [vrutt, xshift=5em, yshift=9mm] (righteye) {};
% Smile
\draw [line width=5mm] (-2.0,-1.0) to[out=320, in=220] (2.0,-1.0);
\draw[line width=5mm](-0.5,3.76) -- +(1cm, 2.5cm) -- +(3.5cm, 2cm);
\node [vrutt, fill=none, draw=black, above of=face, yshift=4.65cm, xshift=3.5cm, minimum height=0.5in] (antenna){};
\node (face.275)[yshift=-3in] (text){\Huge \textbf{\LaTeX}};
\begin{pgfonlayer} {background}
\draw [line width=4mm] (4.4,1.3) arc (-80:315:1);
\draw [line width=4mm] (-4.3,1.3) arc (-80:315:1);
\draw [line width=4mm] (3.05,-7.8) arc (-70:80:2.3);
\draw [line width=4mm] (-3.05,-7.8) arc (250:90:2.3);

The alien looks like this one: