Is there an analogue of \ignorespaces which will also ignore the following \pars?
- 262,582
- 2,795
3 Answers
Here two macros which either ignore all spaces and implicit paragraphs (empty separation line) or also explicit \par macros. Both are based on the fact that \@ifnextchar, which is used to look ahead, also consumes optional spaces before reading the next token. This safes the usage of \ignorespaces. In the first case that token is irrelevant and \@ifnextchar is just use to get the functionality of \ignorespaces but allow us to put code behind it once the spaces are gone.
I came up with this solution while thinking about how to optimize Leo's answer for my applications.
\documentclass{article}
\makeatletter
\def\ignorespacesandimplicitepars{%
\begingroup
\catcode13=10
\@ifnextchar\relax
{\endgroup}%
{\endgroup}%
}
\def\ignorespacesandallpars{%
\begingroup
\catcode13=10
\@ifnextchar\par
{\endgroup\expandafter\ignorespacesandallpars\@gobble}%
{\endgroup}%
}
\makeatletter
% Test cases
\let\ignorespacesandpars\ignorespacesandimplicitepars
%\let\ignorespacesandpars\ignorespacesandallpars
\begin{document}
\def\test#1{#1\ignorespacesandpars}
\test{testa} \test{testb}
\test{testc}
\test{testd}
\test{teste}
\par
\test{testf}
\end{document}
- 262,582
\documentclass{minimal}
\makeatletter
\begingroup
\catcode13=12%
\gdef\gobble@ignorespacesandpars#1{\ignorespacesandpars}%
\gdef\ignorespacesandpars{%
\catcode13=12%
\@ifnextchar^^M{\gobble@ignorespacesandpars}{\catcode13=5}}%
\endgroup
\makeatletter
\begin{document}
\def\test#1{-#1-\ignorespacesandpars}
foo\test{and} bar
foo\test{and}
bar
foo bar
\end{document}
Result:
foo-and-bar
foo-and-bar
foo bar
- 77,365
\def\ignorepar{\penalty 10001 \penalty 10003
\def\par{\ifnum\lastpenalty=10003 \unpenalty \ifnum \lastpenalty=10001 \unpenalty
\else \endgraf \fi \else \endgraf \fi \let\par\endgraf}\ignorespaces}
Note: You may take any penalties more than 10000. The 10001 and the 10003 is choosen because I believe that the probability that the 10001 and 10003 are usually found in a row is very low.
More simple case:
\def\ignorepar{\penalty 12674 \def\par{\ifnum\lastpenalty=12674 \unpenalty \else \endgraf \fi \let\par\endgraf}\ignorespaces}
Edit: no fail if there're more than one blank lines.
\def\ignorepar{\penalty 12674
\def\par{\ifnum\lastpenalty=12674 \else \endgraf \let\par\endgraf \fi}\ignorespaces}
- 3,727
\par, but ignores endlines. Explicit\par's are not ignored. I think it is what we usually need. – Leo Liu Jul 15 '11 at 05:25standaloneclass to ignore implicit paragraphs at the beginning. Two notes: Don't write{\catcode13=5}but{\catcode13=5 }to terminate the number, otherwise if the next paragraph starts with a number then this is also consumed I think. I personally would use grouping to keep the catcode change local, but you require an extra macro for then. – Martin Scharrer Jul 15 '11 at 06:56