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}

Zarko
  • 296,517
  • 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 '20 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 '20 at 16:17

2 Answers2

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

F. Pantigny
  • 40,250
  • 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 '20 at 16:04
  • Is it also possible to add caption referring as a table to it? – Benson_G Nov 25 '20 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*}. – F. Pantigny Nov 25 '20 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 '20 at 16:38
  • I have modified my answer. – F. Pantigny Nov 25 '20 at 17:28
  • Your modified code doesn't work me. Does it maybe have anything to do with what you said before: I several compilations? What does that mean? – Benson_G Nov 25 '20 at 18:21
  • Maybe your TeX installation is not up-to-date. Maybe you have not compiled several times. That means: you execute pdflatex doc.tex. The PDF is generated but informations are also written on the doc.aux. When running pdflatex doc.tex once ago, pdflatex uses doc.aux file and so the (new) PDF is not the same as the first one. If you want, send me a an email: fpantigny@wanadoo.fr. – F. Pantigny Nov 25 '20 at 18:45
  • This sounds quite complicated. I'm using overleaf which I expect to be pretty much up to date. – Benson_G Nov 25 '20 at 18:52
  • Overleaf always does the successive compilations but the version of nicematrix available in Overleaf is not the latest... If you want, I can send you the latest. – F. Pantigny Nov 25 '20 at 19:00
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

Zarko
  • 296,517
  • 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 '20 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 https://tex.stackexchange.com/a/301036/229635 – Benson_G Nov 25 '20 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 '20 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 '20 at 18:43
  • I am using the pdfLaTeX compiler implemented in overleaf. – Benson_G Nov 25 '20 at 18:48
  • @Benson_G, sorry, Im focused only to this question. I add some explanation to answer. Hopefully it will clarify answer. – Zarko Nov 25 '20 at 18:54
  • @Zarko: When you use standard colortbl, the resulting PDF has (your are right) no mathematical thin white lines beetween the colored panels... but some PDF engine (and even modern PDF engines) show some thin white lines at some levels of zoom. It's the case of muPDF used in SumatraPDF but probably also in the PDF viewer of Overleaf (but maybe it's the PDF viewer of the browser). That's why I have written tools in the package nicematrix to solve that problem...:-) – F. Pantigny Nov 25 '20 at 19:12
  • But nicematrix is more complicated to compile according to what you said. This kills my work flow. – Benson_G Nov 25 '20 at 19:14
  • @Zarko which part of your code makes the table fitting the text width? – Benson_G Nov 25 '20 at 19:19
  • @Benson_G: Actually, It's not more complicated for the final user... but Overleaf has not the recent versions of the packages... – F. Pantigny Nov 25 '20 at 19:25
  • @Benson_G, this tiny lines are artifact of pdf viewers. For example, in my Acrobat Reader DC (recent version) they are not visible, in sumatra they are barely visible (all on FHD screens). They are not visible on paper (temporary I can't confirm this by test) . So, the simplest solution is: don't be so picky ... ;-) – Zarko Nov 25 '20 at 19:34
  • I see. I think I will just give up on it and ignore the white lines. – Benson_G Nov 25 '20 at 21:17
  • @Zarko unfortunately my code does not compile or returns a time out while compilation after I've implemented your suggested code for the table. Can you maybe help me with this? – Benson_G Nov 25 '20 at 21:19
  • @Benson_G, hm, I do not know what is going on at your compilation. I test my MWE on my PC windows machine and installed recent MikTeX. Compilation is fast (~5 seconds), with no errors, no warnings. I suggest you to ask new, follow up question (with link to this), where you provide MWE, which reproduce your problem. DIfferences between document and my MWE can be in used packages, your new commands etc. – Zarko Nov 25 '20 at 21:29
  • I solved my problem. – Benson_G Nov 25 '20 at 23:42
  • @Zarko What does the [1] and the {#1} as well as in the \newcommand line for \mcx and in the \renewcommand line before the start of the tabularxenvironment do? – Benson_G Nov 26 '20 at 07:20
  • @Benson_G, you mean \renewcommand\tabularxcolumn[1]{m{#1}}? This command change position of cells content baseline from top to its vertical center. First 1 means, that command has one option, the #1 indicate, where this option is used. Never ever change these settings! – Zarko Nov 26 '20 at 13:01