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:
\documentclass{article}
\usepackage{amsmath}

\usepackage{amssymb}

\begin{document}

\title{Basic Mathematics with Latex by http://QuickLatex.blogspot.com}

\maketitle

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.

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

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

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

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

$

\left(

\begin{array} {c c c}

1 & 2 & 3\\

4 & 5 & 6\\

7 & 8 & 9

\end{array}

\right)

$

\end{document}

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
\documentclass{article}
\usepackage{datatool}
\DTLloaddb[keys={c1,c2,c3}]{ctext}{csvtext.csv}
\begin{document}
\begin{table}
\begin{tabular}{clr}
\textbf{Software} & \textbf{Manufacturer} & \textbf{Malware}
\DTLforeach{ctext}{
\cola=c1,\colb=c2,\colc=c3}{\\
\cola & \colb & \colc}
\end{tabular}
\end{table}
\end{document}
The second method - using pgfplotstable package
\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}
\pgfplotstabletypeset[
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}
]{csvtext.csv}
\end{document}