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.

Is there an analogue of \ignorespaces which will also ignore the following \pars?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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}
share|improve this answer
\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
share|improve this answer
    
NOTE: This does nothing with \par, but ignores endlines. Explicit \par's are not ignored. I think it is what we usually need. –  Leo Liu Jul 15 '11 at 5:25
    
Nice, I actually might need this as well for the standalone class 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 6:56
\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}
share|improve this answer
    
Funny solution. But it fails if there're more than one blank lines. –  Leo Liu Jul 15 '11 at 11:45
    
@Leo Liu. I fixed. –  Alexey Malistov Jul 15 '11 at 12:03

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.