3

I want to change from environment form of tabularx to the command form but it does not compile. I need the command form because I want to use the tabularx inside my self defined environments later.

\documentclass[]{article}
\usepackage{tabularx}
\begin{document}

% working
\begin{tabularx}{\textwidth}{l|l}
my & table
\end{tabularx}

% not working (does not compile)
\tabularx{\textwidth}{l|l}
my & table
\endtabularx

\end{document}
3
  • There's no reason for expecting it to work.
    – egreg
    Dec 29 '15 at 19:14
  • 1
    tabularx is not designed to be used like that so not working is the expected behaviour here, Dec 29 '15 at 19:29
  • Why is it that you would want this behavior? What result are you expecting? At this moment, there isn't really much more to say about your example, other than as egreg pointed out, no reason for it to work.
    – Runar
    Dec 29 '15 at 20:05
2

tabularx look for the end of the table, which is expected to start with \end (usually: \end{tabularx}). However, the command form can be used inside another environment:

\documentclass[]{article}
\usepackage{tabularx}
\begin{document}

\noindent
\begin{tabularx}{\textwidth}{l|X}
my & table
\end{tabularx}

\newenvironment{myenv}{%
  \tabularx{\textwidth}{l|X}%
}{%
  \endtabularx
}

\noindent
\begin{myenv}
my & table
\end{myenv}

\end{document}

Result

1
  • The command form should be used in the definition of another environment, whereas the \begin\end form cannot.
    – egreg
    Dec 29 '15 at 20:56
0

As a general rule, it is wrong to use \env and \endenv in the document body, because \begin{env} and \end{env} do much more than just executing the inner commands.

Just to make a simple example, the following document

\documentclass{article}
\usepackage{lipsum}

\begin{document}

\lipsum[2]

\quote
\lipsum[3]
\endquote

\lipsum[4]

\end{document}

produces

enter image description here

which is clearly not the expected result. Similarly,

\documentclass{article}
\usepackage{lipsum}

\begin{document}

\lipsum[2]
\center
Centered text
\endcenter
\lipsum[3]

\end{document}

produces the following unwanted output:

enter image description here

The inner commands are sometimes used in the definition of another environment, say

\newenvironment{myenum}
  {\enumerate\setlength{\itemsep}{20pt}}
  {\endenumerate}

because this gives better error confinement when myenum is not properly closed.

This is generally optional, but it becomes mandatory for some special environments like tabularx or align, because these environments absorb their contents before acting. So if you want to define a fixed tabularx specimen, you should do something like

\newenvironment{sptabx}
  {\par\nopagebreak\medskip\noindent\tabularx{\textwidth}{lX}}
  {\endtabularx\par\medskip}

so \begin{sptabx}, using special code in the definition of \tabularx, “knows” that it should look for \end{sptabx} as the terminator and absorb everything in between in order to pass it to for typesetting to \endtabularx.

Note that calling \sptabx...\endsptabx would not find \end{sptabx}, so there would be a mysterious error somewhere much later. The same is of course true for \tabularx.

A particular environment must never be called with the inner commands: it is lrbox that should always be used as

\begin{lrbox}{<box bin>}
...
\end{lrbox}

also when used in the definition of an environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy