简体   繁体   中英

Vertical lines and width table latex

I'm replicating a table of an article, I need the table is the width of the text of the sheet and add a vertical lines below the word for , I attach my code.

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

在此处输入图片说明

The result I expect is the one in the superior image. Any idea.

Add \\vline and usepackage{tabularx} for width text.

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

If you just want to replicate the algorithm display, a table would probably suffice:

在此处输入图片说明

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

Some considerations:

  • Used the [H] ere float specifier to maintain the position of the algorithm in-line with the code. This effectively removes the floating capability. Needs the float package .

  • tabularx stretches the table to a specified width using an X -column. Also removed the space around the X -column using @{} so the algorithm is flush with the column edges.

  • booktabs provides a neat layout of the lines and vertical spacing.

  • amsmath 's \\dots provides the best spacing around \\dots , depending on the instance it's used in. Avoid using ... to denote ellipses.

  • Stretching of the display operator within a text-style use of math mode is achieved via \\displaystyle . It does stretch the line height though.

  • mleftright 's \\mleft ... \\mright pairs provide better spacing around the delimiters than the traditional \\left ... \\right .

  • Vertical rules stem from nested tabular s using a natural column widths and a forced indentation of width \\tabcolsep .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM