我怎样才能使第一列与第二列和第三列逐行对齐?

我怎样才能使第一列与第二列和第三列逐行对齐?

这个程序的一部分使用宏来输入伪代码。但是宏没有提供在右端输入注释的制表符。我自己使用表格环境做了制表符注释(显示了两个版本)。但这两列不对齐。我该如何对齐它们?

\documentclass{book}

\usepackage{clrscode3e} % Macro by Cormen for typing algorithm in his book
\usepackage{multirow}
\begin{document}


\title{check}

\author{author}
\maketitle


\begin{tabular}{lll}
\multirow{9}{*}{
\begin{minipage}[]{9cm}
\begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$} 
\li \For $j \gets 2$ \To $\attrib{A}{length}$ 
\li \Do
$\id{key} \gets A[j]$ 
\li \Comment Insert $A[j]$ into the sorted sequence.
\li $i \gets j-1$
\li \While $i > 0$ and $A[i] > \id{key}$
\li \Do
$A[i+1] \gets A[i]$
\li $i \gets i-1$
\End
\li $A[i+1] \gets \id{key}$
\End
\end{codebox}
\end{minipage}
}
& \begin{minipage}[]{4em}\vskip 3ex cost \end{minipage}   & \begin{minipage}[]{5em}\vskip 3ex times \end{minipage} \\
&  $c_1$  & $n$ \\
&  $0$    & $n-1$ \\
&  $c_4$  & $n-1$ \\
&  $c_5$  & $\sum_{j=2}^n t_j$\\
&  $c_6$  & $\sum_{j=2}^n t_j-1$\\
&  $c_7$  & $\sum_{j=2}^n t_j-1$\\
&  $c_8$  & $n-1$
\end{tabular}

\vskip 10ex

\begin{tabular}{lll}
\begin{minipage}[]{9cm}
\begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$} 
\li \For $j \gets 2$ \To $\attrib{A}{length}$ 
\li \Do
$\id{key} \gets A[j]$ 
\li \Comment Insert $A[j]$ into the sorted sequence.
\li $i \gets j-1$
\li \While $i > 0$ and $A[i] > \id{key}$
\li \Do
$A[i+1] \gets A[i]$
\li $i \gets i-1$
\End
\li $A[i+1] \gets \id{key}$
\End
\end{codebox}
\end{minipage}
& 
\begin{minipage}[]{4ex}
cost \\
$c_1$ \\
$0$ \\
$c_4$ \\
$c_5$ \\
$c_6$ \\
$c_7$ \\
$c_8$ 
\end{minipage}
& \begin{minipage}[]{5em}times \\
 $n$ \\
 $n-1$ \\
 $n-1$ \\
 $\sum_{j=2}^n t_j$\\
 $\sum_{j=2}^n t_j-1$\\
 $\sum_{j=2}^n t_j-1$\\
 $n-1$
\end{minipage} 
\end{tabular}



\end{document}

答案1

在此处输入图片描述

两个表格必须具有相同的列设计。为此,我建议使用tabularx表格环境。使用它后,minipages 就不再是必要的了。您还应该注意multirow单元格中的行数和它跨越的单元格数,它们应该相等。除此之外,还需要考虑上方的垂直空间\begin{codebox}。它可以被删除,\vspace*{-1.5\baselineskip}以便在表格行之间留出更多空间,我添加了包makecell并使用它们的宏\setcellgapes{2pt}\makegapedcells

\documentclass{book}
\usepackage{clrscode3e} % Macro by Cormen for typing algorithm in his book
\usepackage{makecell, multirow, tabularx}
\setcellgapes{1pt}
\usepackage{amsmath}


\title{check}
\author{author}

\begin{document}
\maketitle

{ % for make a group in which is valid \makegapedcells
\makegapedcells
\noindent\begin{tabularx}{\linewidth}{@{} X >{$}l<{$} >{$}p{22mm}<{$}
                            @{}}
\multirow[t]{9}{=}{\vspace*{-1.5\baselineskip} % <-- remove top vertical space of `codebox`
    \begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$}
\li \For $j \gets 2$ \To $\attrib{A}{length}$
\li \Do
$\id{key} \gets A[j]$
\li \Comment Insert $A[j]$ into the sorted sequence.
\li $i \gets j-1$
\li \While $i > 0$ and $A[i] > \id{key}$
\li \Do
$A[i+1] \gets A[i]$
\li $i \gets i-1$
\End
\li $A[i+1] \gets \id{key}$
\End
\end{codebox}
            }
    &   \text{cost} &   times               \\
    &   c_1         &   n                   \\
    &   0           & n-1                   \\
    &  c_4          & n-1                   \\
    &  c_5          & \sum_{j=2}^n t_j      \\
    &  c_6          & \sum_{j=2}^n t_j-1    \\
    &  c_7          & \sum_{j=2}^n t_j-1    \\
    &  c_8          & n-1                   \\
\end{tabularx}

\vskip 10ex
\noindent\begin{tabularx}{\linewidth}{@{} X >{$}l<{$} >{$}p{22mm}<{$}
                                      @{}}
\multirow[t]{9}{=}{\vspace*{-1.5\baselineskip} % <-- remove top vertical space of `codebox`
    \begin{codebox}
\Procname{$\proc{Insertion-Sort}(A)$}
\li \For $j \gets 2$ \To $\attrib{A}{length}$
\li \Do
$\id{key} \gets A[j]$
\li \Comment Insert $A[j]$ into the sorted sequence.
\li $i \gets j-1$
\li \While $i > 0$ and $A[i] > \id{key}$
\li \Do
$A[i+1] \gets A[i]$
\li $i \gets i-1$
\End
\li $A[i+1] \gets \id{key}$
\End
\end{codebox}
            }
    &   \text{cost} &   times               \\
    &   c_1         &   n                   \\
    &   0           & n-1                   \\
    &  c_4          & n-1                   \\
    &  c_5          & \sum_{j=2}^n t_j      \\
    &  c_6          & \sum_{j=2}^n t_j-1    \\
    &  c_7          & \sum_{j=2}^n t_j-1    \\
    &  c_8          & n-1
\end{tabularx}
} % end of group
\end{document}

相关内容