[Tex/LaTex] Longtable with multicolumn header and linebreak issues in multirow

longtablemulticolumnmultirow

I would like to create this table:

enter image description here

The code for this is in the second table. (Thanks to Bernard for this!)
Unfortunately I get two errors because of these two tables.

The first one is: Something's wrong–perhaps a missing \item. \end{tabularx}

The second one is: Misplaced \omit. \end{tabularx}

The Code is:

\documentclass[a4paper, ngerman]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[showframe]{geometry}
\usepackage[table, svgnames]{xcolor}
\usepackage{ragged2e}
\usepackage{tabularx, multirow, caption, makecell, hhline}      
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{ltablex}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TABLE 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{table}[!htbp]
\centering
\setlength\tabcolsep{3pt}
\setlist[itemize]{wide=0pt, labelsep=2pt, leftmargin=*, nosep, before 
=\mbox{}\vspace*{-.7\baselineskip}, after=\vspace*{-.8\baselineskip}}
\caption{\label{tab:Gestaltungsbereiche}Gestaltungsbereiche}
%
\begin{tabularx}{\textwidth}{|c|*{3}{>{\RaggedRight}X|}}
\hhline{~|---|}
\multicolumn{1}{r|}{} & \multicolumn{3}{c|}
{\cellcolor{Gainsboro}Gestaltungsbereiche} \\
\hhline{~|---|}
\rowcolor{Gainsboro} \multicolumn{1}{r|}{\cellcolor{white}} &% 
\multicolumn{1}{>{\centering}X}{\centering Betriebsmittel\newline \centering 
(B)}
\makecell{Betriebsmittel\\ (B)} & \makecell{Organisation\\ (O)} & 
\makecell{Raum- und \\Gebäudetechnik \\ (R)}\\
\hline
\multirow{11}{*}[-1ex]{\rotatebox{90}{Fabrikobjekte}}
& \begin{itemize}
    \item Fertigungseinrichtungen
    \item Montageeinrichtungen
    \item Logistikeinrichtungen
    \item Qualitätseinrichtungen
    \item Informations- und 
    \newline Kommunikations\-einrichtungen
    \item Ver- und Entsorgungs\-einrichtungen
    \item Übergeordnete Systeme
    \item Mobiliar
\end{itemize} %
& \begin{itemize}
    \item Unternehmensstrategie
    \item Logistikkonzept
    \item Prozessgestaltung
    \item Aufbauorganisations\-gestaltung
    \item Arbeitsorganisation
    \item Qualitätssicherungs\-konzept
\end{itemize} %
& \begin{itemize}
    \item Grundstück
    \item Bau\-konstruktion
    \item Technische Anlagen
    \item Außenanlagen
    \item Ausstattung
    \item Konzepte
\end{itemize} \\%
\hline
\end{tabularx}%
\end{table}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TABLE 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage
\keepXColumns
\setlength{\extrarowheight}{3pt}
\begin{tabularx}{\textwidth}{|c|*{2}{>{\RaggedRight}X|}p{9cm}|}
\caption{Betriebsmittel: Fabrikobjekte}
\label{tab:addlabel}%\\
\hhline{~|---|}
\rowcolor{Gainsboro} \multicolumn{1}{r|}{\cellcolor{white}}&
\multicolumn{1}{>{\centering\columncolor{Gainsboro}[\tabcolsep][1em]}X}
{\centering Fabrikobjekte 1.Ordnung}&
\multicolumn{2}{c|}{Fabrikobjekte 2.Ordnung}
\\
\hhline{----}
\endfirsthead
\hhline{~|---|}
\rowcolor{Gainsboro} \multicolumn{1}{r|}{\cellcolor{white}}&
\multicolumn{1}{>{\centering\columncolor{Gainsboro}[\tabcolsep][1em]}X}
{\centering Fabrikobjekte 1.Ordnung}&
\multicolumn{2}{c|}{Fabrikobjekte 2.Ordnung}
 \\
\hhline{----}
\endhead
\multirow{45}{*}[-1ex]{\rotatebox{90}{Gestaltungsfeld: Betriebsmittel}}  
%%%
& \multirow{11}{*}{Fertigungseinrichtung} & \multirow{5}{*}{Maschine} &
Gestell \\
& & & Antriebe\\
& & & Kinematisches System \\
& & & Steuerungs-/ Mess-/Regel- \&
Diagnosesysteme \\
& & & Hilfssysteme / Peripherie \\[0.4ex]
\cline{3-4}
& &
\multirow{3}{*}{Vorrichtung / Hilfmittel} &
Vorrichtungs- \& Spannsysteme \\
& & & Werkstückflusssystem \\
& & & Steuerung \\[0.4ex]
\cline{3-4}& & Werkzeuge & \\[0.4ex]
\cline{2-4}& \multirow{9}{*}{Montageeinrichtungen} & {\multirow{5}{*}
{Montagegrundplatz}} & {Gestell} \\
& & & Antriebe\\
& & & Kinematisches System \\
& & & Steuerungs-/ Mess-/Regel- \& Diagnosesysteme \\
& & & Hilfssysteme / Peripherie \\[0.4ex]
\cline{3-4}
& & \multirow{3}{*}{Vorrichtung / Hilfmittel} & {Vorrichtungs- \& 
Spannsysteme} \\
& & &{Werkstückflusssystem} \\
& & & {Steuerung} \\[0.4ex]
\cline{3-4}& & {Werkzeuge} &  \\[0.4ex]
\cline{2-4}& \multirow{25}{*}{Logistikeinrichtungen} & {\multirow{6}{*}
{Transporteinrichtungen}} & {Gestell} \\
& & & Transportvorrichtung\\
& & & Antriebe\\
& & & Kinematisches System \\
& & & Steuerung \\
& & & Hilfssysteme / Peripherie \\[0.4ex]
\cline{3-4}
& & {\multirow{6}{*}{Handhabungseinrichtungen}} & {Gestell} \\
& & & Handhabungsvorrichtung\\
& & & Antriebe\\
& & & Kinematisches System \\
& & & Steuerung \\
& & & Hilfssysteme / Peripherie \\[0.4ex]
\cline{3-4}

\end{tabularx}%
\end{document}

I appreciate every comment.

My current result for the second table is this:

enter image description here

Best Answer

\centering isn't needed for a longtable: it's automatically centred by default. You don't have to insert a tabularx environment in a longtable to say it will spread obver pages. You simply have to load a package which adds the functionality (and the syntax) of longtable to tabularx. I suggest to use ltablex.

