我想在使用 multirow 和 multicol 包时将一个长句子跨越多行。我尝试了几种方法。但是,它们不能满足我的要求。最后,我得到了以下代码和结果。如果有人能帮忙的话,那将是一个很大的帮助。
\begin{table}[h]
\centering
\begin{tabular}{|c|c|c|p{2cm}|p{2cm}|p{2cm}|}
\hline
\multirow{4}{*}{Ref. No.}
&\multirow{4}{*}{Project}
&\multirow{4}{*}{Total estimated construction cost (Rs Mn)}
&\multicolumn{3}{c|}{Amount to be allocated (Rs Mn) in each year}\\
\cline{4-6}
& & &2018&2019&2020
\end{tabular}
\end{table}
答案1
由于缺乏有关您的表格的信息,我只能怀疑您正在寻找类似这样的东西:
为确保万无一失,您需要扩展代码片段以完成以 开头\documentclass{...}
和以 结尾的小文档\end{document}
,并在表中添加至少一行包含表体的内容。
姆韦上图为:
\documentclass{article}
\usepackage{ragged2e}
\usepackage{array, multirow}
\newcolumntype{C}[1]{>{\Centering}p{#1}}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|c|c|p{3cm}| *{3}{C{1.5cm}|} }
\hline
\multirow{2}{*}{Ref. No.}
& \multirow{2}{*}{Project}
& \multirow{2}{=}{\centering Total estimated construction cost (Rs Mn)}
& \multicolumn{3}{C{\dimexpr4.5cm + 4\tabcolsep\relax}|}%
{Amount to be allocated (Rs Mn) in each year} \\
\cline{4-6}
& & & 2018 & 2019 & 2020 \\
\hline
\end{tabular}
\end{table}
\end{document}
答案2
您可能会发现以下宏很有用。
\documentclass{article}
\newcommand{\minwidth}[3][c]% #1 = parbox optional, #2 = number of lines, #3 = text to fit
{\bgroup
\count1=#2\relax
\settowidth{\dimen0}{#3}%
\divide\dimen0 by \count1
\dimen1=\baselineskip
\multiply\dimen1 by \count1
\loop\sbox0{\parbox[#1]{\dimen0}{\strut #3\strut}}%
\ifdim\dimen1<\dimexpr \ht0+\dp0\relax
\advance\dimen0 by 0.5em
\repeat
\box0
\egroup}
\begin{document}
\fboxsep=0pt
\fbox{\minwidth[c]{4}{\raggedright Total estimated construction cost (Rs Mn)}}
\fbox{\minwidth{3}{\centering Amount to be allocated (Rs Mn) in each year}}
\end{document}