Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question
    
take a look at tex.stackexchange.com/questions/23100/… –  dcmst May 19 at 7:55
    
@dcmst I tried to put \ignorespacesandallpars from that answer into my \newenvironment, but it failed to remove \pars. –  Z.H. May 19 at 8:00

1 Answer 1

up vote 7 down vote accepted

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}
share|improve this answer
    
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 at 10:52
    
Also, the result is still correct when I remove \aftergroup. Why we need \aftergroup here? –  Z.H. May 19 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 at 11:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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