多列单元格使用所有可用空间

多列单元格使用所有可用空间

我如何才能让文本使用第一行最后 3 列的可用空间?我可以不断增加该值直到它达到该值,但应该有一种动态的方式来获取可用宽度,不是吗?

在此处输入图片描述

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{tabularx,booktabs}
\usepackage{multicol}
\usepackage{titlesec}
\usepackage{array}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}

\usepackage{geometry}
\geometry{margin=0.5in}

\begin{document}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{table}[h!]
\centering 
\begin{tabular}{|c|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|C{0.9cm}|} 
\hline 
\textbf{Year/Month} & \textbf{Jan.} & \textbf{Fev.} & \textbf{Mar.} & \textbf{Abril} & \textbf{Maio} & \textbf{Jun.} & \textbf{Jul.} & \textbf{Agos.} & \textbf{Set.} & \textbf{Out.} & \textbf{Nov.} & \textbf{Dez.} \\
\hline 
    \textbf{1º} & 
    \multicolumn{8}{c|}{\cellcolor[HTML]{EFEFEF}Lorem ipsum dolor sit amet.} & 
    \cellcolor[HTML]{EFEFEF} test &
    \multicolumn{3}{C{3cm}|}{\cellcolor[HTML]{EFEFEF}Suspendisse at sodales turpis. Ut ornare vulputate ligula. Sed placerat consectetur ante, et placerat tortor tempor et. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.} \\
\hline
\end{tabular}
\end{table}

\end{document}

答案1

使用tabularxragged2e包装:

在此处输入图片描述

\documentclass{report}
\usepackage[margin=0.5in]{geometry}
\geometry{margin=0.5in}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}
\usepackage{tabularx}
\usepackage{ragged2e}
\newcolumntype{C}{>{\centering\arraybackslash}X}


\begin{document}
    \begin{table}[h!]
    \centering
    \small
    \setlength\tabcolsep{3pt}
    \renewcommand\tabularxcolumn[1]{m{#1}}
\begin{tabularx}{\linewidth}{|c| *{12}{C|}}
    \hline
\textbf{Year/Month} & \textbf{Jan.} & \textbf{Fev.} & \textbf{Mar.} & \textbf{Abril} & \textbf{Maio}
                    & \textbf{Jun.} & \textbf{Jul.} & \textbf{Agos.} & \textbf{Set.} & \textbf{Out.}
                    & \textbf{Nov.} & \textbf{Dez.} \\
    \hline
    \rowcolor[HTML]{EFEFEF}
\cellcolor{white}{\textbf{1º}}
    &   \multicolumn{8}{c|}{Lorem ipsum dolor sit amet.}
        &   test    &   \multicolumn{3}{>{\RaggedRight\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth}X|}{Suspendisse at sodales turpis. Ut ornare vulputate ligula. Sed placerat consectetur ante, et placerat tortor tempor et. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.} \\
    \hline
\end{tabularx}
    \end{table}
\end{document}

如果您希望在最后multicolum将文本居中,则当您不喜欢对文本进行连字符连接时,请\RaggedRight\Centering或替换。\centering\arraybackslash

答案2

因为你知道要跨越的列有多宽,所以你可以精确地计算分配给多列的宽度

宽度乘以列数,加上\tabcolsep除一列之外的每一列的两个空格(由其\multicolumn自身提供)加上\arrayrulewidth除一列之外的每一列。

\documentclass{report}
\usepackage{array}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}

\usepackage{geometry}
\geometry{margin=0.5in}

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcommand{\multiC}[2]{% #1 = number of cells to span, #2 = width of C cols
  \multicolumn{#1}{C{\dimexpr#2*#1+2\tabcolsep*(#1-1)+\arrayrulewidth*(#1-1)}|}%
}

\begin{document}

\begin{table}[htp]
\centering
\newcommand{\mname}[1]{\makebox[0.9cm]{\bfseries #1}}

\begin{tabular}{ |c| *{12}{C{0.9cm}|} }
\hline 
\textbf{Year/Month} &
  \mname{Jan.} & \mname{Fev.} & \mname{Mar.} & \mname{Abril} &
  \mname{Maio} & \mname{Jun.} & \mname{Jul.} & \mname{Agos.} &
  \mname{Set.} & \mname{Out.} & \mname{Nov.} & \mname{Dez.} \\
\hline 
\textbf{1º} & 
  \multiC{8}{0.9cm}{\cellcolor[HTML]{EFEFEF}Lorem ipsum dolor sit amet.} & 
  \cellcolor[HTML]{EFEFEF} test &
  \multiC{3}{0.9cm}{\cellcolor[HTML]{EFEFEF}Suspendisse at sodales turpis.
    Ut ornare vulputate ligula. Sed placerat consectetur ante, et placerat
    tortor tempor et. Orci varius natoque penatibus et magnis dis parturient
    montes, nascetur ridiculus mus.} \\
\hline
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

相关内容