I have created my own environment that creates a table using tabu and then puts some vertical space after it, unless my environment is being nested, in which case I want it to skip the vertical space. However, the vertical space seems to be getting inserted even after a nested use of my environment. What am I doing wrong?
MWE (though I wish it was more M):
\documentclass{article}
\usepackage{tabu}
\usepackage{environ}
\usepackage{xparse}
\makeatletter
\newcommand{\lxtabu@table}[1]{
\begin{tabu}{X}
#1
\end{tabu}%
}
\newcounter{lxtabu@counter}
\NewDocumentEnvironment {lxtabu} {}
{%
\stepcounter{lxtabu@counter}
\Collect@Body\lxtabu@table
}{%
\typeout{ending lxtabu}%
\addtocounter{lxtabu@counter}{-1}%
% Only output vspace if we're not in a nested lxtabu.
\ifnum\value{lxtabu@counter} = 0 %
% If you comment this out the extra vertical space between lines 2
% and 3 goes away. (But so does the space between lines 3 and 4,
% which is bad.)
\typeout{inserting space}\vspace{\baselineskip}%
\fi
}
\makeatother
\begin{document}
\begin{lxtabu}
Line 1 \\
\begin{lxtabu}
Line 2 \\
\end{lxtabu}
\\
Line 3, should not have big empty space above it \\
\end{lxtabu}
Line 4, should have \verb|\baselineskip| space above it.
\end{document}
Result:

My desire is that the spacing between lines 2 and 3 should be the same as between lines 1 and 2.
I'm not positive that the \vspace is being "executed" when it shouldn't be, but if I change it to \vspace{0pt} the unwanted space between lines 2 and 3 goes away. The \typeout commands I added produce the expected result, with "inserting space" only appearing once, at the end.
A few more notes:
I'm using LuaLaTex from MacTeX 2014, which is presumably built upon TeX Live 2014.
I'm using
\Collect@Bodybecause\begin{tabu}and\end{tabu}give errors when used inside the beginning and end, respectively, of\NewDocumentEnvironment. Same problem with just\tabuand\endtabu.Though not shown in the MWE, I need
\NewDocumentEnvironmentfor its argument parsing. However, I'd accept a solution that doesn't involve\NewDocumentEnvironmentif I can still declare an environment that can parse optional arguments after mandatory ones, i.e.\begin{lxtabu}{Foo}[Bar].My
lxtabuenvironment needs to make sure there is space between its bottom and whatever comes next, which could be text, anotherlxtabuenvironment, or perhaps something more exotic. I don't want to have to move the\vspaceoutside of mylxtabuenvironment.


%after the opening brace in the "before" segment; put a space after the0in the check on\value{lxtabu@counter};%isn't needed after\fi. i'd also suspect the double backslash after line 3 as participating in the addition of the blank line. a\parat the beginning of the "after" segment might be a better approach. – barbara beeton Aug 07 '14 at 18:05\\at the end of line 3 it doesn't seem to make any difference to any of the spacing. – dsedivec Aug 07 '14 at 18:18\message{...}%s in significant places, commenting out all double backslashes and blank lines, placing a%after every{ltxtabu}, and even putting\unskipat the end of line 3 to guard against the chance that a space at the end of that line makes it "too long". the "inserting space" is indeed coming only at the desired location, and there is no space before line 3, but, curiously, the vertical space before line 4 is doubled! i will try again later. the enlarged space before line 4 is a mystery to me. – barbara beeton Aug 07 '14 at 18:58\end{ltxtabu}, there is a precedent -- in the multi-line math display structures provided byamsmath. (of course, if your users aren't setting math, that doesn't mean much.) – barbara beeton Aug 07 '14 at 19:01