如何让单元格内容占据多行

如何让单元格内容占据多行

我正在创建一个表格,其中某些单元格包含非常长的文本。对于这些情况,我希望此单元格的内容占用多行。使用默认实现,在构建后,我发现内容被截断。显然这不是我想要实现的。

请查看下面的代码。如您所见,第二列的某些条目太大,无法放在一行中。这也导致第三列永远不会出现在 PDF 文件中。

\documentclass[twoside,11pt]{article}

\begin{document}

\begin{table}[ht]
\caption{Categories and example of attributes}
\label{atts}
\centering
\begin{tabular}{l l p{8.0cm} l}
\hline
Variable & Items & Category \\
\hline
Built-up area  & Inside built-up area; outside built-up area & !!!CAT!!! \\
Type of road  & Highway; district or province road & !!!CAT!!! \\
Type of road lanes  & Road with one road lane; road with separated road lanes & !!!CAT!!! \\
Intersection  & Near intersection; outside intersection & !!!CAT!!! \\
Location characteristic  & Road works; bridge; tunnel; railroad; roundabout & !!!CAT!!! \\
Road factors  & Bad road surface; faulty signals; faulty lighting; road works; queue; downhill; curve; bad visibility & !!!CAT!!! \\
Miscellaneous  & Accident following accident; aquaplaning; sun blinded; school; recreation centre; bus stop; person swung out of vehicle; no safety belt; no helmet; no child seat; cargo on roadway before accident; cargo on roadway because of accident; fire after accident; comments & !!!CAT!!! \\
Total number of deaths  & 0; 1; 2; 3; 4; 5 & !!!CAT!!! \\ [1ex]
\hline
\end{tabular}
\end{table}

~\ref{atts} is bad.

\end{document}

答案1

正如 Peter Grill 提到的,包含 -columntype 的代码p{}运行良好。由于您的表格很长,我使用 -package 创建了一个解决方案longtable,该解决方案在多页上创建表格。您需要运行两次pdflatex或使用任何其他方法才能正确打印。请参阅longtable-package 的文档以获取更多信息。

\documentclass[twoside,11pt]{article}
\usepackage{longtable}
\begin{document}

\begin{longtable}{p{.25\linewidth}p{.45\linewidth}p{1.5cm}}
\caption{Categories and example of attributes}\label{atts}\\\hline
Variable & Items & Category \\\hline
\endfirsthead
\hline
Variable & Items & Category \\\hline
\endhead
Built-up area  & Inside built-up area; outside built-up area & !!!CAT!!! \\
Type of road  & Highway; district or province road & !!!CAT!!! \\
Type of road lanes  & Road with one road lane; road with separated road     lanes & !!!CAT!!! \\
Intersection  & Near intersection; outside intersection & !!!CAT!!! \\
Intersection traffic regulation  & Intersection police officer; intersection; signalized intersection flashing light; intersection traffic signs; intersection priority to right & !!!CAT!!! \\
Location characteristic  & Road works; bridge; tunnel; railroad; roundabout & !!!CAT!!! \\
Road factors  & Bad road surface; faulty signals; faulty lighting; road works; queue; downhill; curve; bad visibility & !!!CAT!!! \\
Miscellaneous  & Accident following accident; aquaplaning; sun blinded; school; recreation centre; bus stop; person swung out of vehicle; no safety belt; no helmet; no child seat; cargo on roadway before accident; cargo on roadway because of accident; fire after accident; comments & !!!CAT!!! \\
Weather conditions  & Normal weather; rain; fog; wind; snow; hail; other weather & !!!CAT!!! \\
Road conditions  & Road surface: dry; wet; snow; clean; dirty & !!!CAT!!! \\
Light conditions  & Daylight; twilight; public lighting; night & !!!CAT!!! \\
Week  & Week; weekend & !!!CAT!!! \\
Day of week  & Monday; Tuesday; Wednesday; Thursday; Friday; Saturday; Sunday & !!!CAT!!! \\
Part of the day  & Morning rush hour (7-–9 a.m.); morning (10-–12 a.m.); afternoon (1-–3 p.m.); evening rush hour (4-–6 p.m.); evening (7-–9 p.m.); night (10-–6 a.m.) & !!!CAT!!! \\
Type of road user  & Car; car double use; minibus; light truck; camper; truck; truck and trailer; truck; tractor; bus; trolleybus; motor coach; motorbike under 400 cm3; motorbike over 400 cm 3; moped A; moped B; moped three to four wheels; bike; span; wheel chair; pedestrian with bike; pedestrian; horseman; other road user & !!!CAT!!! \\
Direction  & Positive way; negative way; transverse way; way not applicable & !!!CAT!!! \\
Movement  & Straight direction; opposite direction; loss control to the left; loss control to the right; left turn; right turn; pass left; pass right; u-turn; drive backwards; car breakdown; standstill opening door; standstill; parking; private property; other movement & !!!CAT!!! \\
Dynamics  & Constant speed; brake; accelerate; standstill & !!!CAT!!! \\
Alcohol  & No alcohol test; refused alcohol test; positive alcohol test; negative alcohol test & !!!CAT!!! \\
Sex road user  & Male road user; female road user & !!!CAT!!! \\
Consequences road user  & Dead road user; seriously injured road user; lightly injured road user; uninjured road user & !!!CAT!!! \\
Age road user  & Age road user 0–-17; 18–-29; 30-–45; 46-–60; over 60 & !!!CAT!!! \\
Condition road user  & Road user in normal condition; drunken road user; sedated road user; ill road user & !!!CAT!!! \\
Factors road user  & Through red light; no priority, over white line; incorrect passing; sidestep maneuver; incorrect position on roadway; loss control steering wheel; no distance; fall & !!!CAT!!! \\
Factors vehicle  & Incorrect vehicle lights; bad tires; flat tire; defect trailer or cargo & !!!CAT!!! \\
Type of collision  & Multiple collision; frontal collision; parallel collision; sideways collision; pedestrian collision; collision obstacle on roadway; collision obstacle outside roadway; collision no obstacle & !!!CAT!!! \\
Type of obstacle  & Animal; train; streetcar; load on roadway; container; road works; street border; speed ramp; excavation; tree; public lighting; post; over crash barrier; against crash barrier; wall; fence; canal; other obstacle & !!!CAT!!! \\
Position pedestrian  & On footpath; pedestrian on cycle track; pedestrian out of vehicle; pedestrian right side roadway; pedestrian left side roadway; zebra crossing with traffic lights; zebra crossing with police officer; zebra crossing; next to zebra crossing with traffic lights; next to zebra crossing with police officer next to zebra crossing; no zebra crossing; pedestrian not moving on roadway & !!!CAT!!! \\
Visibility pedestrian  & Pedestrian visible; pedestrian not visible & !!!CAT!!! \\
Walking distance pedestrian  & Walking distance 1–4 m; walking distance 5–10 m; walking distance 11–15 m; walking distance over 16 m & !!!CAT!!! \\
Position cyclist  & Separated cycle track; marked cycle track on roadway; other cycle track & !!!CAT!!! \\
Cycle track  & One way cycle track; two way cycle track normal direction; one way cycle track opposite direction & !!!CAT!!! \\
Gender passenger  & Male passenger; female passenger & !!!CAT!!! \\
Consequences passenger  & Dead passenger; seriously injured passenger; lightly injured passenger & !!!CAT!!! \\
Position passenger  & Passenger front seat; passenger back seat & !!!CAT!!! \\
Age passenger  & 0–-17; 18–-29; 30-–45; 46-–60; over 60 years old & !!!CAT!!! \\
Gender victim  & male victim; female victim & !!!CAT!!! \\
Age victim  & 0–-17; 18–-29; 30–-45; 46-–60; over 60 years old & !!!CAT!!! \\
Consequences victim  & Dead; seriously injured; lightly injured & !!!CAT!!! \\
Number of road users  & 0; 1; 2; 3; 4; 5; 6; 7; 8 & !!!CAT!!! \\
Number of passengers  & 0; 1; 2; 3; 4; 5  & !!!CAT!!! \\
Number of victims  & 0; 1; 2; 3; 4; 5  & !!!CAT!!! \\
Total number of lightly injured  & 0; 1; 2; 3; 4; 5; 6; 7 & !!!CAT!!! \\
Total number of seriously injured  & 0; 1; 2; 3; 4; 5 & !!!CAT!!! \\
Total number of deaths  & 0; 1; 2; 3; 4; 5 & !!!CAT!!! \\ [1ex]\hline
\end{longtable}

\end{document}

答案2

使用{l p{8.0cm} l}似乎有效:

在此处输入图片描述

笔记:

  • 你应该考虑使用包裹tabularx包,以便您可以轻松地获取\linewidth页面上可用的填充内容。
  • 你也应该看看包裹booktabs以获得更加专业的表格。

代码:

\documentclass[twoside,11pt]{article}

\begin{document}

\begin{table}[ht]
\caption{Categories and example of attributes}
\label{atts}
\centering
\begin{tabular}{l p{8.0cm} l}
\hline
Variable & Items & Category \\
\hline
Built-up area  & Inside built-up area; outside built-up area & !!!CAT!!! \\
Type of road  & Highway; district or province road & !!!CAT!!! \\
Type of road lanes  & Road with one road lane; road with separated road lanes & !!!CAT!!! \\
Intersection  & Near intersection; outside intersection & !!!CAT!!! \\
Location characteristic  & Road works; bridge; tunnel; railroad; roundabout & !!!CAT!!! \\
Road factors  & Bad road surface; faulty signals; faulty lighting; road works; queue; downhill; curve; bad visibility & !!!CAT!!! \\
Miscellaneous  & Accident following accident; aquaplaning; sun blinded; school; recreation centre; bus stop; person swung out of vehicle; no safety belt; no helmet; no child seat; cargo on roadway before accident; cargo on roadway because of accident; fire after accident; comments & !!!CAT!!! \\
Total number of deaths  & 0; 1; 2; 3; 4; 5 & !!!CAT!!! \\ [1ex]
\hline
\end{tabular}
\end{table}

~\ref{atts} is bad.

\end{document}

相关内容