14

In defining an environment, I could use \ignorespacesafterend to ignore all spaces after the environment. But how could I also ignore all \pars and empty lines after the environment?

In other word, how could I modify the following latex source

\documentclass{article}

\newenvironment{abc}{\ignorespaces}{\ignorespacesafterend}

\begin{document}

\begin{abc}
One
\end{abc} 
\par
\begin{abc}
Two
\end{abc} 

\begin{abc}
Three
\end{abc}

\end{document}

to get the following result?

OneTwoThree
2

1 Answer 1

11

You need to drop the command in at the right place (which is why latex has \ignorespacesafterend not just \ignorespaces. Also your sample result indicated that you wanted to not only ignore space after \end{} but also remove the space already added before that, hence the \ifhmode\unskip\fi line. The definition is not quite as in the linked question (I removed the catcode setting)

\documentclass{article}

\newenvironment{abc}{\ignorespaces}{%
\ifhmode\unskip\fi
\aftergroup\useignorespacesandallpars}

\def\useignorespacesandallpars#1\ignorespaces\fi{%
#1\fi\ignorespacesandallpars}

\makeatletter
\def\ignorespacesandallpars{%
  \@ifnextchar\par
    {\expandafter\ignorespacesandallpars\@gobble}%
    {}%
}
\makeatother

\begin{document}

\begin{abc}
One
\end{abc} 
\par
\begin{abc}
Two
\end{abc} 

\begin{abc}
Three
\end{abc}

\end{document}
4
  • I try to understand the above code. It seems that \def\useignorespacesandallpars#1\ignorespaces\fi{#1\fi\ignorespacesandallpars} is dependent on the definition of the \end macro.
    – Z.H.
    May 19, 2014 at 10:52
  • Also, the result is still correct when I remove \aftergroup. Why we need \aftergroup here?
    – Z.H.
    May 19, 2014 at 10:56
  • @Z.H. actually you don't need \afergroup: originally I was going to use \aftergroup\ignorespacesandallpars but that wasn't quite the right place either so I added the \use... macro (which does as you note depend on the definition of \end to explicitly insert it, which means the aftergroup isn't needed (but doesn't do any harm) May 19, 2014 at 11:31
  • 1
    would this work with xparse defined environments as well or would I need to do something different to make that work? In other words does an xparse defined environment still end with \ignorespaces\fi so the neat trick you used to put the \ignorespacesandallpars still works? I tried looking at the code but it went through latex3 stuff I couldn't chase down. Mar 3, 2021 at 11:52

You must log in to answer this question.

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