如何在多行设置(和列名)中换行?

如何在多行设置(和列名)中换行?

我尝试在第一列的单元格和一些列标题中使用换行符。我尝试使用建议\pbox。但它似乎不起作用。我已经查找了一些答案,我知道这可能会被解释为重复——但请耐心等待,它确实不起作用。以下是我一直在使用的代码示例:

观察:我一直在尝试在 beamer 类型的文档中制作此表。其中一个答案虽然非常有用,但不适用于该类型的文档 - 因此我现在将在代码中明确说明。此外,我已经更正了列的名称,因为它们的顺序不正确。

\documentclass{beamer}
\usepackage{multirow,bigstrut}
\begin{document}
\frame
{
\frametitle{Table 1}
    \begin{tabular}{|c|c|c|c|c|c|c|}
        \hline
        \textbf{Product} & \textbf{Company}} & \textbf{Temperature} & \textbf{Observations} & \textbf{\pbox{2cm}{Temperature Test\\on Hot Water}} & \textbf{Temperature Test on Cold Water} & \textbf{Consistency} \bigstrut\\
        \hline
        \multirow{2}[4]{*}{\pbox{2cm}{Product \\ 1}} & Company 1 & 13.5 & 84 & 1000.5 & 1000.7 & 1000.8 \bigstrut\\
        \cline{2-7}     & Company 2 & 14.3 & 71 & 1000.2 & 1000.9 & 1000.12 \bigstrut\\
        \hline
    \end{tabular}%
}
end{document}

答案1

您可以使用 轻松获得您想要的内容,它允许使用 、 和在单元makecell格中形成换行符。在我们定义了单元格间隙的值后,该命令将被开关替换。makecell\multirowcell\thead\multirowheadbigstrut\makegapedcells

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[hmargin = 2cm]{geometry}

\usepackage{array, makecell, multirow, pbox, bigstrut}
\renewcommand\theadfont{\bfseries}
\setcellgapes{5pt}
\begin{document}

\noindent
{\setlength\tabcolsep{4pt}\makegapedcells
\begin{tabular}{|*{7}{c|}}
  \hline
  \thead{Product} & \thead{Temperature} & \thead{Date} & \thead{Observations} & \thead{Temperature Test \\on Hot Water} & \thead{Temperature Test \\on Cold Water} & \thead{Consistency} \\
  \hline
  \multirowcell{2}[-5pt]{Product\\ 1} & Company 1 & 13.5 & 84 & 1000.5 & 1000.7 1000.8 & \\
  \cline{2-7} & Company 2 & 14.3 & 71 & 1000.2 & 1000.9 & 1000.12 \\
  \hline
\end{tabular}}%

\end{document} 

对于beamer文档,可以使用以下\rothead命令:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{geometry}

\usepackage{array, makecell, multirow,rotating}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{lb}
\setcellgapes{4pt}
\renewcommand\cellrotangle{40}

\begin{document}
\vspace*{1cm}
{\setlength\tabcolsep{4pt}\makegapedcells
  \setlength{\rotheadsize}{1cm}\footnotesize\centering
  \begin{tabular}{|*{7}{c|}}
    \multicolumn{1}{c}{ \rothead{ \\\rlap{Product}}} & \multicolumn{1}{c}{\rothead{\\\rlap{Temperature}}} & \multicolumn{1}{c}{\rothead{\\ Date}} & \multicolumn{1}{c}{ \rothead{\\\rlap{Observations}}} & \multicolumn{1}{c}{\rothead{\rlap{TemperatureTest}\\ \rlap{on Hot Water}}} & \multicolumn{1}{c}{\rothead{\rlap{Temperature Test}\\\rlap{on Cold Water}}} & \multicolumn{1}{c}{\rothead{\\\rlap{Consistency}}} \\[-0.5ex]
    \hline
    \multirowcell{3}{Product \\ 1} & Company 1 & 13.5 & 84 & 1000.5 & \makecell{1000.7\\ 1000.8} & \\
    \cline{2-7} & Company 2 & 14.3 & 71 & 1000.2 & 1000.9 & 1000.12 \\
    \hline
  \end{tabular}}%

\end{document} 

在此处输入图片描述 在此处输入图片描述

答案2

我不知道 \pbox 来自哪个包,但如果您将其宽度设置为足以容纳“温度”,则 \parbox 可以正常工作。另一方面,我无法让 \bigstrut 在顶行上执行任何操作,因此我改用 \fbox。

桌子

\documentclass{standalone}
\usepackage{multirow,bigstrut}
\begin{document}
\fboxrule=0pt
\centering
\begin{tabular}{|c|c|c|c|c|c|c|}
    \hline
    \textbf{Product} & \textbf{Temperature} & \textbf{Date} & \textbf{Observations} 
    & \fbox{\parbox{2.2cm}{\centering\textbf{Temperature Test on Hot Water}}}
    & \fbox{\parbox{2.2cm}{\centering\textbf{Temperature Test on Cold Water}}} & \textbf{Consistency} \\
    \hline
    \multirow{2}[4]{*}{\parbox{2cm}{\centering Product\\1}} & Company 1 & 13.5 & 84 & 1000.5 & 1000.7 1000.8 \bigstrut \\
    \cline{2-7}     & Company 2 & 14.3 & 71   & 1000.2 & 1000.9 & 1000.12 \bigstrut\\
    \hline
\end{tabular}%
\end{document

}

相关内容