2

In this Thread (https://tex.stackexchange.com/a/301036/229635) it was explained how to make the small vertical lines between two cells disappear by using tabularx and \columncolor and changing the overhang parameters within a multicolumn.

Now I am looking for a way how to create a Table like shown in the picture attached to this post.

The reason why I am asking for your help is that the code updated on August 7th 2020 by Bernard (see link above) doesn't create a table fitting to the text width of my document.

In other words: Is there a way to get rid of all the absolute values and the multicolumn used in the m{} parameters so that the table fits still the textwidth no matter how many columns the table has while still implementing the overhang parameter for color fill to avoid the thin white lines between the cells? I Want to create a Table having no gridlines and a light gray head row without the small vertical lines between color filled cells while using \cellcolor.

Thanks a lot in advance, Benson_G

Picture Reference: Worm, M.; Leibig, D.; Dingels, C.; Frey, H. Cleavable Polyethylene Glycol: 3,4-Epoxy-1-butene as a Comonomer to Establish Degradability at Physiologically Relevant pH.ACS Macro Letters2016,5(12), 1357–1363

enter image description here

\documentclass[a4paper,10pt, enabledeprecatedfontcommands]{scrartcl} %Art des Dokuments, Schriftgröße etc.
\usepackage[utf8]{inputenc}%Direkte Angabe von Umlauten möglich 
\usepackage[left=3cm,right=2cm,top=2cm,bottom=2cm,includehead]{geometry}%Maße für die wissenschaftliche Arbeit
\usepackage[parfill]{parskip} %Bessere Seitenumbrüche
\usepackage[onehalfspacing]{setspace} %Zeilenabstand
\usepackage[T1]{fontenc} %Silbentrennung
\usepackage[english]{babel} %Sprachanpassungen
\usepackage{graphicx} %Einfügen von Bildern
\usepackage{tabularx} %Tabellen
\usepackage{booktabs} %Weitere Möglichkeiten für Tabellen
\usepackage{here} %Lage von Tabellen und Bildern 
\usepackage{units}
\usepackage{physics}
\usepackage{amsmath}
\usepackage{textgreek}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage[labelformat=simple]{subcaption}
\usepackage{float}
\usepackage{pdfpages}
\usepackage{lmodern} 
\usepackage{enumitem}
\usepackage[hyphens]{url}  %% be sure to specify the option 'hyphens'
% \usepackage[pdftex,bookmarksnumbered,hidelinks,breaklinks]{hyperref}
\usepackage[superscript]{cite}
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault} 
\usepackage[version=3]{mhchem} %Summenformeln angeben
\usepackage[labelsep=period]{caption}
\usepackage[per-mode = reciprocal, output-decimal-marker={,}, exponent-product = {\cdot}]{siunitx} %Si-Einheiten
% \usepackage[colorlinks,
% pdfpagelabels,
% pdfstartview = FitH,
% bookmarksopen = true,
% bookmarksnumbered = true,
% linkcolor = black,
% plainpages = false,
% hypertexnames = false,
% citecolor = black]{hyperref}
\usepackage{color}
\usepackage{colortbl}

\definecolor{hellgrau}{rgb}{0.90,0.90,0.90}

\newcommand{\RM}[1]{\MakeUppercase{\romannumeral #1{.}}}

\renewcommand\thesubfigure{\thefigure.\arabic{subfigure}}
\addtokomafont{captionlabel}{\bfseries}

\DeclareNewTOC[
  type=scheme,                         % Name der Umgebung
  types=schemes,                       % Erweiterung (\listofschemes)
  float,                               % soll gleiten
  floatpos=tbp,                        % voreingestellte Gleitparameter
  name=Scheme,                         % Name in Überschriften
  listname={Verzeichnis der Schemata}, % Listenname
                                       % counterwithin=chapter
]{los}

\begin{document}

\begin{table}[H]
\vspace{-1.8 \baselineskip}
  \centering
  \setlength\belowcaptionskip{5 pt}
  \caption{\textbf{Reaction mixture used for the synthesis of cyclohexanone 4-nitrophenylhydrazone.}}
    \begin{tabular}{cccccc}  
     \cellcolor{hellgrau} chemicals   &\cellcolor{hellgrau} \textit{M} $^a$ (g mol$^{\text{-1}}$)  &\cellcolor{hellgrau} \textit{n} $^a$(mol 10$^{\text{-4}}$)  &\cellcolor{hellgrau} \textit{V} $^a$ (mL) &\cellcolor{hellgrau}  \textit{m} $^a$ (g) &\cellcolor{hellgrau} $\varrho$ $^a$ (g mL$^{\text{-1}}$)\\
    Cyclohexanone               & 98.14   &  \multicolumn{4}{c}{0.5 mL of the destillate}    \\
    4-Nitrophenyl hydrazine     & 153.14  &  26    &   -  & 0.4   & -     \\
    Sulfuric acid               & 98.08   &  372   &   2  & 3.68  & 1.84 \\
    Water                       & 18.02   & 1664   &   3  & 3     & 1.00 \\
    Ethanol                     & 47.07   & 1678   &   10 & 7.9   & 0.79    \\
    \end{tabular} 
    \vspace{0.6 \baselineskip} \\
    \raggedright
    $^a$Molecular mass \textit{M}, amount of substance \textit{n}, volume \textit{V}, mass \textit{m},  density $\varrho$.
  \label{tab:ReactionMixtureCyclohexanone4-nitrophenylhydrazone}%
  \vspace{-1.8 \baselineskip}
\end{table}

\end{document}
2
  • Welcome to TeX.SE! Please provide Minimal Working Example (MWE), which reproduce your problem. From your description is not clear, what you doing that you are not happy with the result.
    – Zarko
    Nov 25, 2020 at 15:09
  • Okay so here is my MWE. The problem with this is that is doesn't fit the text width and it has these thin vertical white lines between the cells in the light grey head row.
    – Benson_G
    Nov 25, 2020 at 16:17

2 Answers 2

0

Like this?

enter image description here

(red lines indicate page layout)

  • from your document example I try to make MWE (Minimal Working Example),therefore from your example are removed all packages and settings not related to your problem
  • to old version of packages or even deprecated are indicated and replaced with recent one
  • for table is used tabularx environment, its width is set to \textwidth, so using them is solved your problem that table width should be equal to \textwidth
  • for columns with numbers are used S columns, defined in siunitx package, which enable align numbers at their decimal points
  • since I suspect, that you in table headers indicate "footnotes", I suggest that for this is better use tnotes defined in the threeparttable and for descriptions of their meaning use table notes

MWE:

\documentclass[a4paper,10pt, 
               enabledeprecatedfontcommands]{scrartcl} %Art des Dokuments, Schriftgröße etc.
\usepackage[hmargin={3cm,2cm},vmargin=2cm,
            includehead]{geometry}%Maße für die wissenschaftliche Arbeit
%\usepackage[utf8]{inputenc}% now is default encoding 
\usepackage{lmodern}
\usepackage[T1]{fontenc} %Silbentrennung
\usepackage[english]{babel} %Sprachanpassungen
 
\usepackage[column=O]{cellspace}                % <-- new
\setlength\cellspacetoplimit{2pt}
\setlength\cellspacebottomlimit{2pt}
\addparagraphcolumntypes{X}
\usepackage{booktabs, tabularx, % Tabellen
            threeparttable}     % new 
\newcommand\mcx[1]{\multicolumn{1}{>{\centering\arraybackslash}OX}{#1}}
\renewcommand\TPTtagStyle{\bfseries}%for formating tags (in table, in items)

\usepackage[version=4]{mhchem}  % newest version
\usepackage{siunitx}% instead of units, which is deprecate
\usepackage[skip=1ex, format=plain, font=small, labelfont=bf]{caption}     % new
%\usepackage[labelformat=simple]{subcaption}
\usepackage[table]{xcolor}  % changed, instead of "color" and "colortbl"
\definecolor{hellgrau}{rgb}{0.90,0.90,0.90}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%


\begin{document}
    \begin{table}[ht]
    \centering
    \renewcommand\tabularxcolumn[1]{m{#1}}
\begin{threeparttable}
\caption{Reaction mixture used for the synthesis of cyclohexanone 4-nitrophenylhydrazone.}
\label{tab:ReactionMixtureCyclohexanone4-nitrophenylhydrazone}
    \begin{tabularx}{0.8\linewidth}{l 
                                    S[table-format=3.2]
                                    S[table-format=4.0]
                                    S[table-format=2.0]
                                    S[table-format=1.1]
                                    S[table-format=1.2]
                                    }
    \rowcolor{hellgrau} 
chemicals   & \mcx{$M$\tnote{a} (\si{\gram\per\mole})}  
                & \mcx{$n$\tnote{b} (\si{\gram\per\mole})}  
                    & \mcx{$V$\tnote{c}\par     (\si{\milli\liter})} 
                        & \mcx{$m$\tnote{d}\par     (\si{\gram})}
                            & \mcx{$\varrho$\tnote{e}\par (\si{\gram\per\mole})}          \\
% table body
Cyclohexanone           & 98.14   & \multicolumn{4}{c}{\SI{0.5}{\milli\liter} of the distillate} \\
4-Nitrophenyl hydrazine & 153.14  &  26    & {--} & 0.4   & {--}     \\
Sulfuric acid           & 98.08   &  372   &   2  & 3.68  & 1.84    \\
Water                   & 18.02   & 1664   &   3  & 3     & 1.00    \\
Ethanol                 & 47.07   & 1678   &   10 & 7.9   & 0.79    \\
    \bottomrule
    \end{tabularx}
    \begin{tablenotes}[flushleft, para]\footnotesize
\item[a]    $M$: Molecular mass
\item[b]    $n$: amount of substance , 
\item[c]    $V$: volume, 
\item[d]    $m$: mass,  
\item[e]    $\varrho$: density .
    \end{tablenotes}
\end{threeparttable}
    \end{table}
\end{document}

Edit:

  • Suggested table design has width of the \texwidth, however number of columns cannot be any, if you you like avoid overlaping of cells contents. With other words, it is limited by size column contents.

  • In the case that you not have numbers in table, than you can in above MWE use X column specifiers and omit use of \mcx for use in column headers (but this is not a case in your code example)

IN

18
  • That comes very close to what I want. Can you also explain to me what it is about this \mcx command and what you did with it in the preamble?
    – Benson_G
    Nov 25, 2020 at 18:36
  • But unfortunately my main concern is still not fixed. I can still see the small vertical lines between the light gray cells. Is it maybe possible to implement the overhang parameters in the \newcommand line for the \mcx command in the preamble? Bernard edited the overhang parameters in this thread tex.stackexchange.com/a/301036/229635
    – Benson_G
    Nov 25, 2020 at 18:39
  • @Benson_G, if you inspect MWE preamble carefully, you will find there deifinition for \mcx: it is defined by multicolumn{1}{...X{...}. Consequently it defined X type cells in columns with different column specification. By it is achieved that all columns with numbers have equal width and that column headers can have two lines.
    – Zarko
    Nov 25, 2020 at 18:40
  • @Benson_G, white vertical lines not exist and they are cannot be seen im my MWE. If you see, thanm, this are artifacts of your pdf viewer.
    – Zarko
    Nov 25, 2020 at 18:43
  • I am using the pdfLaTeX compiler implemented in overleaf.
    – Benson_G
    Nov 25, 2020 at 18:48
0

With {NiceTabular*} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{enumitem}

\begin{document}
\newcolumntype{-}{@{\extracolsep{\fill}}}

\begin{table}
\caption{Caption of my table\tabularnote{A tabular note.}}
\begin{NiceTabular*}{\textwidth}{c-c-c-c-c}
\CodeBefore
  \rowcolor{lightgray}{1}
\Body
  Col 1 & Col 2 & Col 3 & Col 4 & Col 5 \\
  text & text & text & text & text \\
  text & text & text & text & text \\
  text & text & text & text & text \\
  text & text & text & text & text \\
\end{NiceTabular*}
\end{table}

\end{document}

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

Output of the above code

9
  • Thanks a lot for your answer! This looks quite good. Can you maybe explain to which parameter I have to change when I want to add or delete columns or rows? Also is there a way to use own colors defined bei the colorist package? Best, Benson_G
    – Benson_G
    Nov 25, 2020 at 16:04
  • Is it also possible to add caption referring as a table to it?
    – Benson_G
    Nov 25, 2020 at 16:16
  • If you want to add a column, you add a -c in the format (that is to say the {c-c-c-c-c} in my example). Instead of the color lightgray, you can use whatever color defined by the usual tools (for example \newrgbcolor). You can insert a {NiceTabular}* in an environment {table} and use \caption exactly as you would do with {tabular*}. Nov 25, 2020 at 16:27
  • Can you show the syntax to me? I don't know how to add a comment with a MWE.
    – Benson_G
    Nov 25, 2020 at 16:38
  • I have modified my answer. Nov 25, 2020 at 17:28

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.