NewDocumentEnvironment 和 xparse 组合在之后添加了不需要的空间

NewDocumentEnvironment 和 xparse 组合在之后添加了不需要的空间

这是这个,但现在恐怕情况不同了。可接受的答案似乎在自定义环境后放置了不需要的空格。我不确定如何删除它们以及导致它们的原因是什么。如果这个问题有点模糊,请原谅,因为我完全不知道。

这是一个 MWE。

\documentclass[a4paper,leqno]{amsart}
\usepackage{amsmath, xparse,xspace}
\setlength{\parindent}{0pt}

\makeatletter
\newcommand{\eqnum}{\textup{\tagform@{\theequation}}}
\makeatother

%%%%%
\ExplSyntaxOn

\NewDocumentEnvironment{aligncol}{m+b}{\guissmo_aligncol:nn {#1} {#2}}{}
\NewDocumentEnvironment{aligncola}{m+b}{\guissmo_aligncol:nn {#1} {#2}}{a}

\seq_new:N \l__guissmo_aligncol_in_seq
\seq_new:N \l__guissmo_aligncol_out_seq
\seq_new:N \l__guissmo_aligncol_temp_seq
\int_new:N \l__guissmo_aligncol_rows_int

\cs_new_protected:Nn\guissmo_aligncol:nn
{
%storetherowsinthesequence
\seq_set_split:Nnn\l__guissmo_aligncol_in_seq{\\}{#2}
\int_compare:nF{\int_mod:nn{\seq_count:N\l__guissmo_aligncol_in_seq}{#1}=0}
{%numberofrowsnotamultipleof#1,addemptyentries
\prg_replicate:nn{#1-\int_mod:nn{\seq_count:N\l__guissmo_aligncol_in_seq}{#1}}
{
\seq_put_right:Nn\l__guissmo_aligncol_in_seq{}
}
}
\seq_clear:N\l__guissmo_aligncol_out_seq
\int_set:Nn\l__guissmo_aligncol_rows_int
{\int_div_truncate:nn{\seq_count:N\l__guissmo_aligncol_in_seq}{#1}}
\int_step_inline:nn{\l__guissmo_aligncol_rows_int}
{%clearthetemporarysequence
\seq_clear:N\l__guissmo_aligncol_temp_seq
\int_step_inline:nnn{0}{#1-1}
{
\seq_put_right:Nx\l__guissmo_aligncol_temp_seq
{
\seq_item:Nn\l__guissmo_aligncol_in_seq
{##1+\l__guissmo_aligncol_rows_int*####1}
}
}
\seq_put_right:Nx\l__guissmo_aligncol_out_seq
{
\seq_use:Nn\l__guissmo_aligncol_temp_seq{&}
}
}
%now\l__guissmo_aligncol_out_seqcontainsthenewrows
\begin{equation}\eqnum
\begin{minipage}{0.9\textwidth}
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\setlength{\abovedisplayshortskip}{0pt}
\setlength{\belowdisplayshortskip}{0pt}
\setbox0=\hbox{T}%tallestletterofthefirstlineofotherminipage
\vskip-\ht0
\begin{align*}
\seq_use:Nn\l__guissmo_aligncol_out_seq{\\}
\end{align*}
\end{minipage}
\end{equation}
}
\ExplSyntaxOff

\begin{document}

Hello!
\begin{aligncol}{1}
a &= a
\end{aligncol}
This has an extra space at the beginning. I don't want it.
\\ This does not not.
\begin{aligncol}{1}
a &= a
\end{aligncol}This does NOT have an extra space, but the code is ugly.
\\ This one does NOT have extra space too!
\begin{aligncola}{1}
a &= a
\end{aligncola}
It seems that the space comes after the environment definition.
\\ Is it the indeed case?

\end{document}

以下是输出的屏幕截图。 在此处输入图片描述

答案1

如果您的环境以 结尾\end{equation},那么这是一个标准问题。使用\ignorespacesafterend

\documentclass[a4paper,leqno]{amsart}
\usepackage{amsmath, xparse}

\makeatletter
\newcommand{\eqnum}{\textup{\tagform@{\theequation}}}
\makeatother

%%%%%
\ExplSyntaxOn

\NewDocumentEnvironment{aligncol}{mb}
 {
  \guissmo_aligncol:nn {#1} {#2}
 }
 {
  \ignorespacesafterend
 }

\seq_new:N \l__guissmo_aligncol_in_seq
\seq_new:N \l__guissmo_aligncol_out_seq
\seq_new:N \l__guissmo_aligncol_temp_seq
\int_new:N \l__guissmo_aligncol_rows_int

\cs_new_protected:Nn\guissmo_aligncol:nn
 {
  %store the rows in the sequence
  \seq_set_split:Nnn \l__guissmo_aligncol_in_seq {\\} {#2}
  \int_compare:nF { \int_mod:nn {\seq_count:N\l__guissmo_aligncol_in_seq}{#1}=0}
   {% number of rows not a multiple of #1, add empty entries
    \prg_replicate:nn {#1-\int_mod:nn{\seq_count:N\l__guissmo_aligncol_in_seq}{#1}}
     {
      \seq_put_right:Nn \l__guissmo_aligncol_in_seq {}
     }
   }
  \seq_clear:N \l__guissmo_aligncol_out_seq
  \int_set:Nn \l__guissmo_aligncol_rows_int
   {
    \int_div_truncate:nn { \seq_count:N \l__guissmo_aligncol_in_seq } {#1}
   }
  \int_step_inline:nn { \l__guissmo_aligncol_rows_int }
   {% clear the temporary sequence
    \seq_clear:N \l__guissmo_aligncol_temp_seq
    \int_step_inline:nnn {0} {#1-1}
     {
      \seq_put_right:Nx\l__guissmo_aligncol_temp_seq
       {
        \seq_item:Nn \l__guissmo_aligncol_in_seq { ##1+\l__guissmo_aligncol_rows_int*####1 }
       }
     }
    \seq_put_right:Nx\l__guissmo_aligncol_out_seq
     {
      \seq_use:Nn\l__guissmo_aligncol_temp_seq{&}
     }
   }
  % now \l__guissmo_aligncol_out_seq contains the new rows
  \begin{equation}\eqnum
  \begin{minipage}{0.9\textwidth}
  \setlength{\abovedisplayskip}{0pt}
  \setlength{\belowdisplayskip}{0pt}
  \setlength{\abovedisplayshortskip}{0pt}
  \setlength{\belowdisplayshortskip}{0pt}
  \sbox0{T} % tallest letter of the first line of other minipage
  \vskip-\ht0
  \begin{align*}
  \seq_use:Nn\l__guissmo_aligncol_out_seq { \\ }
  \end{align*}
  \end{minipage}
  \end{equation}
 }
\ExplSyntaxOff

\begin{document}

\noindent
Hello!
\begin{aligncol}{1}
a &= a
\end{aligncol}
This has no extra space at the beginning.

\end{document}

在此处输入图片描述

相关内容