![忽略环境 \end 之前的换行符](https://linux22.com/image/435896/%E5%BF%BD%E7%95%A5%E7%8E%AF%E5%A2%83%20%5Cend%20%E4%B9%8B%E5%89%8D%E7%9A%84%E6%8D%A2%E8%A1%8C%E7%AC%A6.png)
如何忽略自定义环境末尾的换行符?
\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}