定义一个新的环境/命令来封装颜色框

定义一个新的环境/命令来封装颜色框

我有以下工作 MWE 文件:

\documentclass{article}
\usepackage[rgb,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{booktabs}
%
\begin{document}
\begin{table}[!hbt]
\renewcommand{\arraystretch}{1.5}%
\centering%
\setlength{\fboxsep}{0pt}%
\arrayrulecolor{blue}%
\colorbox{lightgray}{%
\begin{tabular}{l l}
\toprule%
Column 1 & Column 2\tabularnewline% Unchanging column headers
\midrule%
One-one & One-two\tabularnewline% Changing content line one
Two-one & Two-two\tabularnewline% Changing content line two
\bottomrule%
\end{tabular}}
\end{table}
\end{document}

我想将此设置的静态部分提取到新环境或新命令中,以便只有变化的内容行才会传递到新环境,例如mytable

\begin{mytable}
One-one & One-two\tabularnewline% Changing content line one
Two-one & Two-two\tabularnewline% Changing content line two
\end{mytable}

或新命令,\mytable例如

\mytable{%
One-one & One-two\tabularnewline% Changing content line one
Two-one & Two-two\tabularnewline% Changing content line two
}

\colorbox似乎会破坏这一努力。请问我该如何解决这个问题?

编辑

为了回答@David Carlisle 的疑问,下面是我尝试过的命令版本:

\documentclass{article}
\usepackage[rgb,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{booktabs}
%
\newcommand{\mycontents}[1]{%
\renewcommand{\arraystretch}{1.5}%
\centering%
\setlength{\fboxsep}{0pt}%
\arrayrulecolor{blue}%
\colorbox{lightgray}{%
\begin{tabular}{l l}
\toprule%
Column 1 & Column 2\tabularnewline% Column headers
\midrule%
#1 % mind no braces {} around #1; that caused the error reported below
\bottomrule%
\end{tabular}}} % table environment removed as suggested by @egreg
%
\begin{document}

\mycontents{One-one & One-two\tabularnewline%
Two-one & Two-two\tabularnewline}

\end{document}

错误是:

```! Missing } inserted.
<inserted text> 
}
l.25 Two-one & Two-two\tabularnewline}
                                    
?```

答案1

我不会使用table:当没有标题时,table环境实际上没有意义,因为如果表格浮动就无法识别它。

如果您希望所有这些表格都居中,则可以使用以下代码中的和来代替\begingroup和。但也许您希望并排放置两个表格,这是不可能的。\endgroup\begin{center}\end{center}

\documentclass{article}
\usepackage[rgb,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{booktabs}

\newcommand{\mytable}[1]{%
  \begingroup
  \renewcommand{\arraystretch}{1.5}%
  \setlength{\fboxsep}{0pt}%
  \arrayrulecolor{blue}%
  \colorbox{lightgray}{%
    \begin{tabular}{l l}
    \toprule
    Column 1 & Column 2 \\
    \midrule
    #1
    \bottomrule
    \end{tabular}%
  }%
  \endgroup
}

\begin{document}

\mytable{
  One-one & One-two \\
  Two-one & Two-two \\
}

\end{document}

请注意,如果将尾随放在之前,则必须始终\\在末尾添加,或者永远不添加。\\\bottomrule

在此处输入图片描述

这种情况能缓解吗?可以,但还需要进一步努力。

\documentclass{article}
\usepackage[rgb,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{booktabs}

\NewDocumentCommand{\mytableheaders}{}{Column 1 & Column 2}

\ExplSyntaxOn
\NewDocumentCommand{\mytable}{m}
 {
  \group_begin:
  \renewcommand{\arraystretch}{1.5}%
  \setlength{\fboxsep}{0pt}%
  \arrayrulecolor{blue}%
  \colorbox{lightgray}
   {
    \begin{tabular}{l l}
    \toprule
    \mytableheaders \\
    \midrule
    \chandra_mytable_body:n { #1 }
    \bottomrule
    \end{tabular}
  }
  \group_end:
}

\cs_new_protected:Nn \chandra_mytable_body:n
 {
  \regex_match:nnTF { (\c{\\} | \c{tabularnewline})\s* \Z } { #1 }
   {% there was a trailing \\
    #1
   }
   {% no trailing \\
    #1 \\
   }
 }

\ExplSyntaxOff

\begin{document}

\mytable{
  One-one & One-two \\
  Two-one & Two-two \\
}

\bigskip

\mytable{
  One-one & One-two \tabularnewline
  Two-one & Two-two \tabularnewline
}

\bigskip

\mytable{
  One-one & One-two \\
  Two-one & Two-two
}

\bigskip

\mytable{
  One-one & One-two \tabularnewline
  Two-one & Two-two
}

\end{document}

检查参数的尾部\\\tabularnewline后部是否有空格(可能在 之后的结束行插入\\)。如果没有,则添加一个。

在此处输入图片描述

相关内容