如何忽略自定义环境末尾的换行符?
\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}