This is a follow-up question of my Unbalanced braces with newenvironment / NewEnviron post. The gist is that by using


I can expand

\end{myEnv} text2}


{ text2}

This is working perfectly, thanks again egreg. Note that the braces were really unbalaced before expansion. Now I have come to a similar, yet slightly different situation. I want to expand

\end{myEnv} text2}


\eatWhitespace{ text2}

with the 1-parametric command eatWhitespace.

With which definition of myEnv can I achieve that?

Background info:

Why would anyone ever do this? Well, I am using LyX, but I want to use the caption/subcaption package instead of the subfig package (see also subcaption vs. subfig: Best package for referencing a subfigure). I almost succeeded - the only problem is that the figure environment reacts very sensitive to all linebreaks, and LyX inserts plenty of them. This behavior of LyX is hardcoded, and I do not want to recompile it from scratch. So I worked around that problem entirely witout ERT ... and by using dummy commands, which eat the whitespaces. This is an example of the generated LyX output (yes, it's ugly):





\dummy}}\subfigureBlock{\subfigureWidth}{\subfigureImage}{1st image}\begin{myEnv}


\dummy}}\subfigureBlock{\subfigureWidth}{\subfigureImage}{2nd image}\begin{myEnv}


\dummy}}\subfigureBlock{\subfigureWidth}{\subfigureImage}{3rd image}

As you can see, when you insert after each \end{myEnv} an \eatWhitespace{ it makes sense (and is still ugly). The other whitespaces are taken care of by using mbox, which works. However, this is not possible for the length of the subfigure. Thus this crazy construction with myEnv and unbalanced braces is used.

  • 2
    (1) \centering is not an environment, it is a command. (2) I am totally sure that this is not the right way to proceed: instead, to ignore blank lines, try to do \endlinechar=32 (or -1), or \catcode13=10 (or 9) within your figure environment, and remove all your weird brace constructions. (3) If you insist on your bizarre construction, replace \aftergroup\bgroup by \aftergroup\setbox\aftergroup0\aftergroup\vbox\aftergroup\bgroup, it might work. (4) Don't do this. Commented Jan 5, 2012 at 18:08
  • I think that this my answer can be helpful to you, you can combine that trick with the one you mention at the very beginning.
    – yo'
    Commented Jan 23, 2012 at 0:48

1 Answer 1


[None of the code below is tested. Most of the examples should work, though, up to unbalanced braces or missing \makeatletter.]

Your struggle to work around (unreasonable) limitations of LyX (adding random blank lines) by working around (very reasonable) limitations of TeX (it almost exclusively manipulates balanced token lists) is certainly heroic. However, there are simpler ways to ignore blank lines.

  1. Delimited arguments instead of unbalanced braces.

  2. Changing category codes or the end-line character: this won't work within the argument of another macro.

      {% Uncomment one line.
       % \endlinechar=-1\relax        % or 32 to get spaces
       % \catcode\endlinechar=9\relax % or 10 to get spaces 
      llo, wo
  3. Ignoring spaces and paragraph breaks after the end of an environment by recursively looking for them:

  4. [Don't do this.] Keeping your bizarre construction with unbalanced braces can be done in two ways: either with a box:


    or with a brace trick (see the question on brace tricks):


I cannot help but notice that you are using the \centering command as an environment. Use either \begin{center}...\end{center}, or simply \centering in the scope of the environment whose text you wish to center.

Also... when LyX fails you, just use a text editor.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .