![在保持正確格式的同時,在表格儲存格中強制換行的好方法是什麼?](https://rvso.com/image/475726/%E5%9C%A8%E4%BF%9D%E6%8C%81%E6%AD%A3%E7%A2%BA%E6%A0%BC%E5%BC%8F%E7%9A%84%E5%90%8C%E6%99%82%EF%BC%8C%E5%9C%A8%E8%A1%A8%E6%A0%BC%E5%84%B2%E5%AD%98%E6%A0%BC%E4%B8%AD%E5%BC%B7%E5%88%B6%E6%8F%9B%E8%A1%8C%E7%9A%84%E5%A5%BD%E6%96%B9%E6%B3%95%E6%98%AF%E4%BB%80%E9%BA%BC%EF%BC%9F.png)
我研究了以下線程中提供的一些不同的解決方案,以了解如何在表格單元格中強制換行:
基於此,我然後編譯了一個由一些建議的換行方法組成的表格,以查看它們如何與我在序言中列出的表格格式互動:
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
% Must have features for my tables
\usepackage{array}
\setlength{\tabcolsep}{18pt}
\setlength{\arrayrulewidth}{0.5mm}
\renewcommand{\arraystretch}{1.5}
% ________________________________
\usepackage[usestackEOL]{stackengine}
% Package for \strutlongstacks{T} forced linebreak method
\usepackage{makecell}
% Package for \makecell forced linebreak method
\begin{document}
\begin{table}[h]
\centering
\strutlongstacks{T}
\begin{tabular}{|c|c|m{3cm}|}
\hline
\multicolumn{3}{|c|}{Calender for the rest of the year} \\
\hline
\textbf{Date} & \textbf{Homework} & \textbf{Goal} \\
\hline
Week 1 & Homework 1 and 2 & \begin{tabular}[c]{@{}c@{}}Goal for 1 is to... \\ Goal for 2 is to... \end{tabular} \\ % Text exceeds cell when limit is reached
\hline
Week 2 & Homework 3 and 4 & \vtop{\hbox{\strut Goal for 3 is to...}\hbox{\strut Goal for 4 is to...}} \\ % Force elongates cell on itself when limit is exceeded
\hline
Week 3 & Homework 5 and 6 & \Centerstack{Goal for 5 is to... \\ Goal for 6 is to...} \\ % Text exceeds cell when limit is reached
\hline
Week 4 & Homework 7 and 8 & \begin{flushleft} Goal for 7 is to... \\ Goal for 8 is to... \end{flushleft} \\ % Works for long text, too, only for column types of "|m{}|", though, not for regular "|c|"
\hline
Week 5 & Homework 9 and 10 & \parbox{3cm}{Goal for 9 is to... \\ Goal for 10 is to...} \\ % Works but completely ignores the \renewcommand{\arraystretch}{1.5} in the preamble
\hline
Week 6 & Homework 11 and 12 & \makecell{Goal for 11 is to... \\ Goal for 12 is to...} \\ % Text exceeds cell when limit is reached
\hline
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}
我喜歡目標 1 和 2 的顯示方式,並補充我在序言中的設置,因此我理想地希望是否可以對其進行修改,以便當我m{3cm}
在第三列中使用時,第二列仍然像家庭作業一樣居中當文字超出為儲存格提供的限制時,7 和 8 AND 也會自動換行。
如果你們能想到任何方法來優化其中任何一個以正確格式化或提出新的創意方法,如果你們在這裡分享它們,我們將不勝感激:)
答案1
據我了解,您的問題是獲得一個固定大小的多行列,垂直居中。
您可以在 中使用固定大小\makecell
。例子:
\makecell[c{p{3cm}}]{Goal for 1 is to... \\ Goal for 2 is to...}
將建立一個垂直居中的單元格,其固定寬度為3cm
。在您的情況下,由於寬度是在列規範中設定的,因此請替換3cm
為\hsize
(它將採用列的 with )。但為了獲得正確的垂直位置,您必須讓\makecell
處理它並使用p{3cm}
而不是m{3cm}
在列規範中。
還添加
\renewcommand{\cellset}{\def\arraytretch{1.5}}%
如果你想要更大的內在空間\makecell
。
完整範例:
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
% Must have features for my tables
\usepackage{array}
\usepackage{makecell}
\setlength{\tabcolsep}{18pt}
\setlength{\arrayrulewidth}{0.5mm}
\renewcommand{\arraystretch}{1.5}
\renewcommand{\cellset}{\def\arraytretch{1.5}}%
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|c|c|p{3cm}|}
\hline
\multicolumn{3}{|c|}{Calender for the rest of the year} \\
\hline
\textbf{Date} & \textbf{Homework} & \textbf{Goal} \\
\hline
\hline
Week 1 & Homework 1 and 2
& \makecell[c{p{\hsize}}]{Goal for 1 is to... lot of things that take many lines \\
Goal for 2 is to... lot of things that take many lines} \\
\hline
Week 2 & Homework 3 and 4
& \makecell[c{p{\hsize}}]{Goal for 3 is to... lot of things that take many lines \\
Goal for 4 is to... lot of things that take many lines} \\
\hline
Week 3 & Homework 5 and 6
& \makecell[c{p{\hsize}}]{Goal for 5 is to... lot of things that take many lines \\
Goal for 6 is to... lot of things that take many lines} \\
\hline
\end{tabular}
\caption{With \textsf{makecell}}
\label{tab:my_label}
\end{table}
\end{document}
無論如何,現在有表格陣列包,它透過現代鍵值介面為表提供許多自訂,並複製經典表包的功能。使用,可以使用(是 所使用的列說明符)來設定tabularray
垂直居中的寬度列,並且可以使用大括號簡單地獲得多行單元格:3cm
Q[3cm,m]
Q
tabularray
Week 1 & Homework 1 and 2 & {Goal for 1 is to...\\ Goal for 2 is to...} \\
範例(看看它如何更清晰,格式和內容分開):
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
% Must have features for my tables
\usepackage{tabularray}
\begin{document}
\begin{table}[h]
\centering
\begin{tblr}
{
colspec={ | Q[c,m] | Q[c,m] | Q[3cm,m] |},
hline{1-Z} = {1}{-}{0.5mm, solid},
hline{3} = {2}{-}{0.5mm, solid},
vlines={0.5mm},
row{2} = {font=\bfseries},
colsep=18pt,
stretch=1.5,
}
\SetCell[c=3]{c} Calender for the rest of the year
\\
Date & Homework & Goal
\\
Week 1 & Homework 1 and 2
& {Goal for 1 is to... lot of things that take many lines \\[6pt]
Goal for 2 is to... lot of things that take many lines}
\\
Week 2 & Homework 3 and 4
& {Goal for 3 is to... lot of things that take many lines \\[6pt]
Goal for 4 is to... lot of things that take many lines}
\\
Week 3 & Homework 5 and 6
& {Goal for 5 is to... lot of things that take many lines \\[6pt]
Goal for 6 is to... lot of things that take many lines}
\end{tblr}
\caption{With \textsf{tabularray}}
\label{tab:my_label}
\end{table}
\end{document}
答案2
我認為你不需要任何花哨的東西......除了為了\tabularnewline
結束行,因為\\
在最後一列。
\documentclass{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{array}
\begin{document}
\begin{table}[htp]
\centering
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{|c|c|>{\raggedright}m{5cm}|}
\hline
\multicolumn{3}{|c|}{Calendar for the rest of the year} \tabularnewline
\hline
\textbf{Date} & \textbf{Homework} & \multicolumn{1}{c|}{\textbf{Goal}} \tabularnewline
\hline
Week 1 & Homework 1 and 2 &
Goal for 1 is to... lot of things that take many lines \\[6pt]
Goal for 2 is to... lot of things that take many lines \tabularnewline
\hline
Week 2 & Homework 3 and 4 &
Goal for 3 is to... lot of things that take many lines \\[6pt]
Goal for 4 is to... lot of things that take many lines \tabularnewline
\hline
Week 3 & Homework 5 and 6 &
Goal for 5 is to... lot of things that take many lines \\[6pt]
Goal for 6 is to... lot of things that take many lines
Goal for 1 is to... \tabularnewline
\hline
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}
替代佈局:
\documentclass{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{array,booktabs}
\begin{document}
\begin{table}[htp]
\centering
\begin{tabular}{@{}cc>{\raggedright}p{5cm}@{}}
\toprule
\multicolumn{3}{c}{Calendar for the rest of the year} \tabularnewline
\midrule
\textbf{Date} & \textbf{Homework} & \multicolumn{1}{c}{\textbf{Goal}} \tabularnewline
\midrule
Week 1 & Homework 1 and 2 &
Goal for 1 is to... lot of things that take many lines \\[6pt]
Goal for 2 is to... lot of things that take many lines \tabularnewline
\addlinespace
Week 2 & Homework 3 and 4 &
Goal for 3 is to... lot of things that take many lines \\[6pt]
Goal for 4 is to... lot of things that take many lines \tabularnewline
\addlinespace
Week 3 & Homework 5 and 6 &
Goal for 5 is to... lot of things that take many lines \\[6pt]
Goal for 6 is to... lot of things that take many lines
Goal for 1 is to... \tabularnewline
\bottomrule
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}