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
Z.H.
  • 3,346
  • 21
  • 39
  • take a look at http://tex.stackexchange.com/questions/23100/looking-for-an-ignorespacesandpars – d-cmst May 19 '14 at 07:55
  • @dcmst I tried to put \ignorespacesandallpars from that answer into my \newenvironment, but it failed to remove \pars. – Z.H. May 19 '14 at 08:00

1 Answers1

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}

user202729
  • 7,143
David Carlisle
  • 757,742
  • 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
  • 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. – Peter Gerdes Mar 03 '21 at 11:52