将 \tabularnewline 替换为 \hline?

将 \tabularnewline 替换为 \hline?

如果我有以下由另一个工具自动生成的代码(在此改编以便共享),我无法修改:

\documentclass[oneside,a4paper]{book}

\usepackage{longtable}
\usepackage{tablefootnote}
\usepackage{booktabs}

\begin{document}

\begin{longtable}[]{@{}ll@{}}
\toprule
\begin{minipage}[b]{0.29\columnwidth} Value\strut \end{minipage} & \begin{minipage}[b]{0.65\columnwidth} Description\strut \end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.29\columnwidth} \textbf{Value 1}\strut \end{minipage} & \begin{minipage}[t]{0.65\columnwidth} Some details for 1\strut \end{minipage}\tabularnewline
\begin{minipage}[t]{0.29\columnwidth} \textbf{Value 2}\strut \end{minipage} & \begin{minipage}[t]{0.65\columnwidth} Some details for 2\strut \end{minipage}\tabularnewline
\bottomrule
\end{longtable}
\end{document}

有没有什么方法可以让我轻松地\tabularnewline\hline某种方式替换乳胶序言中的?

我尝试过类似的事情,但没有成功。Relax 似乎有效,但切换到 hline 却无效。

\let\toprule\hline
\let\midrule\hline
\let\bottomrule\relax
\let\tabularnewline\hline

我基本上是尝试在每一行的顶部、底部和之后获得水平线,以便它最终看起来像这样(使用手动搜索和替换作为概念证明):

目的

这可能吗?

答案1

环境在启动时tabular会定义tabularnewline一些适当的值,因此如果您尝试预先定义它,它将在环境启动时被覆盖。

这实际上是按照 OP 的要求去做的(不考虑这是否是个好主意),将每个规则都替换tabularnewline\\ \hline。 (请注意,中间规则和底部规则会重复。处理它们是读者的练习。)

评论

  • \__a违反了 expl3 的命名风格。如果需要,请自行修复。
  • texdoc interface3文档可以在/中找到texdoc xparse
  • Synctex 丢失,并且无法在环境内使用 verbatim。这些问题可以修复,但有点困难。
  • 为了简单起见,环境会获取om中的初始参数#1。在这种情况下,这并不重要,但如果您修改了代码,请小心。
%! TEX program = lualatex
\documentclass[oneside,a4paper]{book}

\usepackage{longtable}
\usepackage{tablefootnote}
\usepackage{booktabs}

\ExplSyntaxOn

% ======== backup the old environment
\let    \oldlongtable    \longtable
\let \endoldlongtable \endlongtable


% ======== override it
\RenewDocumentEnvironment {longtable} {+b} {
  \tl_set:Nn \__a {#1}
  \regex_replace_all:nnN {\c{tabularnewline}} {\c{ \\ }\c{hline}} \__a
  \exp_args:NV \__printtable:n \__a
}{}

\cs_set_protected:Npn \__printtable:n #1 {
  \begin{oldlongtable} #1 \end{oldlongtable}
}


\ExplSyntaxOff


\begin{document}

\begin{longtable}[]{@{}ll@{}}
\toprule
\begin{minipage}[b]{0.29\columnwidth} Value\strut \end{minipage} & \begin{minipage}[b]{0.65\columnwidth} Description\strut \end{minipage}\tabularnewline
\midrule
\endhead
\begin{minipage}[t]{0.29\columnwidth} \textbf{Value 1}\strut \end{minipage} & \begin{minipage}[t]{0.65\columnwidth} Some details for 1\strut \end{minipage}\tabularnewline
\begin{minipage}[t]{0.29\columnwidth} \textbf{Value 2}\strut \end{minipage} & \begin{minipage}[t]{0.65\columnwidth} Some details for 2\strut \end{minipage}\tabularnewline
\bottomrule
\end{longtable}
\end{document}

答案2

我不会尝试你尝试做的事情。更好、更简单的方法是使用tabularray包。使用它你的代码将变得非常简短和清晰:

\documentclass{article}
\usepackage{tabularray}



\begin{document}
\begin{longtblr}[
caption = Table title
                ]{hline{1,Z}=0.8pt, hline{2}=0.4pt, hline{3-Z}=solid,
                 colspec = {@{} X[0.3,l, font=\bfseries]X[0.7,c] @{}},
                 row{1}={font=\bfseries},
                 rowhead=1
                 }
Value   &   Description             \\
Value 1 &   Some details for 1      \\
Value 2 &   Some details for 2      \\
Value 3 &   Some details for 3      \\
Value 4 &   Some details for 4      \\
\end{longtblr}
\end{document}

在此处输入图片描述

但是,我不会在每行后画线。如果第二列单元格中的文本只有一行,则还不清楚。如果有更多行,那么居中可能不是最佳选择。例如:

\documentclass{article}
\usepackage{tabularray}

\usepackage{lipsum}


\begin{document}
\begin{longtblr}[
caption = Table title
                ]{hline{1,Z}=0.8pt, hline{2}=0.4pt, 
                 colspec = {@{} X[0.3,l, font=\bfseries]X[0.7,j] @{}},
                 row{1}={font=\bfseries},
                 row{2-Y}={rowsep=3pt},
                 rowhead=1
                 }
Value   &   Description     \\
Value 1 &   \lipsum[1]     \\
Value 2 &   \lipsum[2]     \\
Value 3 &   \lipsum[11]     \\
Value 4 &   \lipsum[66]     \\
\end{longtblr}
\end{document}

无论第二列的文本是否居中,看起来都很漂亮。

在此处输入图片描述

相关内容