Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a figure that contains two tables, I would like to give format to them.

What would be the best option?

{\footnotesize\begin{figure} [h!tb]
\centering
\begin{tabular} {l }
\hline
\textcolor{sql}{double} N;\\
\textcolor{sql}{double}[] L;\\
\textcolor{sql}{double}[,] Q;\\
\textcolor{sql}{int} d;\\
\textcolor{sql}{double}[,]rho;\\
\textcolor{sql}{public} NLQ(\textcolor{sql}{int} dimension) \{ \\
~~d    = dimension;\\
~~N    = 0.0;\\
~~L    = new \textcolor{sql}{double}[d];\\
~~Q   = new \textcolor{sql}{double}[d, d];\\
\} \\
\hline
\end{tabular}
\begin{tabular} {l }
\hline
\textbf{in}:   $record$, $d$  \\
1 :$n$ += 1\\
2: \textcolor{sql}{For} i=0 to d\\
3: ~~~~$L_i$ += $record_i$\\
4: ~~~~\textcolor{sql}{For} j=0 to i\\
5: ~~~~~~~~$Q_{i,j}$=$Q_{j,i}$= $record_i$*$record_j$ \\
6:~~~~~\textcolor{sql}{End}\\
7:\textcolor{sql}{End}\\
\textbf{out:}  $n$, $L$, $Q$ \\
\hline
\end{tabular}
\caption{NLQ Definition  and Reader Function}
\label{fig:implementNLQ}
\end{figure}
}

enter image description here

maybe something like this

enter image description here

by the way I am using:

\definecolor{sql}{rgb}{0,0,2.55}
\definecolor{sqlC}{rgb}{0,0.44,0}
\definecolor{BRICKRED}{rgb}{0.57,0.0,0.0}
\definecolor{FORESTGREEN}{rgb}{0.0,0.34,0.0}
\definecolor{GOLDENROD}{rgb}{0.75,0.55,0.0}
\definecolor{VIOLET}{rgb}{.7,0.0,0.05}
\definecolor{MIDBLUE}{rgb}{0.08,0.08,1.28}

for the color

share|improve this question
2  
Why are you not using an algorithm package for displaying you algorithm? Then you won't have to do the line numbering and indention manually. Examples for such packages are algorithmic, algorithm2eand algorithmicx. See for example en.wikibooks.org/wiki/LaTeX/Algorithms. –  Jana Jan 28 '13 at 7:42
1  
Instead of {\footnotesize\begin{figure} do \begin{figure}\footnotesize –  Martin Schröder Jan 28 '13 at 8:37
add comment

3 Answers 3

up vote 1 down vote accepted

In addition to David's suggestion, for the line between the two tabulars, you can use the \vline command. To make the tables the same height (so that the bottom \hline line up) you can simply add two empty lines by using \\ twice.

\begin{figure} [h!tb]
\footnotesize
\centering
\begin{tabular}[t] {l }
\hline
\textcolor{sql}{double} N;\\
\textcolor{sql}{double}[] L;\\
\textcolor{sql}{double}[,] Q;\\
\textcolor{sql}{int} d;\\
\textcolor{sql}{double}[,]rho;\\
\textcolor{sql}{public} NLQ(\textcolor{sql}{int} dimension) \{ \\
~~d    = dimension;\\
~~N    = 0.0;\\
~~L    = new \textcolor{sql}{double}[d];\\
~~Q   = new \textcolor{sql}{double}[d, d];\\
\} \\
\hline
\end{tabular}
\vline
\hspace{1pt}
\begin{tabular}[t] {l }
\hline
\textbf{in}:   $record$, $d$  \\
1 :$n$ += 1\\
2: \textcolor{sql}{For} i=0 to d\\
3: ~~~~$L_i$ += $record_i$\\
4: ~~~~\textcolor{sql}{For} j=0 to i\\
5: ~~~~~~~~$Q_{i,j}$=$Q_{j,i}$= $record_i$*$record_j$ \\
6:~~~~~\textcolor{sql}{End}\\
7:\textcolor{sql}{End}\\
\textbf{out:}  $n$, $L$, $Q$ \\
\\
\\
\hline
\end{tabular}
\caption{NLQ Definition  and Reader Function}
\label{fig:implementNLQ}
\end{figure}    
share|improve this answer
add comment

Tables are vertically centred by default, change

\begin{tabular} {l }

to

\begin{tabular}[t] {l }

and they will align on their top line (although a tabular may not be the most convenient structure for laying out code fragments).

share|improve this answer
add comment

I'd use nested tables that will allow automatic placement of the rules.

\documentclass{article}
\usepackage{amsmath,array}
\usepackage{xcolor}
\definecolor{sql}{rgb}{0,0,.55}

\newcommand{\+}[1][1]{\unskip\hspace{#1em}\ignorespaces}
\newcommand{\var}[1]{\textit{#1}}
\newcommand{\key}[1]{\text{#1}}
\newcommand{\peq}{\mathrel{{+}{=}}}

\begin{document}
\begin{figure}[htb]
\footnotesize
\centering
\begin{tabular}{@{}l|l@{}}
\hline
\begin{tabular}[t]{l}
\textcolor{sql}{double} $N$;\\
\textcolor{sql}{double}[\,] $L$;\\
\textcolor{sql}{double}[\,,\,] $Q$;\\
\textcolor{sql}{int} $d$;\\
\textcolor{sql}{double}[\,,\,] \var{rho};\\
\textcolor{sql}{public} NLQ(\textcolor{sql}{int} dimension) \{ \\
\+ \begin{tabular}{@{} >{$}r<{$} @{}>{${}}l<{$} @{}}
   d &= \key{dimension};\\
   N &= 0.0;\\
   L &= \key{new \textcolor{sql}{double}}[d];\\
   Q &= \key{new \textcolor{sql}{double}}[d, d];
   \end{tabular}\\
\}
\end{tabular}
&
\begin{tabular}[t]{l}
\textbf{in}:   \var{record}, $d$  \\
1: $n \peq 1$\\
2: \textcolor{sql}{For} $i=0$ to $d$\\
3: \+ $L_i \peq \var{record}_i$\\
4: \+ \textcolor{sql}{For} $j=0$ to $i$\\
5: \+[2] $Q_{i,j}=Q_{j,i}=\var{record}_i * \var{record}_j$ \\
6: \+ \textcolor{sql}{End}\\
7: \textcolor{sql}{End}\\
\textbf{out:}  $n$, $L$, $Q$
\end{tabular}
\\
\hline
\end{tabular}
\caption{NLQ Definition  and Reader Function}
\label{fig:implementNLQ}
\end{figure} 
\end{document}

Note how math is properly segregated to give optimal spacing (and also uniform appearance to the symbols).

The setting of your sql color is dubious, as the values in an rgb specification are normalized to 1 as a maximum, so you're actually declaring pure blue, so I've changed it.

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.