我有一个简单的表格,当文本太大时,我设法使用 p{4.7cm} 使其缩放。以下是代码:
\begin{document}
\begin{table}[]
\scalebox{1.0}{
\begin{tabular}{c|c|c|}
\cline{2-3} &
\multicolumn{1}{p{4.7cm}|}{RIGHT} &
\multicolumn{1}{c|}{LEFT} \\ \hline
\multicolumn{1}{|c|}{test1} & 1& 1\\ \hline
\multicolumn{1}{|c|}{test2} & 1 & 1 \\ \hline
\end{tabular}
}
\end{table}
\end{document}
它看起来是这样的:
我将 p{4.7cm} 设置为右侧,但我希望在文本长度小于 4.7cm 的情况下,列大小与文本大小相等。
可能做这样的事吗?
谢谢
答案1
\documentclass{article}
\usepackage{xltabular}
\convertXColumns
\usepackage{blindtext}
\begin{document}
\setlength\extrarowheight{2pt}
\begin{xltabular}{\linewidth}{|c|X|c|}\cline{2-3}
\multicolumn{1}{c|}{}& short & short \\ \hline
test1 & 1& 1\\ \hline
test2 & 1 & 1 \\ \hline
\end{xltabular}
\bigskip
\begin{xltabular}{\linewidth}{|c|X|c|}\cline{2-3}
\multicolumn{1}{c|}{} & \blindtext & LEFT \\ \hline
test1 & 1& 1\\ \hline
test2 & 1 & 1 \\ \hline
\end{xltabular}
\end{document}
答案2
表格列可以设置自然宽度,最大宽度为整个表格的最大宽度
\documentclass{article}
\usepackage{tabulary}
\begin{document}
\centering
\setlength\extrarowheight{2pt}
\begin{tabulary}{\linewidth}{|c|L|c|}
\cline{2-3}
\multicolumn{1}{c|}{}&RIGHT & LEFT \\ \hline
test1 & 1& 1\\ \hline
test2 & 1 & 1 \\ \hline
\end{tabulary}
\bigskip
\begin{tabulary}{\linewidth}{|c|L|c|}
\cline{2-3}
\multicolumn{1}{c|}{}&
RIGHT RIGHT RIGHT
& LEFT \\ \hline
test1 & 1& 1\\ \hline
test2 & 1 & 1 \\ \hline
\end{tabulary}
\bigskip
\begin{tabulary}{\linewidth}{|c|L|c|}
\cline{2-3}
\multicolumn{1}{c|}{}&
RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT
RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT
RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT
& LEFT \\ \hline
test1 & 1& 1\\ \hline
test2 & 1 & 1 \\ \hline
\end{tabulary}
\end{document}
或按照评论中的要求对所有列进行
\documentclass{article}
\usepackage{tabulary}
\begin{document}
\centering
\setlength\extrarowheight{2pt}
\setlength\tymin{.2\linewidth}
\begin{tabulary}{\linewidth}{|L|L|L|}
\cline{2-3}
\multicolumn{1}{c|}{}&RIGHT & LEFT \\ \hline
test1 & 1& 1\\ \hline
test2 & 1 & 1 \\ \hline
\end{tabulary}
\bigskip
\begin{tabulary}{\linewidth}{|L|L|L|}
\cline{2-3}
\multicolumn{1}{c|}{}&
RIGHT RIGHT RIGHT
& LEFT \\ \hline
test1 & 1& 1\\ \hline
test2 & 1 & 1 \\ \hline
\end{tabulary}
\bigskip
\begin{tabulary}{\linewidth}{|L|L|L|}
\cline{2-3}
\multicolumn{1}{c|}{}&
RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT
RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT
RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT RIGHT
& LEFT \\ \hline
test1 & 1& 1\\ \hline
test2 & 1 & 1 \\ \hline
\end{tabulary}
\end{document}