1

Here is the table in question:

Picture of table

As we see, the colour overflows the table side margins.

  1. What is causing this?
  2. How to fix it? Minimally invasive procedure is preferred.

I have currently not reduced the MWE further to increase possibility that the solution works for the case at hand (lipsum part is removed).

\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf, singlelinecheck=false]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)
\usepackage{threeparttable}

\makeatletter\usepackage{microtype}\g@addto@macro@verbatim{\microtypesetup{activate=false}}\makeatother%

\renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}} \newcolumntype{O}{>{\currentrowstyle}} \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}% #1\ignorespaces } %to make entire row boldface/color with one command instead of one at a time

\begin{document}

\begin{table}[!htbp] \setlength{\extrarowheight}{1.5ex} \centering \begin{threeparttable} \caption{Title for your table goes here \vspace{-0.7em}} \begin{tabular}{@{} Nc|Oc|Oc|Oc|Oc @{}} \Xhline{0.1em} \rowstyle{\bfseries\cellcolor{cyan!70}} %here is where the colouring takes place {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\[1ex] \Xhline{0.07em} $x^2 = 4py$ & \makecell{up if $p > 0$ \ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\ \Xcline{1-5}{0.03em}%horisontal line $y^2 = 4px$ & \makecell{right if $p > 0$\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\ \Xhline{0.08em} \end{tabular} \label{table: label for table} \end{threeparttable} \end{table}

\end{document}

2 Answers2

2

With {NiceTabular} of nicematrix and its built-in tools to color rows, cells and columns, you have directly the expected result.

However, in order to be able to use {NiceTabular} within {threeparttable}, one has to use first the dedicated hook of threeparttable:

\makeatletter
\AddToHook{env/threeparttable/begin}
{\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother

In versions of LaTeX previous to 2020/10/01, the system of hooks was not available and you had to do that job with etoolbox instead:

\usepackage{etoolbox}
\makeatletter
\AtBeginEnvironment{threeparttable}{\TPT@hookin{NiceTabular}}
\makeatother

For the full MWE:

\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf, singlelinecheck=false]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)
\usepackage{threeparttable}
\usepackage{nicematrix}

\makeatletter\usepackage{microtype}\g@addto@macro@verbatim{\microtypesetup{activate=false}}\makeatother%

\renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}} \newcolumntype{O}{>{\currentrowstyle}} \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}% #1\ignorespaces } %to make entire row boldface/color with one command instead of one at a time

\makeatletter \AddToHook{env/threeparttable/begin} {\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}} \makeatother

\begin{document}

\begin{table}[!htbp] \setlength{\extrarowheight}{1.5ex} \centering \begin{threeparttable} \caption{Title for your table goes here \vspace{-0.7em}} \begin{NiceTabular}{@{} Nc|Oc|Oc|Oc|Oc @{}} \CodeBefore \rowcolor{cyan!70}{1} \Body \Xhline{0.1em} \rowstyle{\bfseries} %here is where the colouring takes place {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\[1ex] \Xhline{0.07em} $x^2 = 4py$ & \makecell{up if $p > 0$ \ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\ \Xcline{1-5}{0.03em}%horisontal line $y^2 = 4px$ & \makecell{right if $p > 0$\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\ \Xhline{0.08em} \end{NiceTabular} \label{table: label for table} \end{threeparttable} \end{table}

\end{document}

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

Output of the above code

F. Pantigny
  • 40,250
  • Thank you for the answer! Indeed, three compilations did the trick here. It is interesting to note that I could not compile the code on Overleaf, it shows multiple errors there. Worked great with my local distribution though. Great to learn an extra syntax option for tables. – Linear Christmas Apr 27 '21 at 17:51
  • 1
    Overleaf has not the latest version of nicematrix. However, it's possible to upload in the repertory of your Overleaf project the latest version of the file nicematrix.sty. That file is certainly in your computer (since you have been able to compile) but it's also possible to find it on the SVN server of TeXLive:www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/nicematrix/nicematrix.sty – F. Pantigny Apr 27 '21 at 18:00
  • 1
    Thank you for the extra information. I suppose that one has to add some additional code as well? I have tried to simply add the nicematrix.sty file but this seems to have no effect on its own. – Linear Christmas Apr 27 '21 at 18:24
  • 1
    You are right. Overleaf has not the latest version of LaTeX and, in particular, not the new system of hooks. I'm modified my answer to explain what to type in that case. I have just tested on Overleaf and, now, it works :-) – F. Pantigny Apr 27 '21 at 18:55
  • Once again, thank you for the update. For some reason, this still does not work for me with Overleaf: see this picture. But the situation is also different from before: now no error message is given... Says simply No errors, good job!. I also tried to refresh the page to no avail (sometimes this fixes things). Your updated solution again works nicely on my local distribution, just like the previous solution. EDIT: Seems to be a problem with Overleaf's PDF viewer/renderer. I can download the file nicely :). – Linear Christmas Apr 27 '21 at 19:49
  • There is a problem with the PDF renderer of Overleaf... You can try to change the renderer (in the general options (under "Menu"), at the end, it's possible to switch to another renderer). However, for me, both renderers display correctly the PDF... – F. Pantigny Apr 27 '21 at 19:57
  • You are exactly correct! Very helpful suggestion, thank you. To confirm: the "built-in" PDF viewer does not work for me with your solution whereas the "native" PDF does nicely display the table. Thank you again, I found this discussion very useful. – Linear Christmas Apr 27 '21 at 20:03
1

I suggest using \columncolor because you have optional arguments for the colour overhangs on each side of the cells, combined with a \rowcolors{2}{white}{white} to neutralise colouring after the column heads:

\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{tabu} %for \cellcolor to work inside \rowstyle
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)

\makeatletter\usepackage{microtype}\g@addto@macro@verbatim{\microtypesetup{activate=false}}\makeatother% \usepackage{floatrow, caption} \renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}} \newcolumntype{O}{>{\currentrowstyle}} \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}% #1\ignorespaces } %to make entire row boldface/color with one command instead of one at a time \usepackage{lipsum}

\begin{document}

\lipsum[11]

\begin{table}[!htbp] \captionsetup{singlelinecheck=false}\setlength{\extrarowheight}{2ex} \centering \ttabbox{\rowcolors{2}{white}{white}\begin{tabular}{@{}>{\columncolor{cyan!70}[0pt][\tabcolsep]}Nc|*{3}{>{\columncolor{cyan!70}}Oc|}>{\columncolor{cyan!70}[\tabcolsep][0pt]}Oc @{}} \Xhline{0.08em} %\rowstyle{\bfseries\cellcolor{cyan!70}} {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\[1.5ex] \Xhline{0.05em} $x^2 = 4py$ & \makecell{up if $p > 0$ \ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\[1.5ex] \Xhline{0.03em}%horizontal line $y^2 = 4px$ & \makecell{right if $p > 0$\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\[1.5ex] \Xhline{0.08em} \end{tabular}}{% \caption{A very very long title for your table goes here and nowhere else. } \label{table: label for table}} \end{table}

\end{document}

enter image description here

Bernard
  • 271,350
  • Apologies for the delay in my response. Is there a particular reason why you replaced ^2 with \texttwosuperior type exponent? Or is it just a sideproduct of how you copied the code somehow? To be very nitpicky, you could also uncomment the line with %\rowstyle and just remove the ``\cellcolor{cyan!70}´´ to retain boldface for first row of table. Thank you for the answer and your comment on the question itself! – Linear Christmas Apr 27 '21 at 17:32
  • 1
    Oops! Sorry, it is just my editor which is configured to display exponents ^2 or ^3 as ² or ´³, ar \ alpha, … – Bernard Apr 27 '21 at 17:38