【问题标题】:How to code tables with multi-line cells如何用多行单元格编码表格
【发布时间】:2010-05-24 08:48:55
【问题描述】:

我正在尝试用 LaTeX 写一篇简短的论文,需要添加一个包含 3 列的表格。

+-------------+-----------------+--------------------------------------+
| AAAAAAAAAA  | BBBBBBBBBBBBBBB | Betty Botter Bought a Bit of Butter  |
|             |                 | but the Butter's Bitter              |
+-------------+-----------------+--------------------------------------+
| CCCCCCCC    | DDDD            | Betty Botter Thought:                |
|             |                 | If I Put This Bitter Butter in My    |
|             |                 | Batter it Will Make My Batter Bitter |
+-------------+-----------------+--------------------------------------+

很遗憾,我似乎找不到正确的习惯用法。


我试过了:

\begin{tabular}{lll} 
    AAAAAAAAAA  & BBBBBBBBBBBBBBB & Betty Botter Bought a Bit of Butter but 
    the Butter's Bitter  \\
    CCCCCCCC  & DDDD & Betty Botter Thought: \newline If I Put This Bitter Butter in My Batter it Will Make My Batter Bitter
 \end{tabular}

但 LaTeX 不会在单元格内进行任何换行或格式化。我想我需要告诉它这样做.. 但是怎么做呢?

【问题讨论】:

标签: latex tabular


【解决方案1】:

使用p 列描述符:

改变

\begin{tabular}{lll} 

到

\begin{tabular}{llp{5cm}}

显式插入换行符:

CCCCCCCC  & DDDD & \parbox{5cm}{Betty Botter Thought: \\ If I Put This Bitter Butter in My Batter it Will Make My Batter Bitter}

【讨论】:

  • 有没有办法将 p{} 与其他列描述符之一结合起来?意思是说 p{5cm} 的列应该左对齐、右对齐还是居中对齐?
  • @jja 作为 align-right inside p{} 的解决方法,我在每行的开头使用 \hfill。
  • 可以使用显式插入换行符\newline。
  • 对于其他任何询问居中版本的人:tex.stackexchange.com/a/157400/108304
【解决方案2】:

这是我迄今为止找到的满足我需求的答案:Link here。

它会创建一个新命令,以更恰当的方式在表中创建表:

\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}

所以,如果想在这样的单元格内进行强制换行:

\begin{tabular}{|c|c|c|}
\hline
Foo bar & Foo <forced line break here> bar & Foo bar \\
\hline
\end{tabular}

你最终会使用这样的代码:

Foo bar & \specialcell{Foo\\bar} & Foo bar \\    % vertically centered
Foo bar & \specialcell[t]{Foo\\bar} & Foo bar \\ % aligned with top rule
Foo bar & \specialcell[b]{Foo\\bar} & Foo bar \\ % aligned with bottom rule

可以在新命令的声明中通过将c@更改为l@或r@

来控制水平对齐

所有功劳都来自Tex forum 的egreg。支持他的回答!

【讨论】:

    【解决方案3】:

    正如@aioobe 在他的回答中所写,在这种情况下,可以从左对齐切换

    \begin{tabular}{lll}
    

    到段落对齐,至少在第三列必须手动插入自定义换行符:

    \begin{tabular}{llp{.5\textwidth}}
    

    编辑完成后,可以使用命令\par(而不是\newline)在单元格内实现换行。

    这段代码:

    \documentclass{article}
    
    \begin{document}
    
    \begin{tabular}{llp{.5\textwidth}}
    AAAAAAAAAA & BBBBBBBBBBBBBBB & Betty Botter Bought a Bit of Butter \par but the Butter's Bitter\\
    CCCCCCCC & DDDD & Betty Botter Thought: \par If I Put This Bitter Butter in My \par Batter it Will Make My Batter Bitter\\
    \end{tabular}
    
    \end{document}
    

    产生请求的输出:

    【讨论】:

      【解决方案4】:

      这是一个没有花哨编码的答案。将行写在单独的行中。除了最后一行(行)之外的所有内容都省略了 \hline 它又快又脏,但是,嘿,它确实有效,并且给了我我想要的东西,不管怎样,对于简单的表格。我正在为汽车挡风玻璃做广告。我在每个单元格中有 3 个居中的行

      iTutor Grahamstown
      Mathematics Tutor
      0793296211
      

      我想在我的桌子上重复这个。我只是省略了前两行的 \hline。多个 \hlines 和 '|'是为了使打印输出更容易切割。

      \begin{tabular}{||c||c||c||c||}
      \hline\hline
      
      iTutor Grahamstown &iTutor Grahamstown&iTutor Grahamstown &iTutor Grahamstown \\ %No \hline
      
      Mathematics Tutor & Mathematics Tutor & Mathematics Tutor&Mathematics Tutor \\  %No \hline
      
      0793296211 & 0793296211 & 0793296211 & 0793296211\\ \hline\hline\hline %\hline now
      
      
      \end{tabular}  
      

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-10
        • 1970-01-01
        • 1970-01-01
        • 2012-11-10
        • 1970-01-01
        • 2021-11-20
        相关资源
        最近更新 更多