# How to remove consecutive vertical whitespace from commands

I would like to be able to write macros that approximate the behaviour of CSS's margin.

For example consider the following:

\newcommand{\marginspace}[1]{
% Defines some combinable margin
% This is what I'd like to know how to do
% A naive implementation would just be:
% \par\vspace{#1}
}

\newcommand{\lorem}{
\marginspace{\baselineskip}
Lorem
\marginspace{\baselineskip}
}

\newcommand{\ipsum}{
\marginspace{\baselineskip}
Ipsum
\marginspace{\baselineskip}
}


So then this document:

Foo

\lorem

\ipsum

Bar


Would be rendered something like:

Foo

Lorem

Ipsum

Bar


A naive implementation of \marginspace would just be \par\vspace{#1}, but this puts an extra line between "Lorem" and "Ipsum", where I'd like them combined:

Foo

Lorem

Ipsum

Bar


Is there any way to get the white space to be ignored if there's already sufficient whitespace above?

I think you are looking for \addvspace rather than \vspace, but this might depend on what exactly you are going do to with this.

\documentclass{article}

\newcommand{\marginspace}[1]{%
}

\newcommand{\lorem}{%
\marginspace{\baselineskip}%
Lorem
\marginspace{\baselineskip}%
}

\newcommand{\ipsum}{%
\marginspace{\baselineskip}%
Ipsum
\marginspace{\baselineskip}%
}

\begin{document}

Foo

\lorem

\ipsum

Bar

\end{document}


If your two macros work in tandem...that is to say, if \lorem knows what to keep an eye out for (in this case \ipsum), then \lorem can absorb a few arguments and test them for \ipsum.

\documentclass{article}
\newcommand{\marginspace}[1]{
\par\vspace{#1}
}
\newcommand{\lorem}[2]{
\marginspace{\baselineskip}
Lorem
\ifx\par#1
\ifx\ipsum#2\else
\marginspace{\baselineskip}
\fi
\else
\ifx\ipsum#1\else
\marginspace{\baselineskip}
\fi
\fi
#1#2%
}
\newcommand{\ipsum}{
\marginspace{\baselineskip}
Ipsum
\marginspace{\baselineskip}
}
\begin{document}
Foo

\lorem

baz

\ipsum

Bar

\hrulefill

Foo

\lorem

\ipsum

Bar

\hrulefill

Foo
\lorem
\ipsum
Bar
\end{document}