Archive for the ‘latex’ Category.

Placing subfigures in Latex documents

Sometimes you need to place two or more figures side by side or you just need to include a figure composed of many subfigures. To place the images in your Latex document this way, you can use the subfig package.

\usepackage{subfig}

The command subfloat is used to specify the subfigures.

\begin{figure}[!h]
\centering
\subfloat[NP Server's certificate]{label{fig:gull} \includegraphics[width=0.4\textwidth] {images/npservercert.png}} \qquad
\subfloat[SP Server's certificate]{label{fig:gull} \includegraphics[width=0.4\textwidth] {images/spservercert.png}}
\caption{}
\label{fig:}
\end{figure}

And this is the final result.

subfig Placing subfigures in Latex documents

Source code snippets in Latex

In this post I’m going to show you how to include source code snippets in your Latex documents. To perform the task we’re going to use the listings package.

\usepackage{listings}

First we’re going to define the source code’s layout. We’ll define two different styles to show the capabilities of the package. The first style comes in handy for including source code. The second one is better for including, for instance, console commands. Of course you can define different styles to suit your own needs.

\definecolor{gray92}{gray}{.92}
\definecolor{gray75}{gray}{.75}
\definecolor{gray45}{gray}{.45}
 
\lstdefinestyle{source_code}
{
numbers=left,
stepnumber=5, 
basicstyle=\normalsize, 
captionpos = b, %bottom 
keywordstyle=\color[rgb]{0,0,1}, 
commentstyle=\color[rgb]{0.133,0.545,0.133}, 
stringstyle=\color[rgb]{0.627,0.126,0.941}, 
backgroundcolor=\color{gray92}, 
frame=lrtb, 
framerule=0.5pt, 
linewidth = \textwidth 
}
 
\lstdefinestyle{console} 
{ 
numbers=none, 
basicstyle=\bf\ttfamily, 
backgroundcolor=\color{gray92}, 
frame= lrtb, 
framerule=0.5pt, 
linewidth=\textwidth, 
}

Once the styles are defined there are two ways to include the snippets in the document:

  • Writing the source code directly in the Latex document.
  • Importing the source code file.
\begin{lstlisting}[style=console, caption=A command] 
# gcc -o hello hello.c 
\end{lstlisting}

Note how we can specify the source code’s programming language as well as the listing’s caption:

\lstinputlisting[style=source_code, label = some label, caption=A shell script, language=bash]{example.bash}

The final result looks like this:

picture162 Source code snippets in Latex

You can get the full Latex source code here.

Beautiful tables in Latex

Producing tables is latex is not an easy task. Fortunately I found a very handy macro that transforms Excel tables into Latex code. It is written by Joachim Marder and can be obtained here. If you use OpenOffice you can take a look at calc2latex. With the table creation problem solved, now we can focus on improving the style. We are going to need the following packages:

\usepackage[dvipsnames,usenames]{color} 
\usepackage{array} 
\usepackage{colortbl} 
\usepackage{booktabs}

Next we specify some formatting commands:

%increase the column separation and the cell height 
\renewcommand{tabcolsep}{0.25cm} 
\renewcommand{arraystretch}{2} 
%We can define our custom colors in RGB format 
\definecolor{tableheading}{rgb}{0.82,0.82,0.82} 
\definecolor{softblue}{rgb}{0.8,0.8,1} %Change the bar color 
\arrayrulecolor{ForestGreen}

… and voila! Latex has produced a very stylish table.

picture14 Beautiful tables in Latex

This is the full latex source code:

\documentclass[12pt,a4paper]{report} 
\usepackage[utf8x]{inputenc} 
\usepackage{ucs} 
\usepackage{amsmath} 
\usepackage{amsfonts} 
\usepackage{amssymb} 
\usepackage{makeidx} 
\usepackage[dvipsnames,usenames]{color} 
\usepackage{array} \usepackage{colortbl} 
\usepackage{booktabs} 
\renewcommand{tabcolsep}{0.25cm} 
\renewcommand{arraystretch}{2} 
\definecolor{tableheading}{rgb}{0.82,0.82,0.82} 
%definecolor{totalcolor}{rgb}{1,0.7,0.7} 
%definecolor{firstcolumncolor}{rgb}{0.7,1,0.7} 
\definecolor{softblue}{rgb}{0.8,0.8,1} 
\arrayrulecolor{ForestGreen} 
 
\begin{document} 
% Table generated by Excel2LaTeX from sheet 'material' 
\begin{table}[!h] 
\centering 
\begin{tabular}{ccccc} 
\toprule 
\rowcolor{tableheading} {bf ID} & {bf Description} & {bf Price} & {bf Redeem period} & {bf Redeemed cost} \ 
\midrule 
M1 & PC 1 & 500,00 & 9,00 & 150,00 \ 
\midrule 
M2 & PC 2 & 500,00 & 9,00 & 150,00 \ 
\midrule 
M3 & Server 1 & 1.000,00 & 9,00 & 250,00 \ 
\midrule \textit{Total} &  &  & & multicolumn{1}{>{columncolor{softblue}}c}{550,00} \ 
\bottomrule 
\label{tab:test} 
\end{tabular} 
\caption{Test table} 
\end{table} 
\end{document}