如何对具有多行单元格的表格进行编码

发布于 2024-09-03 05:18:08 字数 1069 浏览 10 评论 0原文

我正在尝试用 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 不会在单元格内进行任何换行或格式化。我想我需要告诉它这样做......但是怎么做呢?

I am trying to write a short paper with LaTeX and need to add a table with 3 columns.

+-------------+-----------------+--------------------------------------+
| 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 |
+-------------+-----------------+--------------------------------------+

Unfortunately I can't seem to find the correct idiom to do it.


I tried:

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

But LaTeX doesn't do any linebreaks or formatting within the cell. I assume I need to tell it to do so.. But how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甚是思念 2024-09-10 05:18:08

使用 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}

Use the p column descriptor:

Change

\begin{tabular}{lll} 

to

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

To explicitly insert line-breaks:

CCCCCCCC  & DDDD & \parbox{5cm}{Betty Botter Thought: \\ If I Put This Bitter Butter in My Batter it Will Make My Batter Bitter}
浅黛梨妆こ 2024-09-10 05:18:08

这是我到目前为止找到的满足我需求的答案: 链接在这里

它创建一个新命令,以更正确的方式在表格中创建表格:

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

所有功劳均归 egreg 来自 Tex 论坛。为他的回答点赞吧!

This is the answer I found so far for my needs: Link here.

It creates a new command that will make a table inside a table in a more proper way:

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

So, if want to do a forced line break inside a cell like here:

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

You will end up using a code like this:

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

Horizontal alignment can be controlled in the declaration of the new command by changing c@ to l@ or r@

All credit goes to egreg from the Tex forum. Do upvote his answer !

如梦亦如幻 2024-09-10 05:18:08

正如 @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}

产生请求的输出:

输出屏幕截图

As @aioobe wrote in his answer, in this case one can switch from the left alignment

\begin{tabular}{lll}

to the paragraph alignment, at least in the third column where the custom linebreak must be inserted manually:

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

After this edit, one can use the command \par (instead of \newline) to implement the linebreak within the cell.

This code:

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

produces the output requested:

screenshot of output

风苍溪 2024-09-10 05:18:08

这是一个没有花哨编码的答案。将各行写在单独的行中。省略除最后一行(行)之外的所有内容的 \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}  

我希望这会有所帮助。

Here is an answer with no fancy coding. Write your rows in separate lines. Omit the \hline for all but the last row(line) Its quick and dirty but, hey, it works and gives me exactly what I want, for simple tables anyway. I was making advertising to go on automobile windscreens. I have 3 centered rows in each cell

iTutor Grahamstown
Mathematics Tutor
0793296211

I wanted this repetitively in my table. I just left out the \hline for the first two rows. The multiple \hlines and '|' are to make cutting up the printout easier.

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

I hope that this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文