目前我的代码如下所示:
\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\begin{document}
\begin{minipage}{\linewidth}
\subsubsection*{Finish Private Chat Success}
\centering
\begin{tabular}{|p{0.3\linewidth}|p{0.3\linewidth}|p{0.1\linewidth}|p{0.1\linewidth}|p{0.2\linewidth}|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & Something & Date & Word Word\\
\hline
\end{tabular}
\end{minipage}
\end{document}
我的问题是,表格不适合页面的宽度,我假设线宽是几何体之间的空间left
。right
但事实似乎并非如此,我已经尝试使用 tablex 包来做到这一点,但也没有成功。使用 minipage 的解决方案似乎最适合我的需求。我想知道如何计算每列的宽度才能真正适合文本区域的宽度。
提前谢谢你的帮助。
答案1
我不会手动计算允许的列宽,而是加载包tabularx
并让它计算出宽度。假设您希望相对(可用)列宽的比例为 3:3:1:1:2,您可以按照以下代码所示的方式实现此目标。
此外,我还会指示 LaTeX 将材料排版在窄列中,使其右侧不对齐,而不是完全对齐。
\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[vmargin=1.5cm,hmargin=2cm]{geometry}
\usepackage{tabularx,ragged2e}
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash%
\hspace{0pt}% % to allow hyphenation of first word in a cell
\hsize=#1\hsize}X}
\setlength\extrarowheight{2pt} % for a less-cramped "look"
\begin{document}
\subsubsection*{Finish Private Chat Success}
\noindent
%% 1.5+1.5+0.5+0.5+1.0=5 = number of columns of type "X"
\begin{tabularx}{\textwidth}{| *{2}{L{1.5}|} *{2}{L{0.5}|} L{1.0}|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here
& And some long text here that is supposed to have automatic line breaks in this cell
& & Date & Word Word\\
\hline
Some text here
& And some long text here that is supposed to have automatic line breaks in this cell
& Something & Date & Word Word\\
\hline
\end{tabularx}
\end{document}
答案2
问题中的代码没有考虑每列文本左右两侧的水平空白以及垂直线的宽度。因此整个表格的宽度加起来超过了文本宽度。
为了正确计算宽度以使表格与文本宽度完全一样宽,您可以使用包calc
和\tabcolsep
(空白宽度)和\arrayrulewidth
(垂直线宽度),如下面的 MWE 所示:
\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\usepackage{calc}
\usepackage{array}
\begin{document}
\noindent
\begin{tabular}{|p{0.3\textwidth-2\tabcolsep-1.2\arrayrulewidth}|p{0.3\textwidth-2\tabcolsep-1.2\arrayrulewidth}|p{0.1\textwidth-2\tabcolsep-1.2\arrayrulewidth}|p{0.1\textwidth-2\tabcolsep-1.2\arrayrulewidth}|p{0.2\textwidth-2\tabcolsep-1.2\arrayrulewidth}|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & text & Date & Word Word\\
\hline
\end{tabular}
\end{document}
如果您更喜欢自动化程度更高的方法,该tabularx
包可能会很有用。您可以使用其灵活宽度的X
列,如以下 MWE 所示:
\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|l|X|l|l|l|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & text & Date & Word Word\\
\hline
\end{tabularx}
\end{document}
如果您坚持在代码中使用精确的比例,您可能还会对该tabu
包感兴趣,它提供了易于使用的语法,如下面的 MWE 所示:
\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\usepackage{tabu}
\begin{document}
\noindent
\begin{tabu}to \textwidth{|X[3]|X[3]|X|X|X[2]|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & text & Date & Word Word\\
\hline
\end{tabu}
\end{document}
答案3
tabularx
对规则进行了一些改进:
\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, vmargin=1.5cm, hmargin=2cm, showframe]{geometry}
\usepackage{tabularx, makecell}
\begin{document}
\subsubsection*{Finish Private Chat Success}
{\centering\setcellgapes{4pt}\makegapedcells
\begin{tabularx}{\linewidth}{!{\vrule width 1pt}X|X|l|l|p{0.2\linewidth}!{\vrule width 1pt}}
\Xhline{1pt}
First & Second & Third & Fourth & Fifth\\
\Xhline{1pt}
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & Something & Date & Word Word\\
\Xhline{1pt}
\end{tabularx}
}
\end{document}