【问题标题】:Vertical lines and width table latex垂直线和宽度表乳胶
【发布时间】:2018-04-02 18:55:44
【问题描述】:

我正在复制一篇文章的表格,我需要表格是表格的文字宽度并在for字下方添加一个竖线,我附上我的代码。

% Please add the following required packages to your document preamble:
% \usepackage{booktabs}

\begin{table}[h]
\centering
\label{my-label}
\begin{tabular}{@{}l@{}}
\toprule
\textbf{Algorithm 2:} The Forward algorithm \\ \midrule
\textbf{Initialization:} \\
\ $\alpha_1(i) =  \pi_i bi(O_1), \  1 \leq i \leq K$ \\
\\
\textbf{Recursion:} \\
\textbf{for} t = 1,..., T-1 \textbf{do} \\
\ \ \ \textbf{for} j = 1,..., K do \\
\ \ \ \ \ \ \ $\alpha_{t+1}(j) = \left [\sum_{i=1}^{k} \alpha_t (i) \alpha_{ij},\right ] b_j(O_{t+1})$ \\
\ \ \ \textbf{end} \\
\textbf{end} \\
\\
\textbf{Result:} $P(O_{1:T}) = \sum_{i=1}^N \alpha_T (i)$ \\ \bottomrule
\end{tabular}
\end{table}

我期望的结果是上乘图像中的结果。任何想法。

【问题讨论】:

    标签: latex


    【解决方案1】:

    为宽度文本添加 \vlineusepackage{tabularx}

       \begin{table}[h]
        \centering
        \label{my-label}
        \begin{tabularx}{\textwidth}{X}
        \toprule
        \textbf{Algorithm 2:} The Forward algorithm \\ \midrule
        \textbf{Initialization:} \\
        \ $\alpha_1(i) =  \pi_i bi(O_1), \  1 \leq i \leq K$ \\
        \\
        \textbf{Recursion:} \\
        \textbf{for} $t = 1,..., T-1$ \textbf{do} \\
        \ \ \vline \ \ \textbf{for} $j = 1,..., K$ \textbf{do} \\
        \ \ \vline \ \ \ \ \vline \ \ $\alpha_{t+1}(j) = \left [\sum_{i=1}^{k} \alpha_t (i) \alpha_{ij},\right ] b_j(O_{t+1})$ \\
        \ \ \vline \ \ \textbf{end} \\
        \textbf{end} \\
        \\
        \textbf{Result:} $P(O_{1:T}) = \sum_{i=1}^N \alpha_T (i)$ \\ \bottomrule
        \end{tabularx}
        \end{table}
    

    【讨论】:

      【解决方案2】:

      如果您只是想复制算法显示,一个表格可能就足够了:

      \documentclass{article}
      
      \usepackage{float,tabularx,booktabs,amsmath,mleftright}
      \usepackage{lipsum}
      
      \begin{document}
      
      \sloppy % Just for this example
      \lipsum[1]
      
      \begin{table}[H]
        \begin{tabularx}{\textwidth}{ @{} X @{} }
          \toprule
          \textbf{Algorithm 2:} The Forward algorithm \\
          \midrule
          \textbf{Initialization:} \\
          \ $\alpha_1(i) = \pi_i b_i(O_1), \  1 \leq i \leq K$ \\
          \\
          \textbf{Recursion:} \\
          \textbf{for} $t = 1, \dots, T - 1$ \textbf{do} \\
            \begin{tabular}{ @{\hspace{\tabcolsep}} | l }
              \textbf{for} $j = 1, \dots, K$ \textbf{do} \\
              \begin{tabular}{ @{\hspace{\tabcolsep}} | l }
                $\displaystyle \alpha_{t + 1}(j) = \mleft[ \sum_{i = 1}^k \alpha_t (i) \alpha_{i j} \mright] b_j(O_{t + 1})$ \\
              \end{tabular} \\
              \textbf{end}
            \end{tabular} \\
          \textbf{end} \\
          \\
          \textbf{Result:} $\displaystyle P(O_{1:T}) = \sum_{i = 1}^N \alpha_T(i)$ \\
          \bottomrule
        \end{tabularx}
      \end{table}
      
      \lipsum[2]
      
      \end{document}
      

      一些注意事项:

      • 使用 [H]ere 浮点说明符来保持算法的位置与代码一致。这有效地消除了浮动能力。需要float package

      • tabularx 使用X 列将表格拉伸到指定宽度。还使用@{} 删除了X 列周围的空间,因此算法与列边缘齐平。

      • booktabs 提供了整齐的线条布局和垂直间距。

      • amsmath\dots\dots 周围提供最佳间距,具体取决于使用它的实例。避免使用... 来表示省略号。

      • 通过\displaystyle 在数学模式的文本样式使用中拉伸显示运算符。不过它确实会拉伸行高。

      • mleftright\mleft...\mright 对在分隔符周围提供比传统的 \left...\right 更好的间距。

      • 垂直规则源自嵌套的tabulars,使用自然列宽和强制缩进宽度\tabcolsep

      【讨论】: