Tabularx 和 itemize 缺少项目

Tabularx 和 itemize 缺少项目

当我这样做时:

\begin{tabularx}{\textwidth}{|p{5cm}|p{5cm}|} 
\toprule
\textbf{KPI} & \textbf{Description} \endhead 
\toprule
\rowcolor{darkgrey} \textbf{The percentage of health and safety trained managers and employees in the workplace}  & \begin{itemize}
\item The percentage of trained managers for occupational health and safety in the institute.
\item The percentage of trained employees for occupational health and safety in the institute.
\end{itemize}  \\ 
\midrule
 \textbf{Frequency of health and safety committee meetings}  & \begin{itemize}\item The number of health and safety committee short meetings per month.
 \item The number of health and safety committee general meetings per year. 
\end{itemize} \\ 
\midrule
\rowcolor{darkgrey} \textbf{Frequency of inspection} & \begin{itemize}

\item Number of inspections per year.
\item Frequency of observation of unsafe behaviors.
 \end{itemize} \\ 
\midrule
\textbf{Frequency of auditing} & 
\begin{itemize}
\item Number of health and safety management system auditing per year.
\end{itemize}   \\ 
\midrule
\rowcolor{darkgrey}  \textbf{Health and safety management team members commitments}  & 
\begin{itemize}
\item Number of deliverables per team member. 
\item Number of participants in the committee meetings.
\end{itemize}   \\ 
\midrule
\textbf{Number of precaution procedures} & 
\begin{itemize}
\item Frequency of early warnings.
\item Number of first aid kits and fire drills.
\item Number of trained employees for first aid kits and fire drills.
\end{itemize}  \\
\bottomrule
\end{tabularx}

我明白了

! LaTeX Error: Something's wrong--perhaps a missing \item.See the LaTeX manual or LaTeX Companion for explanation.Type H <return> for immediate help.... \end{tabularx}

我该如何解决这个问题?我正在使用

\usepackage{booktabs,tabularx}

答案1

我建议你的表格布局略有不同。正如你从示例中看到的,第一和第二个表格中的文本柱子不在同一基线上,这被认为是糟糕的排版。此外,使用阴影时,所有垂直和水平规则都是不必要的。此外,您会看到四处散落的白线,因为\rowcolorbooktabs规则以及booktabs-规则和垂直规则都不兼容。如果您的表格是文档的一部分,文本将不会在左右边距上对齐,这不是很好。

取消列表项上方空间的代码改编自 David Carlisle 的代码这个答案

这是我的建议,如果您有兴趣,我可以解释不同的步骤。

在此处输入图片描述

\documentclass{article}
\usepackage{array,booktabs}
\usepackage[table]{xcolor}
\usepackage{enumitem}
\definecolor{darkgrey}{cmyk}{0,0,0,.15}
\usepackage{lmodern, microtype}

