2

I have a longtable with alternating rowcolor and two math columns. The rowcolor works as intended when the content of the row is in a single line, with padding both above and below the row content. Some entries are valid for both columns and hence are in the middle. But overall the table is looking rather ugly :(

enter image description here

But if the content spans more than one line, or if there is an integral sign, the rowcolor touches the content and there is no padding. I have tried increasing arraystretch (its already 2 and does not make any difference) but it does not help. I have also tried makecell package as suggested in some answers, but that did not work as well, also did try extrarowheight. Another thing I tried is to manually add spaces after newline in each row \\[5pt], but that only solves the problem for the bottom parts touching the next coloured row and its variable. In some cases I had to add 8pt or 10pt for a better look. The top part of the content is understandably not affected by adding space after the row.

Please suggest what can be done. Any other packages instead of longtable and any other tips in general to make the table better will be appreciated. Bonus if all entries in a given column could be aligned.

MWE

\documentclass[twoside]{book}
\usepackage{geometry}               
\geometry{paperwidth=205mm, paperheight=236mm, top=15mm,bottom=20mm, textheight=196mm, headsep=5mm, left=15mm, right=85mm, textwidth=95mm, marginpar=70mm,marginparsep=5mm}                   

\usepackage{amsmath}

\usepackage{fouriernc}
\usepackage{physics}

\usepackage{longtable}
\usepackage{booktabs}

\usepackage[table,svgnames]{xcolor}

\usepackage{multicol}
\newcommand{\tbb}[1]{\textcolor{DarkSlateBlue}{{\textbf{#1}}}}

\newcolumntype{M}{>{$}p{4cm}<{$}}  %  math mode column

\begin{document}



\newgeometry{margin=15mm,inner=15mm,outer=15mm,marginparwidth=0cm,marginparsep=0mm}
%invoked newgeometry to fit the table in the given space, otherwise in the twoside mode the table runs into margin for even pages

{\everymath{\displaystyle}
\setlength{\tabcolsep}{8pt}
\renewcommand{\arraystretch}{2}

\rowcolors{3}{white}{DarkSlateBlue!30}
\begin{longtable}{p{5cm}MM}
\toprule
\tbb{Relation} & \tbb{\text{SI}} & \tbb{\text{Gaussian System}} \\

\midrule
Relation between $\vb{D}$ and $\vb{E}$ & \vb{D} = \varepsilon \varepsilon_{0} \vb{E} & \vb{D} = \varepsilon  \vb{E} \\
Gauss theorem for vector $\vb{D}$  & \oint \vb{D} \, \dd \vb{S} = q & \oint \vb{D} \, \dd \vb{S} = 4 \pi q\\
Capacitance of a capacitor  & \multicolumn{2}{c}{ $C = \frac{q}{U}$}  \\
Capacitance of a parallel-plate capacitor  & C = \frac{\varepsilon \varepsilon_{0}S}{h}& C = \frac{\varepsilon S}{4 \pi h} \\
Energy of system of charges  & \multicolumn{2}{c}{ $W = \frac{1}{2} \sum q_{i} \varphi_{i} $} \\
Total energy of interaction  & \multicolumn{2}{c}{ $W = \frac{1}{2} \int \rho \, \varphi \, \dd V $} \\
Energy of capacitor   & \multicolumn{2}{c}{ $W = \frac{qU}{2} = \frac{CU^{2}}{2} = \frac{q^{2}}{2C} $} \\
Electric field energy density & w = \frac{\vb{E}\vdot \vb{D}}{2} & w = \frac{\vb{E}\vdot \vb{D}}{8 \pi}\\
\bottomrule
\end{longtable}
}

\clearpage
\restoregeometry


\end{document}
1
  • 1
    You can use longtblr in tabularray package.
    – Clara
    Commented Mar 13, 2023 at 15:02

4 Answers 4

4
\documentclass{book}
\usepackage[margin=15mm]{geometry}
\usepackage{physics}
\usepackage[svgnames]{xcolor}
\usepackage{tabularray}
\begin{document}
\begin{longtblr}
[
caption          = {title},
label            = {key},
]
{
colspec          = {X[1.5,l,m]X[l,m]X[l,m]},
hline{1,Z}       = {wd=.08em},
hline{2}         = {wd=.05em},
row{1}           = {font=\bfseries,fg=DarkSlateBlue},
cell{4,6,7,8}{2} = {c=2}{halign=c},
row{even[2-Z]}   = {bg=DarkSlateBlue!30},
cell{2-Z}{2-Z}   = {mode=dmath},
}
Relation                                  & SI                                               & Gaussian System                  \\
Relation between $\vb{D}$ and $\vb{E}$    & \vb{D}=\varepsilon\varepsilon_{0}\vb{E}          & \vb{D}=\varepsilon\vb{E}         \\
Gauss theorem for vector $\vb{D}$         & \oint\vb{D}\,\dd\vb{S}=q                         & \oint \vb{D}\,\dd\vb{S}=4\pi q   \\
Capacitance of a capacitor                & C=\frac{q}{U}                                    &                                  \\
Capacitance of a parallel-plate capacitor & C=\frac{\varepsilon \varepsilon_{0}S}{h}         & C=\frac{\varepsilon S}{4\pi h}   \\
Energy of system of charges               & W=\frac{1}{2}\sum q_{i}\varphi_{i}               &                                  \\
Total energy of interaction               & W=\frac{1}{2}\int\rho\,\varphi\,\dd V            &                                  \\
Energy of capacitor                       & W=\frac{qU}{2}=\frac{CU^{2}}{2}=\frac{q^{2}}{2C} &                                  \\
Electric field energy density             & w=\frac{\vb{E}\vdot\vb{D}}{2}                    & w=\frac{\vb{E}\vdot\vb{D}}{8\pi} \\
\end{longtblr}
\clearpage
\restoregeometry
\end{document}

enter image description here

3

As supplement to nice Clara answer with a little bit changed table layout:

  • no newgeometry for table, table can nice fit in existed table layout
  • bigger rowsep
  • different way to "centering" of twocolumn cells
\documentclass[twoside]{book}
\usepackage{geometry}   % set desired page layout
\usepackage{physics}
\usepackage[svgnames]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\usepackage{lipsum}

\begin{document}
\lipsum[1]
\clearpage

\begin{longtblr}[
caption = {title},
  label = {key}]{colspec = {Q[l] X[l,mode=dmath] X[l,mode=dmath]},
                column{2} = {colsep = 2em}, % <---
                 rowsep  = 5pt,              % <---
                 row{1}  = {font=\bfseries, mode=text},
                 cell{4,6,7,8}{2} = {c=2}{preto=\hspace{8em},l}, % <---
                    row{even[2]}  = {bg=DarkSlateBlue!30},
                 }
    \toprule
Relation                                  
    & SI                                               
        & Gaussian System                       \\
    \midrule
Relation between $\vb{D}$ and $\vb{E}$    
    & \vb{D}=\varepsilon\varepsilon_{0}\vb{E}          
        & \vb{D}=\varepsilon\vb{E}              \\
Gauss theorem for vector $\vb{D}$         
    & \oint\vb{D}\,\dd\vb{S}=q                         
        & \oint \vb{D}\,\dd\vb{S}=4\pi q        \\
Capacitance of a capacitor                
    & C=\frac{q}{U}                                    
        &                                       \\
Capacitance of a parallel-plate capacitor 
    & C=\frac{\varepsilon_{r} \varepsilon_{0}S}{h}         
        & C=\frac{\varepsilon S}{4\pi h}        \\
Energy of system of charges               
    & W=\frac{1}{2}\sum q_{i}\varphi_{i}               
        &                                       \\
Total energy of interaction               
    & W=\frac{1}{2}\int\rho\,\varphi\,\dd V            
        &                                       \\
Energy of capacitor                       
    & W=\frac{qU}{2}=\frac{CU^{2}}{2}=\frac{q^{2}}{2C} 
        &                                       \\
Electric field energy density             
    & w=\frac{\vb{E}\vdot\vb{D}}{2}                    
        & w=\frac{\vb{E}\vdot\vb{D}}{8\pi}      \\
    \bottomrule
\end{longtblr}
\end{document}

enter image description here

7
  • This doesn't work with twoside option of the book class, on the even pages the table runs into right side margins. imgur.com/OUGpS9Q I have almost one-third of textwidth as margin, hence using the newgeometry option to fit the table into page.
    – Damitr
    Commented Mar 19, 2023 at 3:52
  • @Damitr, you are not right. It works. The same as accepted answer. BTW, in question you not provide \documentclass options nor page layout which you use in your document. There is no way to confirm your claims.
    – Zarko
    Commented Mar 19, 2023 at 4:51
  • I have twoside option mentioned in the MWE with the geometry specified please take a look at MWE in orignal question. You perhaps copied the code from the accepted answer, which is not the same as code in MWE that I have provided.
    – Damitr
    Commented Mar 20, 2023 at 3:36
  • @Damitr, I dont undestood what is your problem.
    – Zarko
    Commented Mar 20, 2023 at 6:09
  • @Damitr, I don't understand what is your problem. You can add twoside option` o my MWE (now I do this for you and move table to even page, that you can see that nothing is change in table) . However, if your table is longer (has more rows with multicolumn cells, than you should this noted also in code row marked by <--- )added number of rows where they are) . Anyway, you accept another answer which also use this code line and you not complain about this, so I wonder, why I deserve attention with yours comment (and accepted answer not) ?
    – Zarko
    Commented Mar 20, 2023 at 6:34
2

A solution based on longtable and cellspace for additional spacing.

There are a couple of corrections to what LaTeX does by default. Firs of all, I stretched the first row by adding an invisible rule. Second of all, in a row before the last, I reduced space, that would be taken by superscripts in fractions, by enclosing expressions in \cramped and \smash.

\documentclass[twoside]{book}
\usepackage{geometry}               
\usepackage{mathtools}   % Required by \cramped{}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[column=E]{cellspace}
\usepackage[table,svgnames]{xcolor}
\usepackage{fouriernc}
\usepackage{physics}

\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}

\newcommand{\tbb}[1]{\textcolor{DarkSlateBlue}{{\textbf{#1}}}}
\newcolumntype{M}{>{$}E{l}<{$}}  %  math mode column
\newcolumntype{L}{E{l}}
\newcommand\dstrut[1][1]{%
  \rule[-0.5\dimexpr#1\normalbaselineskip-1ex]{0pt}{#1\dimexpr\normalbaselineskip}}


\begin{document}
\begingroup
\centering
\everymath{\displaystyle}
\setlength{\tabcolsep}{8pt}
\rowcolors{3}{white}{DarkSlateBlue!30}
\begin{longtable}{LMM}
  \toprule
  \multicolumn{1}{l}{\tbb{Relation}}        & \multicolumn{1}{l}{\tbb{\text{SI}}}         & \multicolumn{1}{l}{\tbb{\text{Gaussian System}}} \\
  \specialrule{\lightrulewidth}{3pt}{0pt}
  \dstrut[1.5]%
  Relation between $\vb{D}$ and $\vb{E}$    & \vb{D} = \varepsilon \varepsilon_{0} \vb{E} & \vb{D} = \varepsilon  \vb{E} \\
  Gauss theorem for vector $\vb{D}$         & \oint \vb{D} \, \dd \vb{S} = q              & \oint \vb{D} \, \dd \vb{S} = 4 \pi q \\
  Capacitance of a capacitor                & \multicolumn{2}{E{c}}{$C = \frac{q}{U}$} \\
  Capacitance of a parallel-plate capacitor & C = \frac{\varepsilon \varepsilon_{0}S}{h}  & C = \frac{\varepsilon S}{4 \pi h} \\
  Energy of system of charges               & \multicolumn{2}{E{c}}{$W = \frac{1}{2} \sum q_{i} \varphi_{i} $} \\
  Total energy of interaction               & \multicolumn{2}{E{c}}{$W = \frac{1}{2} \int \rho \, \varphi \, \dd V $} \\
  Energy of capacitor                       & \multicolumn{2}{E{c}}{$W = \frac{qU}{2} = \smash{\cramped{\frac{CU^{2}}{2} = \frac{q^{2}}{2C}}}$} \\
  Electric field energy density             & w = \frac{\vb{E}\vdot \vb{D}}{2}            & w = \frac{\vb{E}\vdot \vb{D}}{8 \pi}\\
\specialrule{\heavyrulewidth}{0pt}{0pt}
\end{longtable}\par
\endgroup
\end{document}

enter image description here

1
  • cellspace looks interesting, I wil look into it
    – Damitr
    Commented Mar 19, 2023 at 4:12
2

With {NiceTabular} of nicematrix.

\documentclass{book}
\usepackage[margin=15mm]{geometry}
\usepackage{physics}
\usepackage[svgnames]{xcolor}

\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}


\everymath{\displaystyle}
\begin{NiceTabular}{X[3,l]X[2,l]X[2,l]}[cell-space-limits=3pt]
\CodeBefore
 \rowcolors{2}{}{DarkSlateBlue!30}
\Body
   \toprule
   \RowStyle[bold,color=DarkSlateBlue]{}
   Relation                                  & SI                                               & Gaussian System
   \\
   \midrule
   Relation between $\vb{D}$ and $\vb{E}$    & $\vb{D}=\varepsilon\varepsilon_{0}\vb{E}$          & $\vb{D}=\varepsilon\vb{E}$         \\
   Gauss theorem for vector $\vb{D}$         & $\oint\vb{D}\,\dd\vb{S}=q$                         & $\oint \vb{D}\,\dd\vb{S}=4\pi q$   \\
   Capacitance of a capacitor                & \Block{1-2}{$C=\frac{q}{U}$} \\
   Capacitance of a parallel-plate capacitor & $C=\frac{\varepsilon \varepsilon_{0}S}{h}$         & $C=\frac{\varepsilon S}{4\pi h}$   \\
   Energy of system of charges               & \Block{1-2}{$W=\frac{1}{2}\sum q_{i}\varphi_{i}$} \\
   Total energy of interaction               & \Block{1-2}{$W=\frac{1}{2}\int\rho\,\varphi\,\dd V$} \\         
   Energy of capacitor                       & \Block{1-2}{$W=\frac{qU}{2}=\frac{CU^{2}}{2}=\frac{q^{2}}{2C}$} \\
   Electric field energy density             & $w=\frac{\vb{E}\vdot\vb{D}}{2}$                    &
   $w=\frac{\vb{E}\vdot\vb{D}}{8\pi}$ \\
   \bottomrule
\end{NiceTabular}

\end{document}

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

Output of the above code

3
  • 1
    With nicematrix, you won't see thin white lines in the colored panels, whatever PDF viewer you use (even if you use FireFox or DropBox...) Commented Mar 13, 2023 at 19:38
  • Unfortunately nicematrix as used in the response above does not support a long table spanning multiple pages. If there a way to make it work across pages please let me know.
    – Damitr
    Commented Mar 19, 2023 at 4:15
  • You are right: there is no way to allow the tabulars of nicematrix to be broken... Commented Mar 19, 2023 at 8:24

You must log in to answer this question.

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