添加

添加

如何\bottomrule在环境末尾自动添加tabularx

\documentclass{scrartcl}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{etoolbox}

\newenvironment{mytabular}[1]{%
  \expanded{\noexpand\begin{tabular}{#1}}%
  \toprule%
}{%
  \bottomrule%
  \end{tabular}%
}%

\newenvironment{mytabularx}[1]{%
  %\apptocmd\tabularx{\toprule}{}{\errmessage{Failed to patch tabularx}}
  % => Package array Error:  Illegal pream-token (\toprule): `c' used.

  \expanded{\noexpand\tabularx{\textwidth}{#1}}%
  \toprule
  % => works
}{%
  \endtabularx%
}%

\newenvironment{mytabularX}[1]{%
  %\pretocmd\TX@endtabularx{\bottomrule}{}{\errmessage{Failed to patch tabularx}}
  % => ./main.tex:59: Misplaced \noalign.
  % => \bottomrule ->\noalign
  % =>                        {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global...
  % => l.59 \begin{mytabularX}{Xcrl}
  %\preto\TX@endtabularx{\bottomrule}%
  % => ./main.tex:59: Misplaced \noalign. (same as above)

  \expanded{\noexpand\tabularx{\textwidth}{#1}}%
}{%
  %\bottomrule
  % => ./main.tex:61: Misplaced \noalign.
  % => \bottomrule ->\noalign
  % =>                        {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global...
  % => l.61 \end{mytabularX} (same error, different line)
  \endtabularx%
}%

\begin{document}

\begin{mytabular}{lcrl}
  this & is & my & table \\
\end{mytabular}

\vspace{2cm}

\begin{mytabularx}{lcrX}
  this & is & my & table \\
\end{mytabularx}

\vspace{2cm}

\begin{mytabularX}{Xcrl}
  this & is & my & table \\
\end{mytabularX}

\end{document}

最令人沮丧的部分是它可以在环境开始时进行添加。

答案1

我建议你使用tabularray包,它可以实现内容和样式的分离。

\documentclass{article}
\usepackage{lipsum}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{width=\linewidth,colspec={Xcrl},hline{1,Z}={.08em},hline{2-Y}={.05em}}
\lipsum[1][1-5] & is & my & table \\
\lipsum[1][1-2] & is & my & table \\
this            & is & my & table \\
this            & is & my & table \\
\end{tblr}
\end{document}

在此处输入图片描述

添加

\documentclass{article}
\usepackage{lipsum}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{width=\linewidth,colspec={Xcrl},hline{1,Z}={.08em}}
\lipsum[1][1-5] & is & my & table \\
\lipsum[1][1-2] & is & my & table \\
this            & is & my & table \\
this            & is & my & table \\
\end{tblr}
\end{document}

在此处输入图片描述


\documentclass{article}
\usepackage{lipsum}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{width=\linewidth,colspec={Xcrl},hline{Z}={.08em}}
\lipsum[1][1-5] & is & my & table \\
\lipsum[1][1-2] & is & my & table \\
this            & is & my & table \\
this            & is & my & table \\
\end{tblr}
\end{document}

在此处输入图片描述

答案2

供参考,如果您使用{NiceTabular}nicematrix它接受X样式的列tabularx),您可以直接在环境上编程一个环境{NiceTabular}(因为{NiceTabular}不会像 那样捕获其主体{tabularx})。

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{lipsum}% for dummy text

\NewDocumentEnvironment{MyTabularX}{m}
  {\begin{NiceTabular}{#1}
   \toprule
  }
  {\bottomrule
   \end{NiceTabular}%
  }

\begin{document}

\begin{MyTabularX}{lX}
some text & \lipsum[1] \\
some longer text & \lipsum[2] \\
\end{MyTabularX}

\end{document}

但是有一个(小)缺点:您需要多次编译(nicematrix在文件中写入信息aux)。

上述代码的输出

答案3

您可以使用\preto\endarray{\bottomrule},它是数组包中用于一般表格的钩子。范围似乎仅限于当前环境。

答案4

我认为您的目标是定义和使用一个名为的环境mytabularx,该环境需要添加以下内容才能使其像正常tabularx环境一样工作:

  • 自动替换\begin{mytabularx}{<col spec>}

    \begin{tabularx}{\textwidth}{<col spec>} \toprule
    

    即提供{\textwidth}参数并附加\toprule。换句话说,您不想向环境提供这两个“项目” mytabularx,对吗?

  • 自动替换\end{mytabularx}

    \bottomrule \end{tabularx}
    

    即,将缺失的\bottomrule指令添加到前缀\end{tabularx}。这是您帖子第一句话中提到的目标,但这不是唯一的目标,是吗?

我将在此待办事项列表中添加第三项:

  • 自动添加\noindent前缀\begin{mytabularx}前缀。这绝对是必要的除非长度参数\parindent已设置为零。

如果你愿意并且能够使用 LuaLaTeX,你可以通过以下方式实现格式化目标:(a)设置一个 Lua 函数(mytabularx在下面的代码中调用),该函数利用 Lua 的gsub函数执行所需的字符串替换操作;(b)将此函数分配给 LuaTeX 的process_input_buffer回调,使其充当 LaTeX 代码的预处理器。这样,Lua 函数就可以对 LaTeX 代码进行操作TeX 启动其自身的任何处理(例如宏扩展等)。因此,LaTeX 永远不会“看到”名为 的环境mytabularx;相反,它会在设备齐全的tabularx环境中工作。

下面的代码提供了一个名为的 Lua 函数mytabularx和两个 LaTeX 辅助宏,分别称为\mytabularxOn\mytabularxOff。(您可以自由地想出不同的名字……)第一个宏激活该mytabularx函数,即将其分配给process_input_buffer回调,第二个宏停用它。

在此处输入图片描述

% !TEX TS-program = lualatex
\documentclass{scrartcl}
\usepackage{tabularx,booktabs,lipsum,luacode}

%% Lua-side code:
\begin{luacode}
function mytabularx ( s )
  s = s:gsub ( '\\begin%s-{mytabularx}%s-(%b{})' , 
               '\\noindent\\begin{tabularx}{\\textwidth}%1\\toprule' ) 
  s = s:gsub ( '\\end%s-{mytabularx}' ,
               '\\bottomrule\\end{tabularx}' )
  return s
end
\end{luacode}

%% LaTeX-side code: Macros to activate and deactivate the Lua function.
\newcommand\mytabularxOn{\directlua{luatexbase.add_to_callback (
   'process_input_buffer' , mytabularx , 'mytabularx' )}}
\newcommand\mytabularxOff{\directlua{luatexbase.remove_from_callback (
   'process_input_buffer' , 'mytabularx' )}}
\mytabularxOn % activate the Lua function by default

\begin{document}

% first, a tabularx environment without \toprule and \bottomrule:
\begin {tabularx} {\textwidth} {lcrX}
  this & is & my & \lipsum[1][1-5] \\ 
% Note that material spills into the right-hand margin
\end{tabularx}

\vspace{1cm}
 
% second, the 'mytabularx' environment
\begin {mytabularx} {lcrX}  
  this & is & my & \lipsum[1][1-4] \\
\end{mytabularx}

\end{document}

相关内容