如何创建自己的自缩放 tabularx 表的新环境,以及向其中添加标题和彩色行?不应使用奇怪的流动命令添加标题captionof
,而应在真实的表格环境中添加。
如果我\begin{tabularx}
在或之后添加\newenvironment
命令形式,我总是收到此错误:\tabularx
\begin{table}
File ended while scanning use of \TX@get@body.
答案1
环境的问题tabularx
在于,它不是实际的环境。它是一个包装的命令,以某种方式拒绝\end
实际的 之后的任何 。解决方案是对和\endtabularx
都使用命令形式。table
tabularx
\newenvironment{xtable}[1]{
\table
\tabularx{\linewidth}{#1}}
{\endtabularx
\endtable}
这是下表的完整 Tex 样本。
\documentclass[a4paper,11pt]{report}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[table]{xcolor}
\usepackage{tabularx}
%Colors
\definecolor{colorTabHead}{gray}{0.3}
\definecolor{colorTab1}{gray}{0.92}
\definecolor{colorTab2}{gray}{0.88}
\definecolor{white}{gray}{1.00}
%Table header
\newcommand{\sthead}[1]
{\cellcolor{colorTabHead}
\textcolor{white}{\sffamily\bfseries #1}}
%New centered X column named Y
\newcolumntype{Y}{>{\centering\arraybackslash}X}
%Table environment
\newenvironment{xtable}[3]{
\table
\renewcommand{\arraystretch}{1.20}
\caption{#2 \label{#3}}
\bigskip
\rowcolors{1}{colorTab1}{colorTab2}
\small\tabularx{\linewidth}{#1}}
{\endtabularx
\endtable}
\begin{document}
\begin{xtable}{XY}{XTable}{tab:fox}
\sthead{X Column} & \sthead{Y Column}\\
The quick brown fox jumps over \newline the lazy dog & The quick brown fox jumps over the lazy dog\\
\end{xtable}
\end{document}
结果是: