长表列的标题

长表列的标题

我怀疑我在代码中添加了一些我不理解也不想要的东西。我的 longtable 列标题的文本似乎不想换行。有人能告诉我我哪里出错了吗?

\documentclass[a4paper]{book}

% General packages to use
\usepackage[english]{babel}
\usepackage{cjhebrew}
\usepackage[margin=1in]{geometry}

% Table-related stuff
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.95}

\usepackage{longtable}
\usepackage{array} % for ExtraRowHeight

\newcolumntype{L}{>{\raggedright\arraybackslash}}
\newcolumntype{R}{>{\raggedleft\arraybackslash}}
\newcolumntype{C}{>{\centering\arraybackslash}}

\renewcommand{\arraystretch}{1.5}
\renewcommand{\tabcolsep}{0.2cm}

\setlength{\LTpre}{6pt}
\setlength{\LTpost}{6pt}

\begin{document}

 \rowcolors{1}{lightgray}{}
\large
\begin{longtable}{Cp{2.5cm}Cp{2.5cm}Cp{3cm}Cp{2.5cm}}
\hline

\multicolumn{1}{c}{\textbf{Long heading which I want to wrap}} & \multicolumn{1}{c}{\textbf{Short heading}} & \multicolumn{1}{c}{\textbf{Short heading}} & \multicolumn{1}{c}{\textbf{Short heading}} \\ \hline
\endhead
Nisanu & Nisan & Mar./Apr. & Apr. \\
Aiaru & Iyyar & Apr./May & May \\
Simanu & Sivan & May/June & June \\
Duzu & Tammuz & June/July & July \\
Abu & Ab & July/Aug. & Aug. \\
Ululu & Elul & Aug./Sep. & Sep. \\
Tashritu & Tishri & Sep./Oct. & Oct. \\
Arahsamnu & Heshvan & Oct./Nov. & Nov. \\
Kislimu & Kislev & Nov./Dec. & Dec. \\
Tebetu & Tebeth & Dec./Jan. & Jan. \\
Shabatu & Shebat & Jan./Feb. & Feb. \\
Addaru & Adar & Feb./Mar. & Mar. \\

\hline
\end{longtable}

\end{document}

非常感谢,

詹姆士。

答案1

你可能想要使用

\textbf{Long heading which I want to wrap}

代替

\multicolumn{1}{c}{\textbf{Long heading which I want to wrap}}

否则会导致列不固定,文本无法换行。

其他的就不用了\multicolumn。还2.5cm不够,3cm每列都改成。

平均能量损失

\documentclass[a4paper]{book}

% General packages to use
\usepackage[english]{babel}
\usepackage{cjhebrew}
\usepackage[margin=1in]{geometry}

% Table-related stuff
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.95}

\usepackage{longtable}
\usepackage{array} % for ExtraRowHeight

\newcolumntype{L}{>{\raggedright\arraybackslash}}
\newcolumntype{R}{>{\raggedleft\arraybackslash}}
\newcolumntype{C}{>{\centering\arraybackslash}}

\renewcommand{\arraystretch}{1.5}
\renewcommand{\tabcolsep}{0.2cm}

\setlength{\LTpre}{6pt}
\setlength{\LTpost}{6pt}

\begin{document}

 \rowcolors{1}{lightgray}{}
\large
\begin{longtable}{Cp{3cm}Cp{3cm}Cp{3cm}Cp{3cm}}
\hline

\textbf{Long heading which I want to wrap}
& \textbf{Short heading}
& \textbf{Short heading}
& \textbf{Short heading} \\ \hline
\endhead
Nisanu & Nisan & Mar./Apr. & Apr. \\
Aiaru & Iyyar & Apr./May & May \\
Simanu & Sivan & May/June & June \\
Duzu & Tammuz & June/July & July \\
Abu & Ab & July/Aug. & Aug. \\
Ululu & Elul & Aug./Sep. & Sep. \\
Tashritu & Tishri & Sep./Oct. & Oct. \\
Arahsamnu & Heshvan & Oct./Nov. & Nov. \\
Kislimu & Kislev & Nov./Dec. & Dec. \\
Tebetu & Tebeth & Dec./Jan. & Jan. \\
Shabatu & Shebat & Jan./Feb. & Feb. \\
Addaru & Adar & Feb./Mar. & Mar. \\

\hline
\end{longtable}

\end{document} 

在此处输入图片描述

答案2

您的表格设置有点复杂。与其定义大量实际上只作用于p列类型的单独列类型,不如更直接地加载ragged2e包并设置以下定义:

\newcolumntype{C}[1]{>{\Centering\arraybackslash}p{#1}}

\Centering(使用而不是 的要点\centering在于前一个宏允许连字符。)C{3cm}在下面的示例中,我已将 用作第 2 至第 4 列。不过,我怀疑,只需将这些列用作 即可c。还请注意,我已删除所有\mulicolumn“包装器”。

对了,既然你有声明\usepackage[table]{xcolor},就没必要\usepackage{colortbl}再陈述了。

在此处输入图片描述

\documentclass[a4paper]{book}

% General packages to use
\usepackage[english]{babel}
\usepackage{cjhebrew}
\usepackage[margin=1in]{geometry}

% Table-related stuff
%\usepackage{colortbl} % no need to load it separately
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.95}

\usepackage{longtable}
\usepackage{array} % for ExtraRowHeight
\usepackage{ragged2e} % for \Centering macro
\newcolumntype{C}[1]{>{\Centering\arraybackslash}p{#1}}

\renewcommand{\arraystretch}{1.5}
\renewcommand{\tabcolsep}{0.2cm} % default value: 6pt

\setlength{\LTpre}{6pt}
\setlength{\LTpost}{6pt}

\begin{document}

\rowcolors{1}{lightgray}{}
{\large
\begin{longtable}{C{4.25cm} C{3cm} C{3cm} C{3cm} }
\hline
\textbf{Long heading which I want to wrap} 
& \textbf{Short heading} & \textbf{Short heading} & \textbf{Short heading} \\ 
\hline
\endhead
\hline
\endfoot
Nisanu & Nisan & Mar./Apr. & Apr. \\
Aiaru & Iyyar & Apr./May & May \\
Simanu & Sivan & May/June & June \\
Duzu & Tammuz & June/July & July \\
Abu & Ab & July/Aug. & Aug. \\
Ululu & Elul & Aug./Sep. & Sep. \\
Tashritu & Tishri & Sep./Oct. & Oct. \\
Arahsamnu & Heshvan & Oct./Nov. & Nov. \\
Kislimu & Kislev & Nov./Dec. & Dec. \\
Tebetu & Tebeth & Dec./Jan. & Jan. \\
Shabatu & Shebat & Jan./Feb. & Feb. \\
Addaru & Adar & Feb./Mar. & Mar. \\
\end{longtable}
} % end of scope of \large directive
\end{document}

仅用于比较,以下是如果C{2.5cm}用于所有四列时的结果 - 请注意,所有四列标题中的文本都会换行。

在此处输入图片描述

答案3

您必须用单行单元格(如)\multicolumn来替换p该单元格的(parbox)规范。c\mbox

你不想摆脱 parbox,所以不要使用,\multicolumn只需使用\centering

在最后一列中,您将需要这样做\centering\arraybackslash,否则以下内容\\只会在单元格内居中换行,而不会按预期结束表格行。

答案4

另一个解决方案是使用普通c列类型,这要归功于包,它允许换行和列标题的通用格式。我还用加载包makecell替换了的使用,以确保在以字母为前缀的列中,单元格顶部和底部的垂直间距最小。\arraystretch=1.5cellspaceS

如果您希望列标题不对齐,而不是默认的垂直和水平居中,则可以使用参数来实现\theadalign,该参数必须包含一对来自 {t,c,b} 和 {l,r,c} 的值:

\documentclass[a4paper]{book}

% General packages to use
\usepackage[english]{babel}
\usepackage{cjhebrew}
\usepackage[margin=1in]{geometry}

% Table-related stuff
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.95}

\usepackage{longtable}
\usepackage{booktabs} %
\usepackage{makecell}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadalign{bc}
\usepackage{cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}

\setlength{\LTpre}{6pt}
\setlength{\LTpost}{6pt}

\begin{document}

 \rowcolors{1}{lightgray}{}
\large
\begin{longtable}{Scccc}
\hline
\thead{Long heading which\\ I want to wrap}
& \thead{Short heading}
& \thead{Short heading}
& \thead{Short heading} \\
\hline
\endhead
Nisanu & Nisan & Mar./Apr. & Apr. \\
Aiaru & Iyyar & Apr./May & May \\
Simanu & Sivan & May/June & June \\
Duzu & Tammuz & June/July & July \\
Abu & Ab & July/Aug. & Aug. \\
Ululu & Elul & Aug./Sep. & Sep. \\
Tashritu & Tishri & Sep./Oct. & Oct. \\
Arahsamnu & Heshvan & Oct./Nov. & Nov. \\
Kislimu & Kislev & Nov./Dec. & Dec. \\
Tebetu & Tebeth & Dec./Jan. & Jan. \\
Shabatu & Shebat & Jan./Feb. & Feb. \\
Addaru & Adar & Feb./Mar. & Mar. \\
\hline
\end{longtable}

\end{document} 

在此处输入图片描述

相关内容