![新环境中的神秘水平空间](https://linux22.com/image/477337/%E6%96%B0%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E7%A5%9E%E7%A7%98%E6%B0%B4%E5%B9%B3%E7%A9%BA%E9%97%B4.png)
以下代码定义了一个环境,该环境垂直偏移文本(上下偏移 5pt)并水平偏移文本(左侧偏移 2em)。\hspace{2em}
但是,与简单的相比,您会发现该环境引入了神秘的(不需要的)水平空间。是什么原因造成的?如何消除?
\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}
附言:我曾经\par
在没有回车符的情况下强制换行。
附言:如果我对环境的目标能够更加优雅地实现,我将非常感激您的推荐。
答案1
添加一个\ignorespaces
作为“开始”定义的一部分。虚假空格源自宏\begin{offset}
:
\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}
当您使用时,您还会看到空间也被删除了\begin{offset}%
。