在固定宽度的表格中换行,以便表格高度调整

在固定宽度的表格中换行,以便表格高度调整

我看了这些问题,但它们似乎没有解决我的问题,因为使用parbox第一列单元格并不能使表格调整到第一个单元格的内容,也不可能简单地cmmultirow选项中指定宽度。

\documentclass[12pt]{report}

\usepackage[]{multirow}
\usepackage[autostyle]{csquotes}% This is for quotes
\usepackage{tabulary}% This is for tables
\usepackage{ragged2e}
\usepackage{longtable,array,ragged2e}% This is formatting for long tables
% \newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}%This is a wrapper to make everything a certain width - http://tex.stackexchange.com/questions/12703/how-to-create-fixed-width-table-columns-with-text-raggedright-centered-raggedlef
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{graphicx}% This is for images
\usepackage{booktabs,dcolumn,caption}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}%This is for precision tables per property
% \captionsetup{labelsep=newline,singlelinecheck=false} % optional, this makes the caption numbers appear on a new line.
\newcolumntype{d}[1]{D{.}{.}{#1}} % "decimal" column type
\renewcommand{\ast}{{}^{\textstyle *}} % for raised "asterisks"
\usepackage{bookmark}% This overrides warnings like http://tex.stackexchange.com/questions/52576/difference-between-bookmark-levels-greater-than-one-what-does-this-mean
\begin{document}


\begin{table}[!ht]
\centering
\begin{tabular}{ |C{3.25cm}|C{3.25cm}|C{3.25cm}|C{3.25cm}| }
\hline
\textbf{Sentence} & \textbf{Training Examples} & \textbf{Region} & \textbf{Value} \\ \hline
\multirow{4}{3.25cm}{"Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$7,500) and Botswana (around \$8,900) and as we head up the scale we find South Korea (around \$27,500),the United Kingdom (\$39,600), the United States (\$48,150) and right at the top we find Qatar (\$98,000) and Luxembourg (\$ 122,147)."} & LB & Lucus Radebe & Bla \\
 & DC & Michael Duberry & Bla \\
 & DC & Dominic Matteo & Bla \\
 & RB & Didier Domi  & Bla\\ \hline
\end{tabular}
\end{table} 

\end{document}

在此处输入图片描述

答案1

\savebox在很多例子中确实很有用,尽管有时它们可​​能有点不合适。

它的工作原理是将设置为指定宽度的 parbox 保存到框中。然后我们可以使用\totalheightof{}from calc-package 计算该框的高度。然后我们将这个长度分配给每一行,4 行等于框高度的 25%。然后我们将这个长度用于 linelinespecifier \\[\spacing],但这会稍微超出一点。多少?好吧,LaTeX 首先在表格中添加了一行,然后插入了这段长度的黑色空间,所以我们需要考虑到这一点。因此,我们将长度更改为\setlength{\customSpacing}{.25\sentenceHeight-\baselineskip}

进一步的建议:

  • 将这么多文本放在表格中对读者来说不太方便。考虑将这些文本放在一个段落中,也可以放在标题中,或者放在表格下方的注释中。

输出

在此处输入图片描述

代码

\documentclass[12pt]{report}

\usepackage[]{multirow}
\usepackage{ragged2e}
\usepackage{array}%
\usepackage{calc}

\newcolumntype{W}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}}

\begin{document}

  \newsavebox{\sentence}
  \savebox{\sentence}{\parbox{3.25cm}{\RaggedRight``Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$7,500) and Botswana (around \$8,900) and as we head up the scale we find South Korea (around \$27,500),the United Kingdom (\$39,600), the United States (\$48,150) and right at the top we find Qatar (\$98,000) and Luxembourg (\$122,147).''}}
  \newlength{\sentenceHeight}
  \setlength{\sentenceHeight}{\totalheightof{\usebox{\sentence}}}

  \begin{table}[!ht]
    \newcommand{\headerFormat}[1]{% Define a headerformat, only used in this specific table.
  \multicolumn{1}{|m{3.25cm}|}{\centering\bfseries#1}
}
    \newlength{\customSpacing}
    \setlength{\customSpacing}{.25\sentenceHeight-\baselineskip}
    \centering
    \begin{tabular}{ *{4}{|W{3.25cm}}| }
      \hline
      \headerFormat{Sentence} & \headerFormat{Training\par Examples} & \headerFormat{Region} & \headerFormat{Value} \\
      \hline
      \multirow{4}{*}{\usebox{\sentence}} & LB & Lucus Radebe & Bla \\[\customSpacing]
      & DC & Michael Duberry & Bla \\[\customSpacing]
      & DC & Dominic Matteo & Bla \\[\customSpacing]
      & RB & Didier Domi  & Bla\\[\customSpacing]
      \hline
    \end{tabular}
  \end{table}



\end{document}

答案2

您可以通过两种方式解决您的问题:

  • 使用multirow,计算所需的数量lines((不是rows!)并在第二列添加一个补充单元格,其中包含所需深度的不可见规则(通过反复试验找到)
  • without multirow, make, from the 2nd row, the 2nd to 4th columns a nested tabular inserted in a\多列`。

在这两种情况下,我都根据类型定义了L,C,Rp{…}类型,因为我认为该m{…}类型用于使列标题垂直居中,这是我使用命令获得\theadmakecell

添加::我还提出了第三种解决方案,以便第 2-4 列中的第 2-5 行等距分布。

    \documentclass[12pt]{report}
    \usepackage{xcolor} %
    \usepackage{multirow}
    \usepackage[autostyle]{csquotes}% This is for quotes
    \usepackage{tabulary}% This is for tables
    \usepackage{longtable,array,ragged2e}% This is formatting for long tables
    % \newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}%This is a wrapper to make everything a certain width - http://tex.stackexchange.com/questions/12703/how-to-create-fixed-width-table-columns-with-text-raggedright-centered-raggedlef
    \newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}m{#1}}
    \newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}}
    \newcolumntype{R}[1]{>{\raggedleft\\\arraybackslash\hspace{0pt}}m{#1}}
    \usepackage{graphicx}% This is for images
    \usepackage{booktabs,dcolumn,caption, makecell}
    \newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}%This is for precision tables per property
    % \captionsetup{labelsep=newline,singlelinecheck=false} % optional, this makes the caption numbers appear on a new line.
    \newcolumntype{d}[1]{D{.}{.}{#1}} % "decimal" column type
    \renewcommand{\ast}{{}^{\textstyle *}} % for raised "asterisks"
    \usepackage{bookmark}% This overrides warnings like http://tex.stackexchange.com/questions/52576/difference-between-bookmark-levels-greater-than-one-what-does-this-mean
    \renewcommand\theadfont{\normalsize\bfseries}

    \begin{document}

    \begin{table}[!htb]
    \centering
    \begin{tabular}{ |C{3.25cm}|C{3.25cm}|C{3.25cm}|C{3.25cm}| }
    \hline
    \thead{Sentence} & \thead{Training\\ Examples} & \thead{Region} & \thead{Value} \\ \hline
    \multirow{17}{3.25cm}{"Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$\,7,500) and Botswana (around \$\,8,900) and as we head up the scale we find South Korea (around \$\,27,500),the United Kingdom (\$\,39,600), the United States (\$48,150) and right at the top we find Qatar (\$\,98,000) and Luxembourg (\$\,122,147)."}%
     & LB & Lucus Radebe & Bla \rule{0pt}{2.5ex}\\
     & DC & Michael Duberry & Bla \\
     & DC & Dominic Matteo & Bla \\
     & RB & Didier Domi & Bla \\%
      & \rule[-18.5\baselineskip] {0pt}{0pt} & & \\
    \hline
    \end{tabular}
    \end{table}

    \begin{table}[!hb]
    \centering%
    \begin{tabular}{ |C{3.25cm}|C{3.25cm}|C{3.25cm}|C{3.25cm}|}
    \hline
    \thead{Sentence} & \thead{Training\\ Examples} & \thead{Region} & \thead{Value}
    \\ \hline
   \rule{0pt}{2.5ex} {"Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$\,7,500) and Botswana (around \$\,8,900) and as we head up the scale we find South Korea (around \$\,27,500),the United Kingdom (\$\,39,600), the United States (\$\,48,150) and right at the top we find Qatar (\$\,98,000) and Luxembourg (\$\,122,147)."}
     &
     \multicolumn{3}{@{}c|}{\begin{tabular}[t] {C{3.25cm}|C{3.25cm}|C{3.25cm}}
     \rule{0pt}{2.5ex}LB & Lucus Radebe & Bla \\
      DC & Michael Duberry & Bla \\
     DC & Dominic Matteo & Bla \\
     RB & Didier Domi & Bla \\[18.2\baselineskip] & & \end{tabular}}%%
    \\
      \hline
    \end{tabular}
    \end{table}

    \clearpage

    \begin{table}[!htb]
    \centering%
    \begin{tabular}{ |C{3.25cm}|C{3.25cm}|C{3.25cm}|C{3.25cm}|}
    \hline
    \thead{Sentence} & \thead{Training\\ Examples} & \thead{Region} & \thead{Value}
    \\ \hline
  \multirow{17}{\linewidth}{"Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$\,7,500) and Botswana (around \$\,8,900) and as we head up the scale we find South Korea (around \$\,27,500),the United Kingdom (\$\,39,600), the United States (\$\,48,150) and right at the top we find Qatar (\$\,98,000) and Luxembourg (\$\,122,147)."}
     & \rule{0pt}{2.5ex}LB & Lucus Radebe & Bla\\ %
     & \multicolumn{3}{@{}c|}{\setlength\extrarowheight{92pt}\begin{tabular}[t] {C{3.25cm}|C{3.25cm}|C{3.25cm}}%
      DC & Michael Duberry & Bla \\%
     DC & Dominic Matteo & Bla \\%
     RB & Didier Domi & Bla %
     \end{tabular}}%
    \\
      \hline
    \end{tabular}
    \end{table}

     \end{document} 

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

答案3

值得一提的是,这种多行功能可以与 ConTeXt Natural Tables 一起使用;不需要任何技巧:

\startsetups table:style
\setupTABLE[each][each][frame=off,leftframe=on, rightframe=on]
\setupTABLE[row][first][style=bold, align={middle,lohi}, topframe=on, bottomframe=on]
\setupTABLE[row][last] [bottomframe=on]
\setupTABLE[each][each][align=middle, width=3.25cm]
\stopsetups

\starttext
\startTEXpage[offset=2mm]
\startTABLE[setups={table:style}]
  \NC Sentence \NC Training Examples \NC Region \NC Value \NC \NR
  \NC[nr=4, align=flushleft, bottomframe=on]
     “Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$7,500) and Botswana (around \$8,900) and as we head up the scale we find South Korea (around \$27,500),the United Kingdom (\$39,600), the United States (\$48,150) and right at the top we find Qatar (\$98,000) and Luxembourg (\$122,147).”
  \NC LB \NC Lucus Radebe    \NC Bla \NC \NR
  \NC DB \NC Michael Duberry \NC Bla \NC \NR
  \NC DB \NC Dominic Matteo  \NC Bla \NC \NR
  \NC RB \NC Didier Domi     \NC Bla \NC \NR
\stopTABLE
\stopTEXpage
\stoptext

在此处输入图片描述

相关内容