當我這樣做時:
\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
我建議您的表格佈局略有不同。正如您從示例中看到的,第一個和第二個中的文本柱子不在同一基線上,這被認為是不好的排版。此外,當您使用陰影時,所有垂直和水平規則都是不必要的。另外,你會得到散佈在各處的白線,因為\rowcolor
和booktabs
- 規則、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}