longtable 和分页符上的自动规则添加

longtable 和分页符上的自动规则添加

我想为长表每隔第 N 行(在本例中为第 3 行)绘制一条规则。我利用此解决方案每 5 行自动添加 \midrule每 3 行绘制一条规则,但我在长表的分页符上,我想重置计数器。我的解决方案是重新定义长表使用的分页符命令,但它不起作用:

平均能量损失

\documentclass{article}

\usepackage[
paperheight=3.25in,
paperwidth=3.25in,
total={3in, 3in}
]{geometry}

\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}

%%% https://tex.stackexchange.com/questions/182953/add-midrule-every-5-lines-automatically
\newcounter{midruleV}
\newcommand*{\midruleV}{%
  \aftergroup\aftergroup\aftergroup\midruleVaux
}
\newif\ifmidruleV
\makeatletter
\newcommand*{\midruleVaux}{%
  \noalign{%
    \stepcounter{midruleV}%
    \ifnum\value{midruleV}=3 %
      \global\midruleVtrue
      \setcounter{midruleV}{0}%
    \else
      \global\midruleVfalse
    \fi
  }
  \ifmidruleV\midrule\fi
}
\newcommand*{\resetmidruleV}{\global\setcounter{midruleV}{0}}
\makeatother

\begin{document}


\resetmidruleV


%%%% my attempts
\let\oldnewpage\newpage
\def\newpage{\oldnewpage\resetmidruleV}

\let\oldbreak\break
\def\break{\oldbreak\resetmidruleV}
%%%%


\begin{longtable}{rclc<{\midruleV}}

\toprule
 Hi\\\toprule
  \endhead % all the lines above this will be repeated on every page
 Continues..\\
  \endfoot
    \bottomrule
  \endlastfoot
1       &     &   &  \\
2       &           &       &    \\
3       &           &       &    \\
4       &           &       &    \\
5       &           &       &    \\  
6       &           &       &    \\
7       &           &       &    \\
8       &           &       &    \\
9       &           &       &    \\
10      &           &       &    \\
11      &           &       &    \\
12       &           &       &    \\
13      &           &       &    \\
14      &           &       &    \\
15      &           &       &    \\
16      &           &       &    \\
17      &           &       &    \\
18      &           &       &    \\
\bottomrule
\end{longtable}

end.

\end{document}

我希望 15 岁以下的中间规则改为 17 岁以下。

在此处输入图片描述

答案1

我想出了一个解决方案,并创建了一个\resetmidruleVonNP在每一行上调用的命令,该命令调用一个 Lua 函数(我使用 Lua 是因为我觉得它更容易)。Lua 函数\label{}为每一行添加一个唯一值,并通过这篇文章检查当前行是否与上一行位于同一页:在 lualatex 中获取 \label 的页码,如果不同,则resetmidruleV调用a。

\documentclass{article}
\usepackage{hyperref}
\usepackage[
paperheight=3.25in,
paperwidth=3.25in,
total={3in, 3in}
]{geometry}

\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}

\usepackage{luacode}


%%% https://tex.stackexchange.com/questions/182953/add-midrule-every-5-lines-automatically
\newcounter{midruleV}
\newcommand*{\midruleV}{%
  \aftergroup\aftergroup\aftergroup\midruleVaux
}
\newif\ifmidruleV
\makeatletter
\newcommand*{\midruleVaux}{%
  \noalign{%
    \stepcounter{midruleV}%
    \ifnum\value{midruleV}=3 %
      \global\midruleVtrue
      \setcounter{midruleV}{0}%
    \else
      \global\midruleVfalse
    \fi
  }
  \ifmidruleV\midrule\fi
}
\newcommand*{\resetmidruleV}{\global\setcounter{midruleV}{0}}
\makeatother



\begin{luacode*}
tabular_row_pages_cntr = 0

function get_ref_page(l)
    local r = token.get_macro('r@'..l)
    if r == nil then
        return 0, 0
    end
    local sec, page =  r:match("{([^}]*)}{([^}]*)}")
    return page
end

function reset_midruleV_on_newpage()
    tabular_row_pages_cntr = tabular_row_pages_cntr + 1
    tex.print('\\label{tabular@row@'..tabular_row_pages_cntr..'}')
    if get_ref_page('tabular@row@'..tabular_row_pages_cntr) - get_ref_page('tabular@row@'..(tabular_row_pages_cntr-1)) == 1 then
      tex.print('\\resetmidruleV')
    end
end
\end{luacode*}
\def\resetmidruleVonNP{\luadirect{reset_midruleV_on_newpage()}}


\begin{document}


\resetmidruleV




\begin{longtable}{rclc<{\resetmidruleVonNP\midruleV}}

\toprule
 Hi\\\toprule
  \endhead % all the lines above this will be repeated on every page
 Continues..\\
  \endfoot
    \bottomrule
  \endlastfoot
1       &     &   &  \\
2       &           &       &    \\
3       &           &       &    \\
4       &           &       &    \\
5       &           &       &    \\
6       &           &       &    \\
7       &           &       &    \\
8       &           &       &    \\
9       &           &       &    \\
10      &           &       &    \\
11      &           &       &    \\
12       &           &       &    \\
13      &           &       &    \\
14      &           &       &    \\ %\newpage
15      &           &       &    \\
16      &           &       &    \\
17      &           &       &    \\
18      &           &       &    \\
\bottomrule
\end{longtable}


end.

\end{document}

在此处输入图片描述

相关内容