1

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.

Werner
  • 603,163
  • 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 01:51

1 Answers1

3

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

enter image description here

\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}%.

Werner
  • 603,163
  • 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
  • 2
    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
  • Oh the tangled webs we weave... – steven_nevets Dec 29 '14 at 21:42
  • I think not having \hspace would be better than adding \ignorespaces If the environment started in vmode then newlines even blank lines at start of the environment would not be a problem. – David Carlisle Dec 30 '14 at 01:53
  • @DavidCarlisle The horizontal offset of 2em is equivalent to the left margin of my custom enumitem list (at each level). This consistency keeps the document from appearing ragged. Would you instead customize the horizontal indention of the quote environment to achieve this end? If so, how? – steven_nevets Dec 30 '14 at 02:52
  • but if the text is more than a line only the first is indented using hspace, I'm trying to work out of you want that (in which case setlength\parindent}{2em} is better way to do it or if you intend to indent the whole text, in which case hspace does not work and you want to use a list enviornment with \leftmargin set to 2em – David Carlisle Dec 30 '14 at 03:10
  • @DavidCarlisle I see your point now. I didn't realize that the hspace only indents the first line. In truth, I'm using the offset primarily as a custom non-centered, single-line \displaymath environment. But there are times that I want to offset non-math material (text blurbs, tables, pictures) by the same indent. So it is a general-purpose offset environment that I'm after. I think your suggestion to use a list environment is probably the way to go. – steven_nevets Dec 30 '14 at 03:36
  • @steven_nevets: adjustwidth from changepage is exactly that - a list environment. – Werner Dec 30 '14 at 03:38
  • @Werner I'll look into that, but I have to admit that I'm growing fatigued by all of the package extensions that I have to call upon for my homework sheets. I'd prefer to define my own commands and environments and incorporate them all by way of a personal style file. – steven_nevets Dec 30 '14 at 03:45