\rowcolors 的问题

\rowcolors 的问题

我的 latex 代码出了问题。我想交替改变行的颜色,但每次编译时都会出现奇怪的效果。这是我的代码:

    \begin{center}
    \rowcolors{1}{black!10}{white}
    \begin{tabular}{s{3cm}*{2}{|s{2cm}}|s{3cm}|s{2cm}}
      \firsthline
      \multicolumn{1}{s{3cm}}{Misure di $l_m$\\($cm$)} & \multicolumn{1}{s{2cm}}{$l_m$\\($\pm 0.1\:cm$)} & \multicolumn{1}{s{2cm}}{$l_f$\\($\pm 0.1\:cm$)} & \multicolumn{1}{s{3cm}}{Misure di $T$\\($s$)} & \multicolumn{1}{s{2cm}}{$T$\\($\pm 0.01\:s$)} \tabularnewline \hline
      130.9 / 130.9  131.1 / 131.0  131.1 & 131.0 & 133.6 & 2.324 / 2.320  2.317 / 2.324  2.320 & 2.32 \tabularnewline \hline
      110.9 / 111.0  110.8 / 110.9  110.9 & 110.9 & 113.5 & 2.123 / 2.121  2.134 / 2.137  2.131 & 2.13 \tabularnewline \hline
      91.4 / 91.6  91.5 / 91.6  91.5 & 91.5 & 94.1 & 1.948 / 1.930  1.936 / 1.931  1.949 & 1.94 \tabularnewline \hline
      73.2 / 73.2  73.2 / 73.1  73.2 & 73.2 & 75.8 & 1.720 / 1.742  1.725 / 1.735  1.735 & 1.73 \tabularnewline \hline
      47.4 / 47.4  47.4 / 47.3  47.3 & 47.4 & 50.1 & 1.406 / 1.407  1.413 / 1.404  1.411 & 1.41 \tabularnewline \hline
      25.5 / 25.4  25.5 / 25.5  25.4 & 25.5 & 28.0 & 1.047 / 1.035  1.053 / 1.060  1.035 & 1.05 \tabularnewline
      \lasthline
    \end{tabular}
    \end{center}

编译代码

这是一个简短的可编译测试:

\documentclass[a4paper, 12pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}

\usepackage[left=25mm, right=25mm, headheight=32pt, bottom=3cm]{geometry}

\usepackage{amsmath,amssymb}

\usepackage{array}
\usepackage{multirow}
\usepackage[table]{xcolor}

\newcolumntype{s}[1]{>{\centering}m{#1}}

\begin{document}

\begin{center}
\rowcolors{1}{black!10}{white}
\begin{tabular}{s{3cm}*{2}{|s{2cm}}|s{3cm}|s{2cm}}
  \firsthline
  \multicolumn{1}{s{3cm}}{Misure di $l_m$\\($cm$)} & \multicolumn{1}{s{2cm}}{$l_m$\\($\pm 0.1\:cm$)} & \multicolumn{1}{s{2cm}}{$l_f$\\($\pm 0.1\:cm$)} & \multicolumn{1}{s{3cm}}{Misure di $T$\\($s$)} & \multicolumn{1}{s{2cm}}{$T$\\($\pm 0.01\:s$)} \tabularnewline \hline
  130.9 / 130.9  131.1 / 131.0  131.1 & 131.0 & 133.6 & 2.324 / 2.320  2.317 / 2.324  2.320 & 2.32 \tabularnewline \hline
  110.9 / 111.0  110.8 / 110.9  110.9 & 110.9 & 113.5 & 2.123 / 2.121  2.134 / 2.137  2.131 & 2.13 \tabularnewline \hline
  91.4 / 91.6  91.5 / 91.6  91.5 & 91.5 & 94.1 & 1.948 / 1.930  1.936 / 1.931  1.949 & 1.94 \tabularnewline \hline
  73.2 / 73.2  73.2 / 73.1  73.2 & 73.2 & 75.8 & 1.720 / 1.742  1.725 / 1.735  1.735 & 1.73 \tabularnewline \hline
  47.4 / 47.4  47.4 / 47.3  47.3 & 47.4 & 50.1 & 1.406 / 1.407  1.413 / 1.404  1.411 & 1.41 \tabularnewline \hline
  25.5 / 25.4  25.5 / 25.5  25.4 & 25.5 & 28.0 & 1.047 / 1.035  1.053 / 1.060  1.035 & 1.05 \tabularnewline
  \lasthline
\end{tabular}
\end{center}

\end{document}

答案1

只是不要使用\firsthline\lasthline,而是\hline。并反转颜色。

不管怎么说,排版实在太差了……

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{siunitx}
\usepackage{array}

\newcolumntype{s}[1]{>{\centering}m{#1}}

\begin{document}

\begin{center}

\rowcolors{1}{white}{black!10}

\begin{tabular}{
  s{2cm}|
  s{2cm}|
  s{2cm}|
  s{2cm}|
  s{2cm}
}
\hline
\multicolumn{1}{s{2cm}}{Misure di $l_m$\\(\unit{cm})} &
\multicolumn{1}{s{2cm}}{$l_m$\\(\qty{\pm 0.1}{cm})} &
\multicolumn{1}{s{2cm}}{$l_f$\\(\qty{\pm 0.1}{cm})} &
\multicolumn{1}{s{2cm}}{Misure di $T$\\(\unit{s})} &
\multicolumn{1}{s{2cm}}{$T$\\(\qty{\pm 0.01}{s})} \tabularnewline
\hline
130.9 / 130.9  131.1 / 131.0  131.1 & 131.0 & 133.6 & 2.324 / 2.320  2.317 / 2.324  2.320 & 2.32 \tabularnewline \hline
110.9 / 111.0  110.8 / 110.9  110.9 & 110.9 & 113.5 & 2.123 / 2.121  2.134 / 2.137  2.131 & 2.13 \tabularnewline \hline
91.4 / 91.6  91.5 / 91.6  91.5 & 91.5 & 94.1 & 1.948 / 1.930  1.936 / 1.931  1.949 & 1.94 \tabularnewline \hline
73.2 / 73.2  73.2 / 73.1  73.2 & 73.2 & 75.8 & 1.720 / 1.742  1.725 / 1.735  1.735 & 1.73 \tabularnewline \hline
47.4 / 47.4  47.4 / 47.3  47.3 & 47.4 & 50.1 & 1.406 / 1.407  1.413 / 1.404  1.411 & 1.41 \tabularnewline \hline
25.5 / 25.4  25.5 / 25.5  25.4 & 25.5 & 28.0 & 1.047 / 1.035  1.053 / 1.060  1.035 & 1.05 \tabularnewline
\hline
\end{tabular}

\end{center}

\end{document}

我把宽度全部固定为 2cm,以便重现您的图片。还请注意\unit\qtyfrom siunitx: 单位的用法应该总是是直立类型,而且$cm$无论如何都是错误的。

在此处输入图片描述

或者,不使用垂直线:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{siunitx}
\usepackage{array}
\usepackage{booktabs}

\newcolumntype{s}[1]{>{\centering}m{#1}}

\begin{document}

\begin{center}

\rowcolors{1}{white}{black!10}

\begin{tabular}{
  s{2cm}
  s{2cm}
  s{2cm}
  s{2cm}
  s{2cm}
}
\toprule
Misure di $l_m$\\(\unit{cm}) &
$l_m$\\(\qty{\pm 0.1}{cm}) &
$l_f$\\(\qty{\pm 0.1}{cm}) &
Misure di $T$\\(\unit{s}) &
$T$\\(\qty{\pm 0.01}{s}) \tabularnewline
\midrule
\addlinespace[-0.05\belowrulesep]
130.9 / 130.9  131.1 / 131.0  131.1 & 131.0 & 133.6 & 2.324 / 2.320  2.317 / 2.324  2.320 & 2.32 \tabularnewline
\addlinespace
110.9 / 111.0  110.8 / 110.9  110.9 & 110.9 & 113.5 & 2.123 / 2.121  2.134 / 2.137  2.131 & 2.13 \tabularnewline
\addlinespace
91.4 / 91.6  91.5 / 91.6  91.5 & 91.5 & 94.1 & 1.948 / 1.930  1.936 / 1.931  1.949 & 1.94 \tabularnewline
\addlinespace
73.2 / 73.2  73.2 / 73.1  73.2 & 73.2 & 75.8 & 1.720 / 1.742  1.725 / 1.735  1.735 & 1.73 \tabularnewline
\addlinespace
47.4 / 47.4  47.4 / 47.3  47.3 & 47.4 & 50.1 & 1.406 / 1.407  1.413 / 1.404  1.411 & 1.41 \tabularnewline
\addlinespace
25.5 / 25.4  25.5 / 25.5  25.4 & 25.5 & 28.0 & 1.047 / 1.035  1.053 / 1.060  1.035 & 1.05 \tabularnewline
\bottomrule
\end{tabular}

\end{center}

\end{document}

在此处输入图片描述

且无彩色带。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{siunitx}
\usepackage{array}
\usepackage{booktabs}

\newcolumntype{s}[1]{>{\centering}m{#1}}

\begin{document}

\begin{center}

\begin{tabular}{
  @{}
  s{2cm}
  s{2cm}
  s{2cm}
  s{2cm}
  s{2cm}
  @{}
}
\toprule
Misure di $l_m$\\(\unit{cm}) &
$l_m$\\(\qty{\pm 0.1}{cm}) &
$l_f$\\(\qty{\pm 0.1}{cm}) &
Misure di $T$\\(\unit{s}) &
$T$\\(\qty{\pm 0.01}{s}) \tabularnewline
\midrule
130.9 / 130.9  131.1 / 131.0  131.1 & 131.0 & 133.6 & 2.324 / 2.320  2.317 / 2.324  2.320 & 2.32 \tabularnewline
\addlinespace
110.9 / 111.0  110.8 / 110.9  110.9 & 110.9 & 113.5 & 2.123 / 2.121  2.134 / 2.137  2.131 & 2.13 \tabularnewline
\addlinespace
91.4 / 91.6  91.5 / 91.6  91.5 & 91.5 & 94.1 & 1.948 / 1.930  1.936 / 1.931  1.949 & 1.94 \tabularnewline
\addlinespace
73.2 / 73.2  73.2 / 73.1  73.2 & 73.2 & 75.8 & 1.720 / 1.742  1.725 / 1.735  1.735 & 1.73 \tabularnewline
\addlinespace
47.4 / 47.4  47.4 / 47.3  47.3 & 47.4 & 50.1 & 1.406 / 1.407  1.413 / 1.404  1.411 & 1.41 \tabularnewline
\addlinespace
25.5 / 25.4  25.5 / 25.5  25.4 & 25.5 & 28.0 & 1.047 / 1.035  1.053 / 1.060  1.035 & 1.05 \tabularnewline
\bottomrule
\end{tabular}

\end{center}

\end{document}

在此处输入图片描述

答案2

我宁愿选择表格数组包裹。

输出

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}
\begin{center}
    \begin{tblr}{
        colspec={Q[c,m,6em]Q[c,m,4.5em]Q[c,m,4.5em]Q[c,m,6em]Q[c,m,4.5em]},
        row{odd}={bg=white},
        row{even}={bg=black!10},
        vline{2-Y} = {1}{0.4pt,white},       % vertical lines of top row
        vline{2-Y} = {2-Z}{0.4pt,black},     % vertical lines of 2nd to last row
        hline{3-Y} = {0.4pt,black, solid},   % thin horizontal lines
        hline{1,2,Z} = {0.8pt,black, solid}, % thick horizontal lines
    }
        Misure di $l_m$ ($cm$)              & $l_m$ ($\pm 0.1\:cm$) & $l_f$ ($\pm 0.1\:cm$) & Misure di $T$ ($s$)                 & $T$ ($\pm 0.01\:s$) \\
        130.9 / 130.9  131.1 / 131.0  131.1 & 131.0                 & 133.6                 & 2.324 / 2.320  2.317 / 2.324  2.320 & 2.32                \\
        110.9 / 111.0  110.8 / 110.9  110.9 & 110.9                 & 113.5                 & 2.123 / 2.121  2.134 / 2.137  2.131 & 2.13                \\
        91.4 / 91.6  91.5 / 91.6  91.5      & 91.5                  & 94.1                  & 1.948 / 1.930  1.936 / 1.931  1.949 & 1.94                \\
        73.2 / 73.2  73.2 / 73.1  73.2      & 73.2                  & 75.8                  & 1.720 / 1.742  1.725 / 1.735  1.735 & 1.73                \\
        47.4 / 47.4  47.4 / 47.3  47.3      & 47.4                  & 50.1                  & 1.406 / 1.407  1.413 / 1.404  1.411 & 1.41                \\
        25.5 / 25.4  25.5 / 25.5  25.4      & 25.5                  & 28.0                  & 1.047 / 1.035  1.053 / 1.060  1.035 & 1.05                \\
    \end{tblr}
\end{center}
\end{document}

答案3

{NiceTabular}这是使用创建该表格的方法nicematrix。输出在所有 PDF 查看器中都是完美的:规则似乎不会消失,并且您不会在第一行看到细白线。

\documentclass[a4paper, 12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[italian]{babel}
\usepackage[left=25mm, right=25mm, headheight=32pt, bottom=3cm]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{center}
\NiceMatrixOptions{cell-space-limits=3pt}
\begin{NiceTabular}{m[c]{3cm} | m[c]{2cm} | m[c]{2cm} | m[c]{3cm} | m[c]{2cm}}
\CodeBefore
  \rowcolors{1}{black!10}{white}
\Body
  \toprule
  \Block{1-*}{} % in order to prevent vertical rules in that row
  \Block{}{Misure di $l_m$\\($cm$)} & \Block{}{$l_m$\\($\pm 0.1\:cm$)} & \Block{}{$l_f$\\($\pm 0.1\:cm$)} & \Block{}{Misure di $T$\\($s$)} & \Block{}{$T$\\($\pm 0.01\:s$)} \\ \Hline
  130.9 / 130.9  131.1 / 131.0  131.1 & 131.0 & 133.6 & 2.324 / 2.320  2.317 / 2.324  2.320 & 2.32 \\ \Hline
  110.9 / 111.0  110.8 / 110.9  110.9 & 110.9 & 113.5 & 2.123 / 2.121  2.134 / 2.137  2.131 & 2.13 \\ \Hline
  91.4 / 91.6  91.5 / 91.6  91.5 & 91.5 & 94.1 & 1.948 / 1.930  1.936 / 1.931  1.949 & 1.94 \\ \Hline
  73.2 / 73.2  73.2 / 73.1  73.2 & 73.2 & 75.8 & 1.720 / 1.742  1.725 / 1.735  1.735 & 1.73 \\ \Hline
  47.4 / 47.4  47.4 / 47.3  47.3 & 47.4 & 50.1 & 1.406 / 1.407  1.413 / 1.404  1.411 & 1.41 \\ \Hline
  25.5 / 25.4  25.5 / 25.5  25.4 & 25.5 & 28.0 & 1.047 / 1.035  1.053 / 1.060  1.035 & 1.05 \\
  \bottomrule
\end{NiceTabular}
\end{center}

\end{document}

上述代码的输出

答案4

假设您没有对表格使用任何特殊对齐方式,则似乎没有必要使用\first/\lasthline。作为一个很好的副作用,这还可以避免您遇到的问题:

\documentclass[a4paper, 12pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}

\usepackage[left=25mm, right=25mm, headheight=32pt, bottom=3cm]{geometry}

\usepackage{amsmath,amssymb}

\usepackage{array}
\usepackage{multirow}
\usepackage[table]{xcolor}

\newcolumntype{s}[1]{>{\centering}m{#1}}

\begin{document}

\begin{center}
\rowcolors{1}{black!10}{white}
\begin{tabular}{s{3cm}*{2}{|s{2cm}}|s{3cm}|s{2cm}}
  \hline
  \multicolumn{1}{s{3cm}}{Misure di $l_m$\\(cm)} & \multicolumn{1}{s{2cm}}{$l_m$\\($\pm 0.1\:$cm)} & \multicolumn{1}{s{2cm}}{$l_f$\\($\pm 0.1\:$cm)} & \multicolumn{1}{s{3cm}}{Misure di $T$\\(s)} & \multicolumn{1}{s{2cm}}{$T$\\($\pm 0.01\:$s)} \tabularnewline \hline
  130.9 / 130.9  131.1 / 131.0  131.1 & 131.0 & 133.6 & 2.324 / 2.320  2.317 / 2.324  2.320 & 2.32 \tabularnewline \hline
  110.9 / 111.0  110.8 / 110.9  110.9 & 110.9 & 113.5 & 2.123 / 2.121  2.134 / 2.137  2.131 & 2.13 \tabularnewline \hline
  91.4 / 91.6  91.5 / 91.6  91.5 & 91.5 & 94.1 & 1.948 / 1.930  1.936 / 1.931  1.949 & 1.94 \tabularnewline \hline
  73.2 / 73.2  73.2 / 73.1  73.2 & 73.2 & 75.8 & 1.720 / 1.742  1.725 / 1.735  1.735 & 1.73 \tabularnewline \hline
  47.4 / 47.4  47.4 / 47.3  47.3 & 47.4 & 50.1 & 1.406 / 1.407  1.413 / 1.404  1.411 & 1.41 \tabularnewline \hline
  25.5 / 25.4  25.5 / 25.5  25.4 & 25.5 & 28.0 & 1.047 / 1.035  1.053 / 1.060  1.035 & 1.05 \tabularnewline
  \hline
\end{tabular}
\end{center}

\end{document}

在此处输入图片描述

相关内容