[Tex/LaTex] Tables: Automatic adjustment of columnwidth to its content gives error “Misplaced \noalign” at \toprule

horizontal alignmentminipagesubfloatstablesvertical alignment

I am trying to put two tables next to each other, vertically aligned top, with some below listed adjustments I unfortunately can't get together.

First, I'd like to know if/how I can avoid the error ! Misplaced \noalign in combination with \toprule and \bottomrule before {tabular}. Inside of tabular the \...rule would work, but I'd like to have one single line above and below the tables.

Also

  1. How can i adjust the width of some columns automatically to it's widest content (to avoid the fixed L{xxcm} at the tabular definition)?

  2. How is it possible to make the content of the first row centered without a multicolumn for each entry?

  3. Where and how do I properly set the width of both tables together to for example 75% of the pagewidth (at the environment of mytable, subfloat, [minipage]?)

These things I'd like to apply to the example below.

Many thanks in advance

\documentclass[a4paper,oneside,titlepage]{scrbook}
%\usepackage[T1]{fontenc} %Silbentrennung
%\usepackage[utf8]{inputenc} % Umlaute
%\usepackage[ngerman]{babel}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{subfig} 
\newcommand{\mW}[2]{$\overline{#1}=#2$} 
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\captionsetup[subfloat]{position=top}

\newenvironment{mytable}[1][htb] {%
    \begin{table}[#1]\small
    }
    {\end{table}
    } 


\begin{document}

\begin{mytable}
    \centering
    \caption{Worms}
    \toprule
    \subfloat{%
    %\begin{minipage}[c]{6.5cm}
        \begin{tabular}{L{1.6cm}R{1.2cm}R{0.7cm}R{0.7cm}R{0.5cm}}
            Klasse & Zeit & y & x & Bez. \\
            \midrule
            0,0 - 2,1& a, 16:45 & 0,0   & 1,0   & H1 \\
            \mW{x}{1,1}& a, 16:56 & 5,0   & 0,5   & H2 \\
            & a, 17:15 & 10,0  & 1,0   & H3 \\
            & a, 17:37 & 15,0  & 2,1   & H4 \\
            & b, 12:40 & 37,4  & 1,0   & H7 \\
            \midrule
            5,0 - 6,0& a, 16:20 & 0,0   & 5,0   & I1 \\
            \mW{x}{5,2}& a, 16:58 & 5,0   & 5,0   & I2 \\
            & a, 17:19 & 10,0  & 6,0   & I3 \\
            & b, 12:39 & 37,4  & 5,0   & I7 \\
            \midrule
            10,0 - 12,1& a, 16:30 & 0,0   & 10,0  & J1 \\
            \mW{x}{10,4}& a, 17:00 & 5,0   & 10,0  & J2 \\
            & b, 12:44 & 31,0  & 10,0  & J6 \\
            & b, 12:38 & 37,4  & 10,0  & J7 \\
        \end{tabular}%

        \label{tab:khlkjlkjlk}%
    %\end{minipage}
    }
    \quad
    \subfloat{
    %\begin{minipage}[c]{6.5cm}
        \begin{tabular}{L{1.6cm}R{1.2cm}R{0.7cm}R{0.7cm}R{0.5cm}}
            Klasse & Zeitpunkt & y & x & Bez. \\
            \midrule
            15,0 - 17,1& a, 16:32 & 0,0   & 15,0  & K1 \\
            \mW{x}{15,7}& a, 17:04 & 5,0   & 15,0  & K2 \\
            & a, 17:25 & 10,0  & 16,0  & K3 \\
            & a, 17:49 & 15,0  & 17,1  & J4 \\
            & b, 12:36 & 37,4  & 15,0  & K7 \\
            & b, 08:24 & 37,4  & 16,5  & M7 \\
            & b, 12:50 & 37,4  & 16,5  & M7 \\
            \midrule
            18,6 - 20,6 & a, 16:38 & 0,0   & 20,0  & L1 \\
            \mW{x}{19,7}& b, 06:13 & 5,0   & 18,6  & M2 \\
            & b, 11:05 & 5,0   & 18,6  & M2 \\
            & a, 17:07 & 5,0   & 19,0  & L2 \\
            & b, 06:17 & 7,5   & 20,2  & M3 \\
            & b, 06:21 & 15,0  & 20,6  & M4 \\
            & b, 06:25 & 23,1  & 20,3  & M5 \\
            & b, 06:30 & 31,0  & 19,7  & M6 \\   
        \end{tabular}%
        \label{tab:blablabel}%
    %\end{minipage}
    }
    \bottomrule
\end{mytable}
\end{document}

enter image description here

[Edit author: Changed title is misleading!
Should be something like that: 1.) Automatic adjustment of columnwidth to its content 2.) '!Misplaced \noalign' at \toprule 3.) Horizontal alignment of the contents of a row
]

Best Answer

You can't use \toprule outside of a tabular environment.

Here's a way to get your table without \subfloat, which is not useful here. I simply set the two tables inside an "overall" table for the purpose of \toprule and \bottomrule; setting explicit widths is time consuming because the width have to be computed in order not to get overfull boxes; I think that it's better letting TeX doing the work.

The two inner tables are set with the [t] option to get them aligned at their tops.

\documentclass[a4paper,oneside,titlepage,draft]{scrbook}
\usepackage[T1]{fontenc} %Silbentrennung
\usepackage[utf8]{inputenc} % Umlaute
\usepackage[ngerman]{babel}
\usepackage{booktabs}
\usepackage{tabularx}

\newcommand{\mW}[2]{$\overline{#1}=#2$} 

\newenvironment{mytable}[1][htb]
 {\begin{table}[#1]\small}
 {\end{table}} 


\begin{document}

\begin{mytable}
\centering
\caption{Worms}
\begin{tabular}{@{}c@{}}% an "outside" table
\toprule
\begin{tabular}[t]{lrrrr}
Klasse & Zeit & y & x & Bez. \\
\midrule
0,0 - 2,1   & a, 16:45 & 0,0   & 1,0   & H1 \\
\mW{x}{1,1} & a, 16:56 & 5,0   & 0,5   & H2 \\
            & a, 17:15 & 10,0  & 1,0   & H3 \\
            & a, 17:37 & 15,0  & 2,1   & H4 \\
            & b, 12:40 & 37,4  & 1,0   & H7 \\
\midrule
5,0 - 6,0   & a, 16:20 & 0,0   & 5,0   & I1 \\
\mW{x}{5,2} & a, 16:58 & 5,0   & 5,0   & I2 \\
            & a, 17:19 & 10,0  & 6,0   & I3 \\
            & b, 12:39 & 37,4  & 5,0   & I7 \\
\midrule
10,0 - 12,1 & a, 16:30 & 0,0   & 10,0  & J1 \\
\mW{x}{10,4}& a, 17:00 & 5,0   & 10,0  & J2 \\
            & b, 12:44 & 31,0  & 10,0  & J6 \\
            & b, 12:38 & 37,4  & 10,0  & J7 \\
\end{tabular}% <- REMOVE THIS % IF YOU WANT A SMALL SEPARATION BETWEEN THE TABLES
\begin{tabular}[t]{lrrrr}
Klasse & Zeitpunkt & y & x & Bez. \\
\midrule
15,0 - 17,1 & a, 16:32 & 0,0   & 15,0  & K1 \\
\mW{x}{15,7}& a, 17:04 & 5,0   & 15,0  & K2 \\
            & a, 17:25 & 10,0  & 16,0  & K3 \\
            & a, 17:49 & 15,0  & 17,1  & J4 \\
            & b, 12:36 & 37,4  & 15,0  & K7 \\
            & b, 08:24 & 37,4  & 16,5  & M7 \\
            & b, 12:50 & 37,4  & 16,5  & M7 \\
\midrule
18,6 - 20,6 & a, 16:38 & 0,0   & 20,0  & L1 \\
\mW{x}{19,7}& b, 06:13 & 5,0   & 18,6  & M2 \\
            & b, 11:05 & 5,0   & 18,6  & M2 \\
            & a, 17:07 & 5,0   & 19,0  & L2 \\
            & b, 06:17 & 7,5   & 20,2  & M3 \\
            & b, 06:21 & 15,0  & 20,6  & M4 \\
            & b, 06:25 & 23,1  & 20,3  & M5 \\
            & b, 06:30 & 31,0  & 19,7  & M6 \\
\end{tabular}\\% end the "outside" table line
\bottomrule
\end{tabular}
\end{mytable}
\end{document}

enter image description here

Related Question