如何在表格中水平居中 p 型列?

如何在表格中水平居中 p 型列?

我有这个乳胶代码:

\begin{tabular}{|c|*{26}{p{0.1cm}|}}
\hline 
\multirow {2}{*}{Activities} & \multicolumn{4}{c|}{June} & \multicolumn{5}{c|}{July} & \multicolumn{4}{c|}{August} & \multicolumn{4}{c|}{September} & \multicolumn{5}{c|}{October} & \multicolumn{4}{c|}{November} \\\cline {2-27}
 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\\hline 
lorem & \cellcolor {blue!50} &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
ipsumffffffffffff &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
dolor &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
sit &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
amet &  &  &  &  &  &  &  &  &  &  &  & \cellcolor {blue!50} &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
\end{tabular}

生成以下表格:

在此处输入图片描述

如您所见,数字 1-26 不在中心,甚至溢出(从数字 10 开始直到 26)。我已经尝试过\newcolumntype{P}{>{\centering\arraybackslash}p{0.1cm}}如何使表格中的列值居中?),但是没有作用。

以下是整个文档:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
 \geometry{
 a4paper,
 total={170mm,257mm},
 left=20mm,
 top=20mm,
 }
 \usepackage{graphicx}
 \usepackage{titling} 

\title{Some title
}
\author{Max Mustermann}
\date{November 2022}
 
 \usepackage{fancyhdr}
\fancypagestyle{plain}{%  the preset of fancyhdr 
    \fancyhf{} % clear all header and footer fields
    \fancyhead[L]{Some other title}
    \fancyhead[R]{\theauthor}
}
\makeatletter
\def\@maketitle{%
  \newpage
  \null
  \vskip 1em%
  \begin{center}%
  \let \footnote \thanks
    {\LARGE \@title \par}%
    \vskip 1em%
  \end{center}%
  \par
  \vskip 1em}
\makeatother

\usepackage{lipsum}  
\usepackage{cmbright}

\usepackage{array}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{xcolor}

\begin{document}

\maketitle
\section*{Description}
\lipsum[1] \\

\begin{tabular}{|c|*{26}{p{0.1cm}|}}
\hline 
\multirow {2}{*}{Activities} & \multicolumn{4}{c|}{June} & \multicolumn{5}{c|}{July} & \multicolumn{4}{c|}{August} & \multicolumn{4}{c|}{September} & \multicolumn{5}{c|}{October} & \multicolumn{4}{c|}{November} \\\cline {2-27}
 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\\hline 
lorem & \cellcolor {blue!50} &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
ipsumffffffffffff &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
dolor &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
sit &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
amet &  &  &  &  &  &  &  &  &  &  &  & \cellcolor {blue!50} &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
\end{tabular}

\end{document}

以下是整个文档的内容:

在此处输入图片描述

答案1

  • 您没有提供任何有关页面布局的信息。

  • 0.1 厘米的列宽对于单元格的内容来说太小了,因此它会溢出该空间(并且无法居中)。

  • 表格尺寸太大,无法放入文本块。

  • 可能的解决方案是:

    • 使用geometry包并定义宽\textwidth
    • 通过使用“changepage”包,使表格突出于文本块之外,例如突出于外边距边框
  • 在这两种情况下,我建议使用array包、\small字体大小、减少列分隔并使用列类型w{c}{<column width>}

  • 包使用情况geometry

\documentclass{article}
\usepackage[margin = 25mm]{geometry}

\usepackage{lipsum}% For dummy text. Don't use in a real document
\usepackage[table]{xcolor}
\usepackage{array, multirow}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\lipsum[1]
\begin{center}
\small
\setlength{\tabcolsep}{3pt}
\begin{tabular}{|c|*{26}{w{c}{1em}|}}
    \hline
\multirow {2}{*}{Activities} 
    & \multicolumn{4}{c|}{June} 
        & \multicolumn{5}{c|}{July} 
            & \multicolumn{4}{c|}{August} 
                & \multicolumn{4}{c|}{September} 
                    & \multicolumn{5}{c|}{October} 
                        & \multicolumn{4}{c|}{November}     \\
    \cline {2-27}
    & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\\hline
    lorem & \cellcolor {blue!50} &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
    ipsumffffffffffff &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
    dolor &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
    sit &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
    amet &  &  &  &  &  &  &  &  &  &  &  & \cellcolor {blue!50} &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
    \end{tabular}
\end{center}
\lipsum[1]
\end{document}

在此处输入图片描述

(红线表示文本块的边框)

  • 包使用情况changepage
\documentclass{article}

\usepackage{lipsum}% For dummy text. Don't use in a real document
\usepackage[table]{xcolor}
\usepackage{array, multirow}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage[strict]{changepage}

\begin{document}
\lipsum[1]
\begin{adjustwidth*}{}{-2\marginparwidth}
\begin{center}
\small
\setlength{\tabcolsep}{3pt}
\begin{tabular}{|c|*{26}{w{c}{1em}|}}
    \hline
\multirow {2}{*}{Activities} 
    & \multicolumn{4}{c|}{June} 
        & \multicolumn{5}{c|}{July} 
            & \multicolumn{4}{c|}{August} 
                & \multicolumn{4}{c|}{September} 
                    & \multicolumn{5}{c|}{October} 
                        & \multicolumn{4}{c|}{November}     \\
    \cline {2-27}
    & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\\hline
    lorem & \cellcolor {blue!50} 
             &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
    ipsumffffffffffff 
          &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
    dolor &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &              \\\hline
    sit   &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &                \\\hline
    amet  &  &  &  &  &  &  &  &  &  &  &  & \cellcolor {blue!50} 
                                              &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\
    \hline[![enter image description here][2]][2]
    \end{tabular}
\end{center}
\end{adjustwidth*}
\lipsum[1]
\end{document}

在此处输入图片描述

答案2

我们有Overfull hbox 2.15 too widep{0.1cm}。

我提议:

  • \setlength{\tabcolsep}{4pt} (默认为 6pt)
  • changepage扩展表格边距的包

代码

\documentclass{article}
\usepackage[a4paper,margin = 2cm,
            showframe,%<-- comment in the final version
]{geometry}
\usepackage{amsmath}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage[strict]{changepage}
\parindent=0pt
\begin{document}
\verb|\the\tabcolsep| = \the\tabcolsep


\lipsum[1]

\begin{adjustwidth}{-1.2cm}{-1.2cm}
\begin{center}
\setlength{\tabcolsep}{4pt}
\begin{tabular}{|c|*{26}{>{\centering\arraybackslash}p{1em}|}}
    \hline 
    \multirow {2}{*}{Activities} & \multicolumn{4}{c|}{June} & \multicolumn{5}{c|}{July} & \multicolumn{4}{c|}{August} & \multicolumn{4}{c|}{September} & \multicolumn{5}{c|}{October} & \multicolumn{4}{c|}{November} \\\cline {2-27}
    & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\\hline 
    lorem & \cellcolor {blue!50} &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
    ipsumffffffffffff &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
    dolor &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
    sit &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline 
    amet &  &  &  &  &  &  &  &  &  &  &  & \cellcolor {blue!50} &  &  &  &  &  &  &  &  &  &  &  &  &  &  \\\hline
    \end{tabular}      
\end{center}
\end{adjustwidth}
\lipsum[1]
\end{document}

在此处输入图片描述

相关内容