忽略环境 \end 之前的换行符

忽略环境 \end 之前的换行符

如何忽略自定义环境末尾的换行符?

\newenvironment{sentence}
  {}
  {.}


\begin{sentence}
This is a sentence


\end{sentence}

输出为

This is a sentence
.

我希望

This is a sentence.

答案1

这很简单有效,但它会禁用环境中的所有段落,而不仅仅是最后一个段落。考虑到环境sentence可能这样就没问题了。

在此处输入图片描述

\documentclass{article}

\newenvironment{sentence}
  {\let\par\relax}
  {\ifhmode\unskip\fi.\endgraf}

\begin{document}



\begin{sentence}This 1 is a sentence\end{sentence}

\begin{sentence}
This 2 is a sentence
\end{sentence}

\begin{sentence}
This 3 is a sentence

\end{sentence}


\end{document}

稍微脆弱一点的版本,允许多个段落

在此处输入图片描述

\documentclass{article}

\newenvironment{sentence}
  {\def\par{\futurelet\tmp\zzpar}}
  {\ifhmode\unskip\fi.\endgraf}

\def\zzpar{%
 \ifx\tmp\par\else
  \ifx\tmp\end\else
    \endgraf
  \fi\fi}

\begin{document}



\begin{sentence}This 1 is a sentence\end{sentence}

\begin{sentence}
This 2 is a sentence
\end{sentence}

\begin{sentence}
This 3 is a sentence

\end{sentence}

\begin{sentence}
This 4a is a first paragraph.

This 4b is a second


\end{sentence}


\end{document}

相关内容