格式化图形内的两个表格

格式化图形内的两个表格

我有一个包含两个表格的图,我想为它们指定格式。

最好的选择是什么?

{\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}
}

在此处输入图片描述

也许像这样

在此处输入图片描述

顺便说一下,我正在使用:

\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}

颜色

答案1

除了 David 的建议之外,对于两个表格之间的线条,您可以使用命令\vline。要使表格的高度相同(以便底部\hline线条向上),您只需使用\\两次添加两个空行即可。

\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}    

答案2

我将使用嵌套表来允许自动放置规则。

\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}

请注意如何正确分离数学以提供最佳间距(以及符号的统一外观)。

您的颜色设置sql令人怀疑,因为规范中的值rgb被标准化为最大值 1,所以您实际上声明的是纯蓝色,所以我对其进行了更改。

在此处输入图片描述

答案3

表格默认垂直居中,更改

\begin{tabular} {l }

\begin{tabular}[t] {l }

并且它们会在顶行对齐(尽管表格可能不是布局代码片段最方便的结构)。

相关内容