[Tex/LaTex] Adding vertical space (\vspace) around custom environment

environmentsmacrosspacing

I have document with custom environment myenv which left margin is 4em.

\documentclass{book}
\usepackage{lipsum}

\newenvironment{myenv}{
\setlength{\leftskip}{4em}
}


\begin{document}

\lipsum[1-2]

\begin{myenv}

Vertical space should be here.

\lipsum[3-5]

Vertical space should be here.

\end{myenv}

\lipsum[6]

\end{document}

I want to add vertical space around this environment – that is before/above and after/below. Most straightforward, but at the same time labor-intensive, way to accomplish this that I know of is:

\begin{document}

\lipsum[1-2]

\begin{myenv}

\vspace{2em}

\lipsum[3-5]

\vspace{2em}

\end{myenv}

\lipsum[6]

\end{document}

Since I want to be able to change this vertical space without changing vertical spaces in rest of the document, I use macro:

\documentclass{book}
\usepackage{lipsum}

\newenvironment{myenv}{
\setlength{\leftskip}{4em}
}

\def \myvspacemacro{
\vspace{2em}
}


\begin{document}

\lipsum[1-2]

\begin{myenv}

\myvspacemacro{}

\lipsum[3-5]

\myvspacemacro{}

\end{myenv}

\lipsum[6]

\end{document}

Is there any easier and faster way of doing this? Ideally, I would like to set length of top vertical space and bottom vertical space for environment same way as I set leftskip value. Is it possible? There are 2 similar questions: Proper way of vertical spacing before/after environments, Vertical space before and after custom environment, but none of them seems to solve situation like mine.

Simply put, I want to achieve same effect as I would achieve in HTML/CSS by using margin-top and margin-bottom properties.

I also tried to add \vspace to my environment definition, but it only adds margin on top of environment and leaves no margin below environment.

\documentclass{book}
\usepackage{lipsum}

\newenvironment{myenv}{
\vspace{2em}
\setlength{\leftskip}{4em}
}

\begin{document}

\lipsum[1-2]

\begin{myenv}

\lipsum[3-5]

\end{myenv}

\lipsum[6]

\end{document}

Best Answer

How about this? Note that an \newenvironment has three mandatory arguments: the name, the pre-code, and the post-code. Also the line-ending % signs are often required to prevent stray spaces from being introduced (they are not needed for this example, but I place them there as a best practice).

EDITED in response to Barbara's comment and using cgnieder's suggestion. I verified that extra space is no longer added by having the environment start at the top of a page. Thanks to them both.

Further EDIT. David correctly points out that playing with \leftskip is dangerous. I tried to allay his concerns by defining it as a change, not in absolute terms, but that, apparently is not sufficient. Thinking I will leave it to the experts, I will delete my answer... which I can only do once the OP unaccepts it.

\documentclass{book}
\usepackage[nopar]{lipsum}
\newenvironment{myenv}{%
 \par%
 \addtolength{\leftskip}{4em}%
 \vspace{2em}%
}{%
 \par%
 \addtolength{\leftskip}{-4em}%
 \vspace{2em}%
}
\begin{document}
\lipsum[1-2]
\begin{myenv}
\lipsum[3-5]
\end{myenv}
\lipsum[6]
\end{document}