2

I have a question regarding tables in Latex. I tried to find a solution now for two hours, but couldn't fix all my problems in one code. So I found single solutions for everything, but couldn't bring them together.

I added some code and a picture to explain the table I want to have in the end. Unfortunately there are some points I don't like and hope someone can help me. I would like to have:

  1. No white colored spaces above and below the lines
  2. I would like to adjust the spacing between the lines generally
  3. The table shall have exactly the width of my text
  4. I want to adjust the width of all columns by my self specifying in percent
  5. I want in some cases have the first column colored differently
  6. I want the text in my head row to be white
  7. I need to use footnotes in the table. It doesn't matter if they are explained below the table or at the page bottom.
  8. I want to change the horizontal alignment of the cells with numbers in it to right align.
  9. I'm open to the package to use, so if needed feel free to propose another one :-)

Thank you very much for your help.

\documentclass{scrbook}

\usepackage{lipsum}

\usepackage{tabularx} 
\usepackage{booktabs}

\usepackage{colortbl}
\definecolor{Gray}{rgb}{0.80784, 0.86667, 0.90196} %dunkelblau
\definecolor{Lightgray}{rgb}{0.9176, 0.95, 0.95686} %hellblau
\definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294} %akzentfarbe

\begin{document}

\lipsum[2]
\begin{table}
\begin{center}
\begin{tabularx}{\textwidth}{p{.15\linewidth} p{.1\linewidth} p{.2\linewidth} p{.2\linewidth} X}
    \toprule
    \rowcolor{Akzent}
    Category & Size & Maximum Takeoff Weight (MTOW) (kg) & Normal Operating Altitude (ft) & Airspeed (m/s)\\
    \midrule
    \rowcolor{Gray}
    Group 1 & Small & 0 - 20 & < 1,200 AGL & < 100\\
    \rowcolor{Lightgray}
    Group 2 & Medium & 21 - 55 & < 3500 & < 250\footnote{Explanation}\\
    \bottomrule
\end{tabularx}
\caption{Table}
\end{center}
\end{table}

\end{document}

enter image description here

1
  • Welcome to TeX.SX! (1) Have a look at this post, (3) It should have according to your option, (4) you already do (except last column), (5) have a look at \cellcolor, (6) see (1), (7) there is a \makesavenoteenv (package \footnote), (8) use a >{\raggedleft}p{.1\linewidth}. These are just suggestions at first glance and may not work out perfectly, but see it as starting point.
    – TeXnician
    Feb 24, 2017 at 9:51

3 Answers 3

4

Here is something that looks nicer, though far from perfect. For point 1, it's due to the padding that booktabs adds above and/or below its rules. So I had to suppress this padding, and replace it with \extrarowsep, for \belowrulesep and with a rule of the same thickness as below, and same colour as the row colour, for aboverulesep. To ease doing so, I defined a \colourpadding command, with one argument, the colour one wishes.

There might have been a simpler solution with the \makegapedcells command from makecell. Unfortunately, it appears not to be compatible with colouring tables.

Point 2: There are several ways to do that. The simplest is changing the value of arraystretch. It has the inconvenience that for bib values, the contents is no more centred in its cell. If necessary, you can use \arraystretch=some length, which adds a length at the top of cells in each row. Two last solutions: makecell has two commands: \setcellgapes{some length} and \makegapedcells}, which adds a fixed length at the top and bottom of all cells in a table. It does not work here. Last solution: cellspace defines minimal distances between the top of a cell and the line above (or the bottom of the row above), and between the bottom of a cell and the line below.

Point 3. It's done with tabularx. I preferred to use 3 X columns, and slightly change the first two columns widths.

Point 4: I don't see what you want exactly.

Point 5: I think you would have to colour differently each cell of the first column, via \cellcolor.

Point 6 is done.

Point 7: Footnotes can be put at the bottom of the table with the threeparttable package (ot threeparttablex for long tables). For usual footnote, you have the tablefootnote package.

Point 8. To have numbers right-aligned, you can add >{\raggedleft} to the column specifier, or choose the S column type (load siunitx).

\documentclass{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{ragged2e, eqparbox}
\newcolumntype{M}[1]{>{\RaggedRight}m{#1} <{\hspace*{-1pt}}}

\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}

\usepackage[table]{xcolor}
\definecolor{Gray}{rgb}{0.80784, 0.86667, 0.90196} %dunkelblau
\definecolor{Lightgray}{rgb}{0.9176, 0.95, 0.95686} %hellblau
\definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294} %akzentfarbe
\newcommand{\whitehead}{\bfseries\color{white}}

\newcommand\colourpadding[1]{\addlinespace[-1pt]\arrayrulecolor{#1}\midrule[6pt]\arrayrulecolor{black}}

\begin{document}

\lipsum[2]
\begin{table}
\centering
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\setlength\doublerulesep{0pt}
\setlength\extrarowheight{5pt}
\begin{tabularx}{\textwidth}{M{.12 \linewidth}M{.08\linewidth}*{3}{X}} \toprule
    \rowcolor{Akzent}
 \whitehead Category & \whitehead Size &\whitehead Maximum Takeoff Weight (MTOW) (kg) &\whitehead Normal Operating Altitude (ft) &\whitehead Airspeed (m/s)\\
    \colourpadding{Akzent}
        \midrule
        \rowcolor{Gray}
        Group 1 & Small & 0 – 20 & \eqmakebox[A][l]{< 1,200 AGL} & < 100\\
    \colourpadding{Gray}
        \rowcolor{Lightgray}
        Group 2 & Medium & 21 – 55 & \eqmakebox[A][l]{< 3500} & < 250\rlap{\footnote{Explanation}}\\
\colourpadding{Lightgray}
    \bottomrule
\end{tabularx}
\caption{Table}
\end{table}

\end{document} 

enter image description here

2
  • Hi Bernhard, thank you very much for this very helpful answer. The table looks nearly as I intended. Just a few questions regarding the column width & alignment. P4: Let's say for any reason I want to design the table such, that all columns are 20% (of textwidth) wide. How could I do that (or any other distribution of the width). Do you understand my question now? Sorry for my english :-) Do you know a website with a good overview about the cell specifiers (M, X, S, l, c, ..), for me as beginner it's a bit of an overkill and I think many question could be answered by knowledge of them :-) Mar 1, 2017 at 8:15
  • This wikibook gives the essentials, I think. Missing: some specific packages like siunitx which powerful tools for columns of numbers, makecell (line breaking in l, r or c cells) and ltablex (functionalities of longtable in tabularx). For you precise question (equal width columns), I would use \begin{tabularx}{\linewidth}{*{5}{X}}. X is a \p{length} column type, with length calculated by the package so the total width is \linewidth. You can see details on how to customise X in the package documentation.
    – Bernard
    Mar 1, 2017 at 9:47
1

With exploit boldline from Shipunov bundle for line width and celspace for additional vertical space in cells:

enter image description here

\documentclass{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\usepackage[table]{xcolor}
  \definecolor{Gray}{rgb}{0.80784, 0.86667, 0.90196} %dunkelblau
  \definecolor{Lightgray}{rgb}{0.9176, 0.95, 0.95686} %hellblau
  \definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294} %akzentfarbe
\usepackage{tabularx}
  \newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage{cellspace}
  \setlength\cellspacetoplimit{5pt}
  \setlength\cellspacebottomlimit{5pt}
  \addparagraphcolumntypes{C}
\usepackage{boldline}% part of Shipunov bundle

\begin{document}
    \lipsum[1]
\begin{table}[ht]
    \centering
\newcommand\whitehead[1]{\textcolor{white}{\bfseries #1}}
\renewcommand\tabularxcolumn[1]{b{#1}}
\setlength\extrarowheight{4pt}
    \begin{tabularx}{\textwidth}{Sl
                                  l
                             *{3}{C}
                                }
    \hlineB{2}  % <-- defined in the package "boldline", thicknes=2pt
    \rowcolor{gray}
\whitehead{Category}
    &   \whitehead{Size}
        &    \whitehead{Maximum Take-off Weight (MTOW) (kg)}
            &   \whitehead{Normal Operating Altitude (ft)}
                &   \whitehead{Airspeed (m/s)}              \\
    \hlineB{1}
    \rowcolor{gray!10}
        Group 1 & Small  &  0 – 20 & < 1,200 AGL    & < 100 \\
    \rowcolor{gray!20}
        Group 2 & Medium & 21 – 55 & < 3500         & < 250 \rlap{\footnote{Explanation}}\\
    \hlineB{2}
\end{tabularx}
    \caption{Table}
\end{table}
    \lipsum[2]

\end{document}
1

Here is what you can do with {NiceTabular} of nicematrix. The main drawback of {NiceTabular} is that it can't be broken (like longtable).

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage{nicematrix}
\usepackage{lipsum}
\usepackage{booktabs}
\usepackage{enumitem}

\definecolor{Gray}{rgb}{0.80784, 0.86667, 0.90196} %dunkelblau
\definecolor{Lightgray}{rgb}{0.9176, 0.95, 0.95686} %hellblau
\definecolor{Akzent}{rgb}{0.6627, 0.63529, 0.55294} %akzentfarbe

\begin{document}

\lipsum[1]

\begin{table}
\NiceMatrixOptions{cell-space-top-limit=2pt}
\begin{NiceTabular}{m[l]{.12 \linewidth}m[l]{.08\linewidth}*{3}{X[m,c]}}[colortbl-like]
\toprule
\rowcolor{Akzent}
\RowStyle[color=white,bold]{\sffamily}
 Category & Size & Maximum Takeoff Weight (MTOW) (kg) & Normal Operating Altitude (ft) & Airspeed (m/s)\\
\midrule
\rowcolor{Gray}
Group 1 & Small & 0 -- 20 & < 1,200 AGL & < 100\\
\rowcolor{Lightgray}
Group 2 & Medium & 21 -- 55 & < 3500 & < 250\tabularnote{Explanation}\\
\bottomrule
\end{NiceTabular}
\caption{Table}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .