forum icon indicating copy to clipboard operation
forum copied to clipboard

环境体前后的空格

Open tanukihee opened this issue 5 years ago • 3 comments

检查

  • [x] 已在 issues 中进行搜索(包括已关闭的问题)

编译环境

  • 操作系统

    • [x] Windows 10
    • [ ] Windows 8/8.1
    • [ ] Windows 7
    • [ ] 更早版本的 Windows
    • [ ] macOS
    • [ ] Linux(请附发行版)
  • TeX 发行版

    • [x] TeX Live 2020
    • [ ] MiKTeX
    • [ ] CTeX 套装 2.9.2.164
    • [ ] 更早版本的 CTeX 套装

最小工作示例(MWE)

\documentclass{article}

\usepackage{xparse}

\NewDocumentEnvironment{enva}{O{foo}}{#1}{}
\newenvironment{envb}[1][foo]{#1}{}

\begin{document}
    \begin{enva}
        bar
    \end{enva}

    \begin{enva}[quaz]
        bar
    \end{enva}

    \begin{envb}
        bar
    \end{envb}

    \begin{envb}[quaz]
        bar
    \end{envb}

\end{document}

(用 pdfLaTeX 编译后)效果如下图

image

描述问题

使用 \NewDocumentEnvironment 自定义新环境后,环境开始时会有一个空格,使用 \newenvironment 定义时空格消失。

当它们带参数时,空格都出现。在 \begin{} 后方注释换行符后,空格都消失。

链接

  • TeX.SX:
  • GitHub:

我还做了这些尝试

附件

tanukihee avatar Dec 21 '20 15:12 tanukihee

这个应该是故意为之,正是避免 LaTeX2e 里面环境内容以 [ 开头造成的语义冲突。(记不清在哪里看到的了🤦‍♂️)

stone-zeng avatar Dec 21 '20 17:12 stone-zeng

感觉可以反馈给 https://github.com/latex3/latex3

zepinglee avatar Dec 21 '20 17:12 zepinglee

xparse 可以用 b 来消掉环境内容前后的空格,用 !b 恢复,

\documentclass{article}

\usepackage{xparse}

\NewDocumentEnvironment{enva}{O{foo}}{#1}{.}
\newenvironment{envb}[1][foo]{#1}{.}
\NewDocumentEnvironment{envc}{O{foo}+b}{#1#2}{.}
\NewDocumentEnvironment{envd}{O{foo}+!b}{#1#2}{.}

\begin{document}
\begin{enva}
    bar
\end{enva}

\begin{enva}[quaz]
    bar
\end{enva}

\begin{envb}
    bar
\end{envb}

\begin{envb}[quaz]
    bar
\end{envb}

\begin{envc}
    bar
\end{envc}

\begin{envc}[quaz]
    bar
\end{envc}

\begin{envd}
    bar
\end{envd}

\begin{envd}[quaz]
    bar
\end{envd}

\end{document}

image

\newenvironment 定义时,前方的空格被吞掉,后方换行的空格却会保留……

tanukihee avatar Dec 21 '20 17:12 tanukihee