[英]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. 添加\\vline
并使用\\vline
usepackage{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}
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. 使用[H]
ere浮点说明符可以使算法的位置与代码保持一致。 This effectively removes the floating capability. 这有效地消除了浮动功能。 Needs the float
package . 需要float
包 。
tabularx
stretches the table to a specified width using an X
-column. tabularx
使用X
列将表格拉伸到指定宽度。 Also removed the space around the X
-column using @{}
so the algorithm is flush with the column edges. 还使用@{}
删除X
列周围的空间,以使算法与列边缘齐平。
booktabs
provides a neat layout of the lines and vertical spacing. booktabs
提供了线条和垂直间距的整齐布局。
amsmath
's \\dots
provides the best spacing around \\dots
, depending on the instance it's used in. Avoid using ...
to denote ellipses. amsmath
的\\dots
提供了最佳的间距各地\\dots
,取决于它在使用的实例。避免使用...
来表示椭圆。
Stretching of the display operator within a text-style use of math mode is achieved via \\displaystyle
. 通过\\displaystyle
可以在数学样式的文本样式使用中扩展显示运算符。 It does stretch the line height though. 它确实会延长线的高度。
mleftright
's \\mleft
... \\mright
pairs provide better spacing around the delimiters than the traditional \\left
... \\right
. mleftright
的\\mleft
... \\mright
对在定界符周围提供了比传统\\left
... \\right
更好的间距。
Vertical rules stem from nested tabular
s using a natural column widths and a forced indentation of width \\tabcolsep
. 垂直规则源自使用自然列宽度和宽度\\tabcolsep
的强制缩进的嵌套tabular
s。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.