长表环境中多行多行单元格的自动垂直居中

长表环境中多行多行单元格的自动垂直居中

我正在尝试使用 latex 自动生成表格。但是我似乎无法使多行单元格垂直居中对齐。

我正在处理的表格可能跨越多页,并且任何单元格中可能有多行。我可以手动计算特定多行单元格的行数,但我想通过脚本来执行此操作。我有什么选择?在此处输入图片描述

\documentclass[version=last,fontsize=10pt]{scrartcl}

\usepackage{fontspec}
\setmainfont{Arial}
\usepackage{helvet}
\usepackage[landscape]{geometry}
\usepackage{array, makecell, multirow, bigdelim}
\usepackage{booktabs}
\usepackage{ragged2e}
\usepackage{longtable}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}

\geometry{a4paper,left=1.5cm,right=1.5cm,top=1.5cm,bottom=2cm}

% formatting for the headline, etc
\newcommand{\hl}{\centering\textbf}

%===================================
\begin{document}

\begin{longtable}{
  c
  >{\raggedright\arraybackslash}m{14mm}
  >{\raggedright\arraybackslash}m{16mm}
  >{\raggedright\arraybackslash}m{23mm}
  >{\raggedright\arraybackslash}m{33mm}
  >{\raggedright\arraybackslash}m{49mm}
  >{\raggedright\arraybackslash}m{81mm}
  c }
\toprule
\multirow{3}{8mm}{\hl{Ref. no.}} &
\multirow{3}{=}{\hl{Life cycle}} &
\multirow{3}{=}{\hl{Task}} &
\multirow{3}{=}{\hl{Hazard zone}} &
\multicolumn{3}{c}{\hl{Accident scenario}} &
\multirow{3}{8mm}{\hl{Ref. no.}} \\ \cmidrule{5-7}
& & & &
\hl{Hazard} &
\hl{Hazardous situation} &
\hl{Hazardous event} & \\ \midrule
\endhead

% -----
1 &
\multirow{6}{=}{Use phase: setting} &
\multirow{4}{=}{Changing tool} &
\multirow{6}{=}{Working zone} &
\multirow{6}{=}{Cutting of fingers or hands at sharp tool edges} &
Working near/with the tool (handling/mounting) &
Contact with sharp edges due to careless or loss of balance &
1 \\ \cmidrule{6-6}
% -----
2 &
&
&
&
&
\multirow{2}{=}{Working near/with the tool (fastening/unfastening)} &
Contact with the sharp edges due to carelessness or loss of balance &
2 \\
% -----
3 &
&
&
&
&
&
Contact with sharp edges due to loss of control, tool slipping by use of unsuitable hand tools &
3 \\ \cmidrule{6-6}
% -----
4 &
&
&
&
&
Working near/with the tool (handing, mounting and fasening) &
Unexpected start-up inducing contact with moving parts &
4 \\ \cmidrule{3-3} \cmidrule{6-6}
% -----
5 &
&
\multirow{2}{=}{Mounting, adjusting guide} &
&
&
\multirow{2}{=}{Working near the tool} &
Contact with sharp edges due to carelessness &
5 \\
% -----
6 &
&
&
&
&
&
Unexpected start-up inducing contact with moving parts &
6 \\ \cmidrule{2-2} \cmidrule{3-3} \cmidrule{4-4} \cmidrule{5-5} \cmidrule{6-6}
% -----
7 &
Use phase: setting &
Changing spindle speed &
Transmission zone &
Crushing of fingers or hands by rotating elements (between pulleys and belt) &
Working near the transmission system (e.g. check/inspection of transmission with machine running &
Access to/contact with moving parts due to a design error &
7 \\
\bottomrule
\end{longtable}

\end{document}

答案1

tblr环境可能的解决方案tabularray包裹:

\documentclass[version=last,fontsize=10pt]{scrartcl}

\usepackage{fontspec}
\setmainfont{Arial}
\usepackage{helvet}
\usepackage[landscape]{geometry}
\usepackage{ragged2e}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\geometry{a4paper,left=1.5cm,right=1.5cm,top=1.5cm,bottom=2cm}

% formatting for the headline, etc
\newcommand{\hl}{\centering\textbf}

\begin{document}

\begin{longtblr}[
  caption = {Long Table Caption},
]{
  colspec = {Q[c,8mm]Q[l,m,14mm]Q[l,m,16mm]Q[l,m,23mm]Q[l,m,33mm]Q[l,m,49mm]Q[l,m,81mm]Q[c,8mm]},
  rowhead = 2,
  row{1,2} = {c,font=\bfseries},
  cell{1}{5} = {c=3}{c}, % multicolumn
  cell{1}{1-4,8} = {r=2}{c}, % multirow
}
\toprule
 Ref. no. & Life cycle & Task & Hazard zone & Accident scenario &          &                 & Ref. no. \\
\cmidrule{5-7}
          &            &      &             & Hazard & Hazardous situation & Hazardous event & \\
\midrule
1 &
\SetCell[r=6]{} Use phase: setting &
\SetCell[r=4]{} Changing tool &
\SetCell[r=6]{} Working zone &
\SetCell[r=6]{} Cutting of fingers or hands at sharp tool edges &
Working near/with the tool (handling/mounting) &
Contact with sharp edges due to careless or loss of balance &
1 \\
\cmidrule{6-6}
2 &
&
&
&
&
\SetCell[r=2]{} Working near/with the tool (fastening/unfastening) &
Contact with the sharp edges due to carelessness or loss of balance &
2 \\
3 &
&
&
&
&
&
Contact with sharp edges due to loss of control, tool slipping by use of unsuitable hand tools &
3 \\
\cmidrule{6-6}
4 &
&
&
&
&
Working near/with the tool (handing, mounting and fasening) &
Unexpected start-up inducing contact with moving parts &
4 \\
\cmidrule{3-3} \cmidrule{6-6}
5 &
&
\SetCell[r=2]{} Mounting, adjusting guide &
&
&
\SetCell[r=2]{} Working near the too &
Contact with sharp edges due to carelessness &
5 \\
6 &
&
&
&
&
&
Unexpected start-up inducing contact with moving parts &
6 \\
\cmidrule{2-2} \cmidrule{3-3} \cmidrule{4-4} \cmidrule{5-5} \cmidrule{6-6}
7 &
Use phase: setting &
Changing spindle speed &
Transmission zone &
Crushing of fingers or hands by rotating elements (between pulleys and belt) &
Working near the transmission system (e.g. check/inspection of transmission with machine running &
Access to/contact with moving parts due to a design error &
7 \\
\bottomrule
\end{longtblr}

\end{document}

在此处输入图片描述

相关内容