关于 longtable、math coloumn、rowcolor 和 arraystretch 的一些问题

关于 longtable、math coloumn、rowcolor 和 arraystretch 的一些问题

我有一个longtable带有交替rowcolor和两个数学列的表格。当行的内容在一行中时,行颜色会按预期工作,行内容的上方和下方都有填充。有些条目对两列都有效,因此位于中间。但总的来说,表格看起来相当丑陋 :(

在此处输入图片描述

但是如果内容跨越多行,或者有积分符号,则行颜色会接触内容且没有填充。我尝试过增加arraystretch(已经是 2 并且没有任何区别)但没有帮助。我也尝试过makecell按照一些答案中的建议使用软件包,但效果不太好,也尝试过extrarowheight。我尝试的另一件事是在每行的换行符后手动添加空格\\[5pt],但这只能解决底部接触下一个彩色行及其变量的问题。在某些情况下,我不得不添加 8pt 或 10pt 以获得更好的外观。可以理解,在行后添加空格不会影响内容的顶部。

请提出建议。如果有任何其他替代软件包longtable或任何其他使表格更好的技巧,我们将不胜感激。如果给定列中的所有条目都可以对齐,那就更好了。

平均能量损失

\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

\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}

在此处输入图片描述

答案2

作为对克拉拉 (Clara) 优秀回答的补充,表格布局略有改变:

  • 无需为表格添加新几何图形,表格可以很好地适应现有的表格布局
  • rowsep
  • 两列单元格“居中”的不同方法
\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}

在此处输入图片描述

答案3

基于longtablecellspace用于额外间距的解决方案。

我对 LaTeX 的默认操作做了一些更正。首先,我通过添加一条不可见的规则拉伸了第一行。其次,在倒数第二行中,我通过将表达式括在\cramped和中来减少分数上标占用的空间\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}

在此处输入图片描述

答案4

与。{NiceTabular}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}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容