\newcolumntype{P}{p{\dimexpr(0.5\linewidth-1em-2\tabcolsep)}}
\newcolumntype{Z}{p{\dimexpr(2em-2\tabcolsep)}}
\newcommand{\listbox}[1]{\parbox[t]{\dimexpr(\linewidth-\tabcolsep)}{\mbox{}\vspace{-\normalbaselineskip\relax}\begin{itemize}[nosep, leftmargin=1em,]#1\end{itemize}}}

\newcommand{\rowspace}{\addlinespace[0.5\normalbaselineskip]}

\setlength{\arrayrulewidth}{3pt}

%%---------------- show page layout. don't use in a real document!
%\usepackage{showframe}
%\renewcommand\ShowFrameLinethickness{0.15pt}
%\renewcommand*\ShowFrameColor{\color{red}}
%%---------------------------------------------------------------%

\begin{document}

\setlength{\tabcolsep}{1pt}
\noindent\begin{tabular}{>{\bfseries}PZP} 

KPI && \textbf{Description} \\
\addlinespace[0.25\normalbaselineskip]\toprule
\arrayrulecolor{darkgrey}\hline
\rowcolor{darkgrey}
The percentage of health and safety trained managers and employees in the workplace  & &
\listbox{\item The percentage of trained managers for occupational health and safety in the institute
\item The percentage of trained employees for occupational health and safety in the institute} \par\\
\rowspace
 Frequency o health and safety committee meetings  &&
 \listbox{\item The number of health and safety committee short meetings per month
 \item The number of health and safety committee general meetings per year} \par\\ 

\arrayrulecolor{darkgrey}\hline
\rowcolor{darkgrey}Frequency of inspection & &
\listbox{\item Number of inspections per year
\item Frequency of observation of unsafe behaviors} \par\\
\arrayrulecolor{darkgrey}\hline
\rowspace
Frequency of auditing & &
\listbox{\item Number of health and safety management system auditing per year}   \par\\ 

\arrayrulecolor{darkgrey}\hline
\rowcolor{darkgrey}Health and safety management team members commitments  & &
\listbox{\item Number of deliverables per team member 
\item Number of participants in the committee meetings}   \par\\ 
\arrayrulecolor{darkgrey}\hline
\rowspace
Number of precaution procedures & &
\listbox{\item Frequency of early warnings
\item Number of first aid kits and fire drills
\item Number of trained employees for first aid kits and fire drills} \par\\
\bottomrule[2pt]
\end{tabular}
\end{document}

答案2

还有一个可能的解决方案...

  • 使用enumitem和表内的etoolbox包列表itemize适合在表中使用
  • 表格中的水平线使用boldline包中定义的线
  • 对于带有列表的列,定义新的列类型I(参见下面的 MWE)

编辑:

  • cellspace通过使用包装和底部的设置,在单元格中实现更多的顶部垂直空间,如itemize表格所示
  • 删除了未使用的代码片段并更正了列类型定义
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{ragged2e}
\usepackage{boldline,
            cellspace,
            tabularx}
\setlength\cellspacetoplimit{5pt}
\usepackage{enumitem}
\usepackage{etoolbox}
\AtBeginEnvironment{table}{%
    \setlist[itemize]{nosep,     % <-- new list setup
                      leftmargin=*,
                      label=\textbullet,%% <===
                      after=\vspace{-0.8\baselineskip} %% <===
                      }
                           }% end of AtBeginEnvironment
\newcolumntype{I}{>{\csname @minipagetrue\endcsname%
                  \RaggedRight\itemize}X<{\enditemize}
                    }

\begin{document}
    \begin{table}[ht]
    \rowcolors{1}{white}{gray!15}
\begin{tabularx}{\linewidth}{>{\bfseries\RaggedRight}S{p{48mm}} I}%% <===
    \hlineB{2}
KPI & \multicolumn{1}{>{\bfseries}l}{Description} \\
    \hlineB{1.5}
The percentage of health and safety trained managers and employees in the workplace 
    &    \item  The percentage of trained managers for occupational health and safety in the institute.
         \item  The percentage of trained employees for occupational health and safety in the institute.
         \\
Frequency of health and safety committee meetings
    &   \item The number of health and safety committee short meetings per month.
        \item The number of health and safety committee general meetings per year.
        \\
Frequency of inspection 
    &   \item Number of inspections per year.
        \item Frequency of observation of unsafe behaviors.
        \\
Frequency of auditing
    &   \item Number of health and safety management system auditing per year.
        \\
Health and safety management team members commitments 
    &   \item Number of deliverables per team member.
        \item Number of participants in the committee meetings.
        \\
Number of precaution procedures  
    &   \item Frequency of early warnings.
        \item Number of first aid kits and fire drills.
        \item Number of trained employees for first aid kits and fire drills.   
        \\
    \hlineB{2}
\end{tabularx}
    \end{table}
\end{document}

结果是:

在此处输入图片描述

答案3

通过将您的更改\endhead\\并添加所需的包(加上将列类型更改为X以使 tabularx“工作”)这个错误就消失了:

\documentclass{article}
\usepackage{tabularx,booktabs,colortbl}
%\usepackage{xcolor}
\definecolor{darkgrey}{cmyk}{0,0,0,.4}
\begin{document}
\begin{tabularx}{\textwidth}{|p{5cm}|X|} 
\toprule
\textbf{KPI} & \textbf{Description} \\
\toprule
\rowcolor{darkgrey} \textbf{The percentage of health and safety trained managers and employees in the workplace}  & \begin{itemize}
\item The percentage of trained managers for occupational health and safety in the institute.
\item The percentage of trained employees for occupational health and safety in the institute.
\end{itemize}  \\ 
\midrule
 \textbf{Frequency of health and safety committee meetings}  & \begin{itemize}\item The number of health and safety committee short meetings per month.
 \item The number of health and safety committee general meetings per year. 
\end{itemize} \\ 
\midrule
\rowcolor{darkgrey} \textbf{Frequency of inspection} & \begin{itemize}

\item Number of inspections per year.
\item Frequency of observation of unsafe behaviors.
 \end{itemize} \\ 
\midrule
\textbf{Frequency of auditing} & 
\begin{itemize}
\item Number of health and safety management system auditing per year.
\end{itemize}   \\ 
\midrule
\rowcolor{darkgrey}  \textbf{Health and safety management team members commitments}  & 
\begin{itemize}
\item Number of deliverables per team member. 
\item Number of participants in the committee meetings.
\end{itemize}   \\ 
\midrule
\textbf{Number of precaution procedures} & 
\begin{itemize}
\item Frequency of early warnings.
\item Number of first aid kits and fire drills.
\item Number of trained employees for first aid kits and fire drills.
\end{itemize}  \\
\bottomrule
\end{tabularx}
\end{document}

在此处输入图片描述

相关内容