13

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
11

You need to drop the command in at the right place (which is why latex has \ifgnorespacesafterend 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}
  • 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 '14 at 10:52
  • Also, the result is still correct when I remove \aftergroup. Why we need \aftergroup here? – Z.H. May 19 '14 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) – David Carlisle May 19 '14 at 11:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.