如何在 tabularx 中的多行中自动换行?

如何在 tabularx 中的多行中自动换行?

我希望表格适合页面,但默认情况下表格太宽。我已经尝试过了tabularx,但现在使用 的行multirow不够高。

这是我现在的代码:

\begin{table}[h]
\begin{tabularx}{\textwidth}{|l|l|l|X|}
\hline
Header Field & IPv6 length & \multicolumn{1}{r|}{HC1 length} & Description \\ \hline
Version & 4 bits & \multicolumn{1}{r|}{0 bits} & Assuming IPv6 \\ \hline
Traffic Class & 8 bits & \multicolumn{1}{r|}{\multirow{2}{*}{1 bit}} & \multirow{2}{\hsize}{\parbox{\linewidth}{Traffic Class and Flow Label are compressed to a single bit when their values are both zero}} \\ \cline{1-2}
Flow Label & 20 bits & \multicolumn{1}{r|}{} & \\ \hline
Payload Length & 16 bits & 0 bits & Derived from the 802.15.4 frame or fragmentation header \\ \hline
Next Header & 8 bits & 2 bits & Compressed when packet uses UDP or TCP \\ \hline
Hop Limit & 8 bits & 8 bits & Not Compressed \\ \hline
Source Address & \multirow{2}{*}{128 bits} & \multirow{2}{*}{2 bits} & \multirow{2}{\hsize}{The 64-bit interface identifier (IID) for both source and destination addresses are removed if the destination can derive them from the corresponding link-layer address in the 802.15.4 frame or mesh header.} \\ \cline{1-1}
Destination Address & & & \\ \hline
HC2 encoding & 0 bits & 1 bit & The HC2 bit indicates if the next header is compressed using HC2. \\ \hline
\end{tabularx}
\end{table}

它看起来是这样的: 分享Latex 结果

我使用 ShareLatex。

我该如何修复这个问题?

答案1

如果\textwidth太小,甚至tabularx不能提供太多帮助。

这个主张展示了实现目标的不同技术,但当然可能会有更好的技术。

我做到了不是解决表中行太多的问题!

\documentclass{article}

\usepackage[lmargin=1.5cm,rmargin=1.5cm]{geometry}
\usepackage{multirow}


\usepackage{tabularx}%

\begin{document}

\begin{table}[h]
\begin{tabularx}{\textwidth}{|l|l|l|X|}
\hline
Header Field & IPv6 length & \multicolumn{1}{r|}{HC1 length} & Description \\ \hline
Version & 4 bits & \multicolumn{1}{r|}{0 bits} & Assuming IPv6 \\ \hline
Traffic Class & 8 bits & \multicolumn{1}{r|}{\multirow{2}{*}{1 bit}} \tabularnewline\cline{1-2}
Flow Label & 20 bits & & \multirow{-2}{\hsize}{\parbox{\linewidth}{Traffic Class and Flow Label are compressed to a single bit when their values are both zero}} \tabularnewline 
& & & \tabularnewline
& & & \tabularnewline
\hline
Payload Length & 16 bits & 0 bits & Derived from the 802.15.4 frame or fragmentation header \\ \hline
Next Header & 8 bits & 2 bits & Compressed when packet uses UDP or TCP \\ \hline
Hop Limit & 8 bits & 8 bits & Not Compressed \\ \hline

\multirow{2}{*}{Source Address} & \multirow{5}{*}{128 bits} & \multirow{5}{*}{2 bits} & \multirow{5}{\hsize}{The 64-bit interface identifier (IID) for both source and destination addresses are removed if the destination can derive them from the corresponding link-layer address in the 802.15.4 frame or mesh header.}  \tabularnewline
& & & \tabularnewline
\cline{1-1}

\multirow{2}{*}{Destination Address} & & & \tabularnewline
& & & \tabularnewline
& & & \tabularnewline
\hline
HC2 encoding & 0 bits & 1 bit & The HC2 bit indicates if the next header is compressed using HC2. \\ \hline
\end{tabularx}
\end{table}

\end{document}

在此处输入图片描述

相关内容