The syntax is that of longtable: you put the \caption inside tabularx, before the declaration for the first table head (these declarations end with `\endfirsthead).

Then I suggest to add the column heads with the declarations for heads of the following heads (which end with \endhead).

I removed all the \multicolumn{1}{l|} in the last column, which prevent line breaking and generate overflowing into the margin.

So here is the code I propose:

\documentclass[a4paper, ngerman]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[showframe]{geometry}
\usepackage[table, svgnames]{xcolor}
\usepackage{ragged2e}
\usepackage{multirow, caption, makecell, hhline}
\usepackage{graphicx}
\usepackage{ltablex}
\keepXColumns
\begin{document}
%\begin{longtable}
%\centering
%\caption{Betriebsmittel: Fabrikobjekte}
\setlength{\extrarowheight}{3pt}
\begin{tabularx}{\textwidth}{|c|*{3}{>{\RaggedRight}X|}}
\caption{Betriebsmittel: Fabrikobjekte}
  \label{tab:addlabel}%\\
    \hhline{~|---|}
     \rowcolor{Gainsboro} \multicolumn{1}{r|}{\cellcolor{white}}&
    \multicolumn{1}{>{\centering\columncolor{Gainsboro}[\tabcolsep][1em]}X}{\centering Fabrikobjekte 1.Ordnung}&
    \multicolumn{2}{c|}{Fabrikobjekte 2.Ordnung}
    \\
    \hhline{----}
\endfirsthead
    \hhline{~|---|}
     \rowcolor{Gainsboro} \multicolumn{1}{r|}{\cellcolor{white}}&
    \multicolumn{1}{>{\centering\columncolor{Gainsboro}[\tabcolsep][1em]}X}{\centering Fabrikobjekte 1.Ordnung}&
    \multicolumn{2}{c|}{Fabrikobjekte 2.Ordnung}
    \\
    \hhline{----}
\endhead
    \multirow{12}{*}[-1ex]{\rotatebox{90}{Gestaltungsfeld: Betriebsmittel}}
     %%%

     & \multirow{11}{*}{Fertigungseinrichtung} & \multirow{5}{*}{Maschine} &
     Gestell \\
     & & & Antriebe\\
     & & & Kinematisches System \\
     & & & Steuerungs-/ Mess-/Regel- \&
     Diagnosesysteme \\
     & & & Hilfssysteme / Peripherie \\[0.4ex]
     \cline{3-4}
     & &
     \multirow{3}[2]{*}{Vorrichtung / Hilfmittel} &
     Vorrichtungs- \& Spannsysteme \\
     & & & Werkstückflusssystem \\
     & & & Steuerung \\[0.4ex]
     \cline{3-4}& & Werkzeuge & \\[0.4ex]
     \cline{2-4}\\
     %%%
    \end{tabularx}%

    \end{document} 

enter image description here

Added :

This code compiles fine with three tables. Note the third table is a shorter code to obtain the second table. I had change the number used in multirow: you must take the total number of lines in the rows the multirow cell must spread over, and not the number of rows, and possibly add a corrective optional argument for the vertical placement of the content.

On the other hand loading ltablex makes all tabularx environments a longtable. Here they are centred, do not float (do not nest them in table) and the caption must be inserted inside the environment.

\documentclass[a4paper, ngerman]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{geometry}
\usepackage[table, svgnames]{xcolor}
\usepackage{ragged2e}
\usepackage{multirow, caption, makecell, hhline}
\usepackage{ltablex}%{tabularx}%
\usepackage{graphicx}
\usepackage{enumitem}

\newcommand\mypar{ \par\hangindent=1.5em\hangafter=1}

\begin{document}

\sffamily\captionsetup{font=sf}

\setlength\tabcolsep{3pt}
\setlist[itemize]{wide=0pt, labelsep=2pt, leftmargin=*, nosep, before
  =\mbox{}\vspace*{-.7\baselineskip}, after=\vspace*{-.8\baselineskip}}
\keepXColumns
%
\begin{tabularx}{\textwidth}{|c|*{3}{>{\RaggedRight\arraybackslash}X|}}
  \caption{\label{tab:Gestaltungsbereiche}Gestaltungsbereiche} \\
  \hhline{~|---|}
  \multicolumn{1}{r|}{} & \multicolumn{3}{c|}
  {\cellcolor{Gainsboro}Gestaltungsbereiche} \\
  \hhline{~|---|}
  \rowcolor{Gainsboro} \multicolumn{1}{r|}{\cellcolor{white}} & %
  \makecell{Betriebsmittel \\ (B)} & \makecell{Organisation\\ (O)} &
  \makecell{Raum- und \\Gebäudetechnik \\ (R)}\\
  \hline
  \endfirsthead
  \multirow{11}{*}[-1ex]{\,\rotatebox{90}{Fabrikobjekte}\,}
                                                              &
  \begin{itemize}
    \item Fertigungseinrichtungen
    \item Montageeinrichtungen
    \item Logistikeinrichtungen
    \item Qualitätseinrichtungen
    \item Informations- und
          Kommunikations\-einrichtungen
    \item Ver- und Entsorgungs\-einrichtungen
    \item Übergeordnete Systeme
    \item Mobiliar
  \end{itemize} %
                                                              & \begin{itemize}
    \item Unternehmensstrategie
    \item Logistikkonzept
    \item Prozessgestaltung
    \item Aufbauorganisations\-gestaltung
    \item Arbeitsorganisation
    \item Qualitätssicherungs\-konzept
  \end{itemize} %
                                                              & \begin{itemize}
    \item Grundstück
    \item Bau\-konstruktion
    \item Technische Anlagen
    \item Außenanlagen
    \item Ausstattung
    \item Konzepte
  \end{itemize}
  \\%
  \hline
\end{tabularx}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TABLE 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage
\setlength{\extrarowheight}{2pt}
\begin{tabularx}{\textwidth}{|c|*{2}{>{\RaggedRight}X|}>{\RaggedRight}p{4.8cm}|}
  \caption{Betriebsmittel: Fabrikobjekte}
  \label{tab:addlabel} \\
  \hhline{~|---|}
  \multicolumn{1}{r|}{\cellcolor{white}} &
  \cellcolor{Gainsboro}\centering Fabrikobjekte 1.Ordnung &
  \multicolumn{2}{c|}{\cellcolor{Gainsboro}Fabrikobjekte 2.Ordnung}
  \\
  \hhline{----}
  \endfirsthead
  \hhline{~|---|}
  \rowcolor{Gainsboro} \multicolumn{1}{r|}{\cellcolor{white}} &
  \multicolumn{1}{>{\centering\columncolor{Gainsboro}[\tabcolsep][1em]}X}
  {\centering Fabrikobjekte 1.Ordnung} &
  \multicolumn{2}{c|}{Fabrikobjekte 2.Ordnung}
  \\
  \hhline{----}
  \endhead
  \hhline{----}
  \endfoot
  \multirow{32}{*}{\,\rotatebox{90}{Gestaltungsfeld: Betriebsmittel}\,}
  %
                                                              & \multirow{11}{=}{Fertigungseinrichtung} & \multirow{6}{*}{Maschine} &
  Gestell \\
                                                              & & & Antriebe \\
                                                              & & & Kinematisches System \\
                                                              & & & Steuerungs-/ Mess-/Regel- \& \par\quad Diagnosesysteme \\
                                                              & & & Hilfssysteme / Peripherie \\[0.4ex]
  \cline{3-4}
                                                              & &
  \multirow{3}{=}{Vorrichtung / Hilfmittel} &
  Vorrichtungs- \& Spannsysteme \\
                                                              & & & Werkstückflusssystem \\
                                                              & & & Steuerung \\[0.4ex]
  \cline{3-4} & & Werkzeuge & \\[0.4ex]
  \cline{2-4} & \multirow{11}{=}[-2pt]{Montageeinrichtungen} &
  \multirow{6}{=}{Montagegrundplatz} & Gestell \\
                                                              & & & Antriebe \\
                                                              & & & Kinematisches System \\
                                                              & & & Steuerungs-/ Mess-/Regel- \&\par\quad Diagnosesysteme \\
                                                              & & & Hilfssysteme / Peripherie \\[0.4ex]
  \cline{3-4}
                                                              & & \multirow{3}{=}{Vorrichtung / Hilfmittel} & {Vorrichtungs- \& Spannsysteme} \\
                                                              & & & Werkstückflusssystem \\
                                                              & & & Steuerung \\[0.4ex]
  \cline{3-4} & & {Werkzeuge} & \\[0.4ex]
  \cline{2-4} & \multirow{12}{=}{Logistikeinrichtungen} &
  \multirow{6}{=}{Transporteinrichtungen} & Gestell \\
                                                              & & & Transportvorrichtung \\
                                                              & & & Antriebe \\
                                                              & & & Kinematisches System \\
                                                              & & & Steuerung \\
                                                              & & & Hilfssysteme / Peripherie \\[0.4ex]
  \cline{3-4}
                                                              & & \multirow{6}{=}{Handhabungseinrichtungen} & Gestell \\
                                                              & & & Handhabungsvorrichtung \\
                                                              & & & Antriebe \\
                                                              & & & Kinematisches System \\
                                                              & & & Steuerung \\
                                                              & & & Hilfssysteme / Peripherie \\[0.4ex]
\end{tabularx}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TABLE 2 bis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newpage
\setlength{\extrarowheight}{3pt}
\begin{tabularx}{\textwidth}{|c|*{2}{>{\RaggedRight}X|}>{\RaggedRight\everypar{\hangindent=1.5em\hangafter=1}}p{4.8cm}|}
  \caption{Betriebsmittel: Fabrikobjekte}
  \label{tab:addlabel} \\
  \hhline{~|---|}
  \multicolumn{1}{r|}{\cellcolor{white}} &
  \cellcolor{Gainsboro}\centering Fabrikobjekte 1.Ordnung &
  \multicolumn{2}{c|}{\cellcolor{Gainsboro}Fabrikobjekte 2.Ordnung}
  \\
  \hhline{----}
  \endfirsthead
  \hhline{~|---|}
  \rowcolor{Gainsboro} \multicolumn{1}{r|}{\cellcolor{white}} &
  \multicolumn{1}{>{\centering\columncolor{Gainsboro}[\tabcolsep][1em]}X}
  {\centering Fabrikobjekte 1.Ordnung} &
  \multicolumn{2}{c|}{Fabrikobjekte 2.Ordnung}
  \\
  \hhline{----}
  \endhead
  \hhline{----}
  \endfoot
  \multirow{27}{*}{\,\rotatebox{90}{Gestaltungsfeld: Betriebsmittel}\,}
  %
                                                              & \multirow{11}{=}{Fertigungseinrichtung} & \multirow{6}{=}{Maschine}
                                                              & Gestell \par Antriebe \par Kinematisches System \par Steuerungs-/ Mess-/Regel- \& Diagnosesysteme \par Hilfssysteme / Peripherie \\[0.4ex]
  \cline{3-4}
                                                              & & \multirow{3}{=}{Vorrichtung / Hilfmittel} &
  Vorrichtungs- \& Spannsysteme \par Werkstückflusssystem \par Steuerung \\[0.4ex]
  \cline{3-4} & & Werkzeuge & \\[0.4ex]
  \cline{2-4} & \multirow{11}{=}[-2pt]{Montageeinrichtungen} & \multirow{6}{=}{Montagegrundplatz} %
                                                              & Gestell \par Antriebe \par Kinematisches System \par Steuerungs-/ Mess-/Regel- \& Diagnosesysteme \par Hilfssysteme / Peripherie \\[0.4ex]
  \cline{3-4}
                                                              & & \multirow{3}{=}{Vorrichtung / Hilfmittel} &
  Vorrichtungs- \& Spannsysteme \par Werkstückflusssystem \par Steuerung \\[0.4ex]
  \cline{3-4} & & {Werkzeuge} & \\[0.4ex]
  \cline{2-4} & \multirow{12}{=}{Logistikeinrichtungen} & \multirow{6}{*}{Transporteinrichtungen}
                                                              & Gestell \par Transportvorrichtung \par Antriebe \par Kinematisches System \par Steuerung \par Hilfssysteme / Peripherie \\[0.4ex]
  \cline{3-4}
                                                              & & \multirow{6}{=}{Handhabungseinrichtungen}
                                                              & Gestell \par Handhabungsvorrichtung \par Antriebe \par Kinematisches System \par Steuerung \par Hilfssysteme / Peripherie \\[0.4ex]
\end{tabularx}%

\end{document} 

enter image description here enter image description here enter image description here

Related Question