如何编辑 Environ 包的 \BODY

如何编辑 Environ 包的 \BODY

NewEnviron我从包中获得了environ,我用它来封装表格。下面的 MWE 就是一个例子。目的是处理序言中的表格格式,并让用户只提供内容。我现在需要的是一些与\BODY环境命令交互的能力,这样我就可以正确地指定标题(通过ltablex(实际上是longtable)包)并让它正确显示多页表。

表格内容现在扩展为:

\caption{#1} \\ % Include the caption at the top of the table
\endfirsthead % Only show the caption for the first page
\hiderowcolors \caption{Continued} \\ % Include a caption on subsequent pages that just says continued
\showrowcolors % row coloring was turned off for the caption, now lets turn it back on
\endhead % End of header for all pages after first
\hline
\rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
Header 1 & Header 2 \\   % This is part of \BODY
Content 1 & Content 2 \\ % This is part of \BODY
Content 3 & Content 4 \\ % This is part of \BODY
\hline

我希望它是:

\caption{#1} \\ % Include the caption at the top of the table
\hline
\rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
Header 1 & Header 2 \\   % This is part of \BODY
\endfirsthead % Only show the caption for the first page
\hiderowcolors \caption{Continued} \\ % Include a caption on subsequent pages that just says continued
\showrowcolors % row coloring was turned off for the caption, now lets turn it back on
\hline
\rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
Header 1 & Header 2 \\   % This is part of \BODY
\endhead % End of header for all pages after first
Content 1 & Content 2 \\ % This is part of \BODY
Content 3 & Content 4 \\ % This is part of \BODY
\hline

有办法实现这个吗?


最小工作示例

\documentclass{article}

\usepackage[table,dvipsnames]{xcolor}   % Provides coloring for tables and text
\usepackage{ltablex}                    % Customized table formatting (uses tabularx and longtable)
\usepackage{environ}                    % Used to define custom table environment

% Fixes conflict between variable names in tabularx and xcolor
\newcounter{tblerows}
\expandafter\let\csname c@tblerows\endcsname\rownum

\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686} % Define a color used in the tables
\newcommand\setrow[1]{\gdef\rowmac{#1}#1\ignorespaces} % Used for making a row bold
\newcommand\clearrow{\global\let\rowmac\relax} \clearrow % Used for clearing a row formatting
\newcolumntype{L}[1]{>{\hsize=#1\hsize\rowmac\raggedright\arraybackslash}X} % Left-aligned column, input is relative width of page
\keepXColumns

\NewEnviron{doctable}[1]{%
    \centering
    \rowcolors{3}{black!5}{black!15}
    \begin{tabularx}{\textwidth}{L{0.5}|L{0.5}<{\clearrow}}
        \caption{#1} \\ % Include the caption at the top of the table
        \endfirsthead % Only show the caption for the first page
        \hiderowcolors \caption{Continued} \\ % Include a caption on subsequent pages that just says continued
        \showrowcolors % row coloring was turned off for the caption, now lets turn it back on
        \endhead % End of header for all pages after first
        \hline
        \rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
        \BODY
        \hline
    \end{tabularx}
    \label{tbl:somelabel}
}

\begin{document}

    \begin{doctable}{Some Caption}
        Header 1 & Header 2 \\
        Content 1 & Content 2 \\
        Content 3 & Content 4 \\
    \end{doctable}

\end{document}

答案1

标题行应作为进一步的参数给出。

\documentclass{article}

\usepackage[table,dvipsnames]{xcolor}   % Provides coloring for tables and text
\usepackage{ltablex}                    % Customized table formatting (uses tabularx and longtable)
\usepackage{environ}                    % Used to define custom table environment

% Fixes conflict between variable names in tabularx and xcolor
\newcounter{tblerows}
\makeatletter
\let\c@tblerows\rownum
\makeatother

\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686} % Define a color used in the tables
\newcommand\setrow[1]{\gdef\rowmac{#1}#1\ignorespaces} % Used for making a row bold
\newcommand\clearrow{\global\let\rowmac\relax} \clearrow % Used for clearing a row formatting
\newcolumntype{L}[1]{>{\hsize=#1\hsize\rowmac\raggedright\arraybackslash}X} % Left-aligned column, input is relative width of page
\keepXColumns

\NewEnviron{doctable}[2]{%
  \centering
  \rowcolors{3}{black!5}{black!15}
  \begin{tabularx}{\textwidth}{L{0.5}|L{0.5}<{\clearrow}}
    \caption{#1} \\ % Include the caption at the top of the table
    \hline
    \rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
    #2 \\
    \hline
    \endfirsthead % Only show the caption for the first page
    \hiderowcolors \caption{Continued} \\ % Include a caption on subsequent pages that just says continued
    \showrowcolors % row coloring was turned off for the caption, now lets turn it back on
    \rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
    #2 \\
    \hline
    \endhead % End of header for all pages after first
    \BODY
    \hline
    \end{tabularx}
}

\begin{document}

\begin{doctable}
  {Some Caption\label{somelabel}}
  {Header 1 & Header 2}
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
\end{doctable}

\end{document}

在此处输入图片描述

另外,但我不推荐这样做,

\documentclass{article}

\usepackage[table,dvipsnames]{xcolor}   % Provides coloring for tables and text
\usepackage{ltablex}                    % Customized table formatting (uses tabularx and longtable)
\usepackage{environ}                    % Used to define custom table environment

% Fixes conflict between variable names in tabularx and xcolor
\newcounter{tblerows}
\makeatletter
\let\c@tblerows\rownum
\makeatother

\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686} % Define a color used in the tables
\newcommand\setrow[1]{\gdef\rowmac{#1}#1\ignorespaces} % Used for making a row bold
\newcommand\clearrow{\global\let\rowmac\relax} \clearrow % Used for clearing a row formatting
\newcolumntype{L}[1]{>{\hsize=#1\hsize\rowmac\raggedright\arraybackslash}X} % Left-aligned column, input is relative width of page
\keepXColumns

\makeatletter
\long\def\split@doublebar#1\\#2\@nil{%
  \def\doctableheader{#1}%
  \def\doctablebody{#2}%
}

\NewEnviron{doctable}[1]{%
  \centering
  \expandafter\split@doublebar\BODY\@nil
  \rowcolors{3}{black!5}{black!15}
  \begin{tabularx}{\textwidth}{L{0.5}|L{0.5}<{\clearrow}}
    \caption{#1} \\ % Include the caption at the top of the table
    \hline
    \rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
    \doctableheader \\
    \hline
    \endfirsthead % Only show the caption for the first page
    \hiderowcolors \caption{Continued} \\ % Include a caption on subsequent pages that just says continued
    \showrowcolors % row coloring was turned off for the caption, now lets turn it back on
    \rowcolor{greyblue} \setrow{\bfseries} % Make the header row bold and colored grey-blue
    \doctableheader \\
    \hline
    \endhead % End of header for all pages after first
    \doctablebody
    \hline
    \end{tabularx}
}
\makeatother

\begin{document}

\begin{doctable}
  {Some Caption\label{somelabel}}
  Header 1 & Header 2 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
  Content 1 & Content 2 \\
  Content 3 & Content 4 \\
\end{doctable}

\end{document}

答案2

这是一个复杂的事情,我看不到任何简单的解决方案。

我将在这里提供一些解决此问题的策略:

\let\rest\BODY
\def\knabbknabb#1&#2{#1\def\rest{#2}}
\def\knibbknibb#1\\#2{#1\def\rest{#2}}
\expandafter\unexpanded\knabbknabb\rest\relax
\expandafter\unexpanded\knibbknibb\rest\relax
\let\BODY\rest

首先我复制\BODY\rest,然后定义我的剪切函数。

的输出\knabbknabb{rest}Header 1, 的输出knibbknibb{rest}Header 2

之后,\rest包含

Content 1 & Content 2 \\
Content 3 & Content 4 \\

并复制回\BODY

这就是我的策略。

相关内容