\newenvironment 中的 tabularx

\newenvironment 中的 tabularx

这段代码有问题:

\newenvironment{Tbl}
{\begin{tabularx}{\textwidth}{|l|X|} \hline}
{\end{tabularx}}

但这没问题:

\newenvironment{Tbl}
{\begin{tabular}{|l|l|} \hline}
{\end{tabular}}

为什么?我怎样才能让第一个工作呢?

以下是完整的 LaTeX 文件:

\documentclass[a4paper, 12pt]{article}
\usepackage{tabularx}

\newenvironment{Tbl}
{\begin{tabularx}{\textwidth}{|l|X|} \hline}
{\end{tabularx}}

\begin{document}

\begin{Tbl}
\end{Tbl}

\end{document}

latex test.tex

生成:

This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
 %&-line parsing enabled.
entering extended mode
(./test.tex
LaTeX2e <2005/12/01>
Babel <v3.8h> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, arabic, basque, bulgarian, coptic, welsh, czech, slovak, german, ng
erman, danish, esperanto, spanish, catalan, galician, estonian, farsi, finnish,
 french, greek, monogreek, ancientgreek, croatian, hungarian, interlingua, ibyc
us, indonesian, icelandic, italian, latin, mongolian, dutch, norsk, polish, por
tuguese, pinyin, romanian, russian, slovenian, uppersorbian, serbian, swedish, 
turkish, ukenglish, ukrainian, loaded.
(/usr/share/texmf/tex/latex/base/article.cls
Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf/tex/latex/base/size12.clo))
(/usr/share/texmf/tex/latex/tools/tabularx.sty
(/usr/share/texmf/tex/latex/tools/array.sty)) (./test.aux))
Runaway argument?
 \par 
! File ended while scanning use of \TX@get@body.
<inserted text> 
                \par 
<*> test.tex

? 

答案1

这被认为是“设计状态”tabularx. 请参阅tabularx文档(部分5 宏,第 4 页)。

解决这个问题的方法是使用tabularx环境的“命令形式”:

\newenvironment{Tbl}
  {\tabularx{\textwidth}{|l|X|} \hline}
  {\endtabularx}

答案2

你的代码在我的 LaTeX 实现中运行良好,但我不得不添加

\usepackage{array}
\usepackage{tabularx}

在我的文件序言中。

更新:尝试添加内容:

\begin{Tbl}
Hello & World \\
\end{Tbl}

相关内容