Mysterious horizontal space in newenvironment

The following code defines an environment that offsets text vertically (5pt above and below) and horizontally (2em from the left). When compared to a simple \hspace{2em}, however, you can see that the environment introduces mysterious (unwanted) horizontal space. What is causing this and how can it be eliminated?

\documentclass{article}

\setlength{\parindent}{0pt}

\newenvironment{offset}
{\par\vspace{5pt}\hspace{2em}}{\par\vspace{5pt}}

\begin{document}
Surrounding text.
\begin{offset}
Foo
\end{offset}
Surrounding text

\hspace{2em}Foo %for comparison
\end{document}


Ps. I've used \par to force a new paragraph in the absence of a carriage return.

Pss. If my objective with the environment can be realized more elegantly, I'd appreciate the recommendation.

• A "more elegant" way of constructing the environment would probably require some use-case examples. – Werner Dec 29 '14 at 20:06
• why do you have the 2em horizontal space? It just indents the first line?? If you want to indent the entire paragraph use an environment like quote if you just want a start of paragraph indent use \parindent. Either of these would not start the paragraph prematurely and cause the bad space from the end oof line, adding \ignorespaces masks the problem but the reral problem is the apparent mis-use of \hspace – David Carlisle Dec 30 '14 at 1:51

Add an \ignorespaces as part of your "beginning" definition. The spurious space stems from the \begin{offset} macro:

\documentclass{article}

\setlength{\parindent}{0pt}

\newenvironment{offset}
{\par\vspace{5pt}\hspace{2em}\ignorespaces}
{\par\vspace{5pt}}

\begin{document}
Surrounding text.
\begin{offset}
Foo
\end{offset}
Surrounding text

\hspace{2em}Foo %for comparison
\end{document}


You'll see the space removed as well when you use \begin{offset}%.

• I see. Thank you. To your request for use-case examples, I intend to use the environment as a general purpose offset for "small" bits of text, math formulae, tables, pictures, etc.. – steven_nevets Dec 29 '14 at 20:20
• You could consider the quoting environment, or using adjustwidth from changepage. – Werner Dec 29 '14 at 20:21
• When I make the concluding vertical space an argument with \newenvironment{offset}[1]{\par\vspace{5pt}\hspace{2em}\ignorespaces}{\par\vspace{#1}}, I get errors with the call 'begin{offset}{5pt}Foo\end{offset}. What am I doing wrong? – steven_nevets Dec 29 '14 at 21:22
• @steven_nevets: For this you'll have to see Why can't the end code of an environment contain an argument? – Werner Dec 29 '14 at 21:26
• Oh the tangled webs we weave... – steven_nevets Dec 29 '14 at 21:42