2

I am trying to automate table generation using latex. However I cannot seem to get a multirow cell centre aligned vertically.

The table I am working on may span multiple pages, and may have multiple lines in any of the cells. I can manually count the number of lines of a particular multirow cell, however I would like to do that from a script. What are my options?enter image description here

\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
  • do you in use of \multirow consider all lines of text, which it span? for example, i suspect that instead of \multirow{6}{=}{Use phase: setting} should be \multirow{12}{=}{Use phase: setting} . however, it is not realistic, that this correction will put text in the middle. of multi row cell since it not consider vertical spaces of cmidrules in spanned text lines. for this is intended manual tweaking of text position provided with multirow command (see package documentation}.
    – Zarko
    Dec 20, 2017 at 6:58

1 Answer 1

1

A possible solution with tblr environment of tabularray package:

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.