这个 tabularx 有什么问题?

这个 tabularx 有什么问题?

我讨厌 LaTeX 中的表格!我总是遇到一些麻烦,但这应该很容易:

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{tabularx}

\begin{document}
 
\begin{center}
    \renewcommand{\arraystretch}{1.5}
    \begin{tabularx}{\textwidth}{|X|X|X|X|X|}
    \hline
        \rowcolor[gray]{0.95}
        \multicolumn{5}{|c|}{Title}
        \\ \hline
        \multicolumn{1}{|c|}{\textsf{Day 1}}
        & \multicolumn{1}{c|}{\textsf{Day 2}}
        & \multicolumn{1}{c|}{\textsf{Day 3}}
        & \multicolumn{1}{c|}{\textsf{Day 4}}
        & \multicolumn{1}{c|}{\textsf{Day 5}}
        \\ \hhline{|=|=|=|=|=|}
        \multicolumn{1}{|c|}{Text 1}
        & \multicolumn{1}{c|}{Text 2}
        & \multicolumn{1}{c|}{Text 3}
        & \multicolumn{1}{c|}{Text 4}
        & \multicolumn{1}{c|}{Text 5}
        \\
        \multicolumn{1}{|c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        \\ \hline
    \end{tabularx}
\end{center}

\end{document}

问题预览:

在此处输入图片描述

如图所示,表格右侧没有关闭。代码有什么问题?

答案1

你问,

代码有什么问题?

代码将每个单元格都包裹(埋葬?)在\multicolumn{1}{|c|}{...}包装器中,从而完全覆盖X列类型在单元格内提供自动换行的功能。因此,完全没有理由期望环境的宽度tabularx等于\textwidth

补救措施?您需要 (a) 摆脱\multicolumn{1}{|c|}{...}包装器,以便tabularx机器能够真正发挥作用,以及 (b) 定义和使用X列类型的版本,该版本使单元格内容居中而不是完全对齐。请参阅下面的代码,了解满足第二个条件的方法。

对于手头的示例,将\tabcolsep控制列间空白量的参数的值减少一半可以成功避免最后一行出现换行符。请参阅tabularx以下屏幕截图中的第二个示例以了解最终的“外观”。

在此处输入图片描述

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage[total={6in,10in}, left=1.5in,top=0.5in, 
            includehead,includefoot]{geometry}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{multirow,hhline}
\usepackage{tabularx}
\usepackage{ragged2e} % for '\Centering' macro
% Define a centered version of 'X' column type:
\newcolumntype{C}{>{\Centering}X} 
% or: \newcolumntype{C}{>{\centering\arraybackslash}X} 

\begin{document}

\begin{center}
\renewcommand{\arraystretch}{1.5}
    \begin{tabularx}{\textwidth}{|*{5}{C|}} % <-- "C", not "X"
    \hline
    \rowcolor[gray]{0.95}
    \multicolumn{5}{|c|}{\texttt{tabularx} with default value of \texttt{\string\tabcolsep}}
    \\ \hline
    \textsf{Day 1}
    & \textsf{Day 2} & \textsf{Day 3} & \textsf{Day 4} & \textsf{Day 5}
    \\ \hhline{|=|=|=|=|=|}
    Text 1 & Text 2 & Text 3 & Text 4 & Text 5 \\
    12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00
    \\ \hline
    \end{tabularx}

\bigskip
\setlength\tabcolsep{3pt} % default value: 6pt

    \begin{tabularx}{\textwidth}{|*{5}{C|}} % <-- "C", not "X"
    \hline
    \rowcolor[gray]{0.95}
    \multicolumn{5}{|c|}{\texttt{tabularx} with reduced value of \texttt{\string\tabcolsep}}
    \\ \hline
    \textsf{Day 1}
    & \textsf{Day 2} & \textsf{Day 3} & \textsf{Day 4} & \textsf{Day 5}
    \\ \hhline{|=|=|=|=|=|}
    Text 1 & Text 2 & Text 3 & Text 4 & Text 5 \\
    12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00 & 12h00 to 18h00
    \\ \hline
    \end{tabularx}
\end{center}

\end{document}

答案2

在此处输入图片描述

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{tabularx}

\begin{document}
 
\begin{center}
    \renewcommand{\arraystretch}{1.5}
    \begin{tabular}{|c|c|c|c|c|}
    \hline
        \rowcolor[gray]{0.95}
        \multicolumn{5}{|c|}{Title}
        \\ \hline
        \multicolumn{1}{|c|}{\textsf{Day 1}}
        & \multicolumn{1}{c|}{\textsf{Day 2}}
        & \multicolumn{1}{c|}{\textsf{Day 3}}
        & \multicolumn{1}{c|}{\textsf{Day 4}}
        & \multicolumn{1}{c|}{\textsf{Day 5}}
        \\ \hhline{|=|=|=|=|=|}
        \multicolumn{1}{|c|}{Text 1}
        & \multicolumn{1}{c|}{Text 2}
        & \multicolumn{1}{c|}{Text 3}
        & \multicolumn{1}{c|}{Text 4}
        & \multicolumn{1}{c|}{Text 5}
        \\
        \multicolumn{1}{|c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        & \multicolumn{1}{c|}{12h00 to 18h00}
        \\ \hline
    \end{tabular}
\end{center}

\end{document}

相关内容