在 LaTeX 中抑制环境后的缩进

发布于 2024-08-30 18:12:34 字数 1004 浏览 4 评论 0原文

我正在尝试在 LaTeX 文档中创建一个新环境,其中该环境后面的下一段中的缩进被抑制。

有人告诉我(TeXbook 和 LaTeX 源代码),通过将 \everypar 设置为 {\setbox0\lastbox},TeX 排字机将在下一段的开头执行此操作从而删除缩进:

\everypar{\setbox0\lastbox}

所以这就是我所做的,但没有效果(以下段落仍然缩进):

\newenvironment{example}
  {\begin{list}
     {}
     {\setlength\leftmargin{2em}}}
  {\end{list}\everypar{\setbox0\lastbox}}

我已经研究了 LaTeX 的内部结构以及我可以管理的内容。似乎 \end 例程在某个时刻显示 \endgroup\par,这可能是 LaTeX 忽略我的 \ 的原因everypar 设置。 \global 也没有帮助。我知道 \noindent 但想自动执行此操作。

示例文档片段:

This is paragraph text. This is paragraph text, too.

\begin{example}
  \item This is the first item in the list.
  \item This is the second item in the list.
\end{example}

This is more paragraph text. I don't want this indented, please.

感兴趣的内部例程和开关似乎是 \@endpetrue\@endparenv 等。感谢您的帮助。

I'm trying to create a new environment in my LaTeX document where indentation in the next paragraph following the environment is suppressed.

I have been told (TeXbook and LaTeX source) that by setting \everypar to {\setbox0\lastbox}, the TeX typesetter will execute this at the beginning of the next paragraph and thus remove the indentation:

\everypar{\setbox0\lastbox}

So this is what I do, but to no effect (following paragraph is still indented):

\newenvironment{example}
  {\begin{list}
     {}
     {\setlength\leftmargin{2em}}}
  {\end{list}\everypar{\setbox0\lastbox}}

I have studied LaTeX's internals as well as I could manage. It seems that the \end routine says \endgroup and \par at some point, which may be the reason LaTeX ignores my \everypar setting. \global doesn't help either. I know about \noindent but want to do this automatically.

Example document fragment:

This is paragraph text. This is paragraph text, too.

\begin{example}
  \item This is the first item in the list.
  \item This is the second item in the list.
\end{example}

This is more paragraph text. I don't want this indented, please.

Internal routines and switches of interest seem to be \@endpetrue, \@endparenv and others. Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

撩动你心 2024-09-06 18:12:34

如果不重新定义 \end ,我就无法让任何东西发挥作用,但我当然不是专家。

以下内容相当hacky,但在我有限的测试中有效。当然,这会干扰嵌套环境(如果遇到问题,您应该能够重新定义 \begin 以恢复旧的 \end)。

\newenvironment{example}{%
  \bgroup
  \let\oldend=\end
  \def\end##1{\oldend{##1}\csname @afterindentfalse\endcsname
                          \csname @afterheading\endcsname}
  \begin{list}{}
    {\setlength\leftmargin{2em}}
  }{%
  \end{list}
  \egroup
}

I couldn't get anything to work without redefining \end, but I'm certainly no expert.

The following is quite hacky, but worked in my limited testing. Of course this will interfere with nested environments (you should be able to redefine \begin to restore the old \end if you have problems).

\newenvironment{example}{%
  \bgroup
  \let\oldend=\end
  \def\end##1{\oldend{##1}\csname @afterindentfalse\endcsname
                          \csname @afterheading\endcsname}
  \begin{list}{}
    {\setlength\leftmargin{2em}}
  }{%
  \end{list}
  \egroup
}
避讳 2024-09-06 18:12:34

难道你不能通过在你的环境和下一行之间不留空行来避免这种情况吗?

This is paragraph text. This is paragraph text, too.

\begin{example}
  \item This is the first item in the list.
  \item This is the second item in the list.
\end{example}
% (No blank line)
This is more paragraph text. I don't want this indented, please.

Can't you avoid this by not having a blank line between your environment and the next line?

This is paragraph text. This is paragraph text, too.

\begin{example}
  \item This is the first item in the list.
  \item This is the second item in the list.
\end{example}
% (No blank line)
This is more paragraph text. I don't want this indented, please.
夏日浅笑〃 2024-09-06 18:12:34

像这样简单的事情对我有用:

\makeatletter
\newenvironment{example}{%
  \bgroup
    \list{}{}
}{%
    \endlist
    \@afterindentfalse
    \@afterheading
  \egroup
}
\makeatother

但是,在调用第一个 \section (或 \chapter,在类“book”和“report”的情况下)之前它不起作用。我不知道为什么。

Something as simple as this works for me:

\makeatletter
\newenvironment{example}{%
  \bgroup
    \list{}{}
}{%
    \endlist
    \@afterindentfalse
    \@afterheading
  \egroup
}
\makeatother

But, it doesn't work before the first \section (or \chapter, in the case of classes "book" and "report") is called. I don't know why.

素年丶 2024-09-06 18:12:34

我尝试了伊万的答案,但它对我不起作用。但我确实让它工作了!这就是我所做的:

\makeatletter
\renewenvironment{quotation}{% 
\bgroup%
\let\oldend=\end%
\def\end##1{\oldend{##1}\csname @afterindentfalse\endcsname%
                        \csname @afterheading\endcsname}%
\list{}{\listparindent 1.5em%
\itemindent    \listparindent%
\leftmargin 1.5em%               This controls the size of the indentation
\rightmargin   \leftmargin
\parsep        \z@ \@plus\p@}%      This line reduces inter-paragraph space to normal values.
\item\relax%
}{%
\endlist%%
\egroup%
}
\makeatother

这样做的优点是它可以很好地排版您的块引用,并删除块引用后面段落的缩进。

I tried the Ivan's answer, but it wasn't working for me. But I did get it working! Here's what I did:

\makeatletter
\renewenvironment{quotation}{% 
\bgroup%
\let\oldend=\end%
\def\end##1{\oldend{##1}\csname @afterindentfalse\endcsname%
                        \csname @afterheading\endcsname}%
\list{}{\listparindent 1.5em%
\itemindent    \listparindent%
\leftmargin 1.5em%               This controls the size of the indentation
\rightmargin   \leftmargin
\parsep        \z@ \@plus\p@}%      This line reduces inter-paragraph space to normal values.
\item\relax%
}{%
\endlist%%
\egroup%
}
\makeatother

The advantage to this is that it typesets your blockquotes very nicely, and removes the indentation from paragraph after the blockquote.

掀纱窥君容 2024-09-06 18:12:34

您无需重新定义 \end 即可执行此操作。

\makeatletter
\newenvironment{example}
   {\begin{list}
      {}
      {\setlength\leftmargin{2em}}}
   {\end{list}%
    \def\if@endpe{%
      \@doendpe
      \let\par\@@par
      \iffalse}}
\makeatother

说明

\end 更改 \everypar after 扩展 \endexample< /代码>。为了使事情变得更加复杂,它设置 \par 来恢复 \everypar{}。显然 \@doendpe 是为了确保如果段落在环境后面继续,则没有缩进,但如果有 \par (或空),则恢复正常行为线)在环境之后。

您可能希望避免更改 \end 因为它必须在环境开始时更改,因此可能会干扰嵌套环境。幸运的是,\end 的定义包含\expandafter\endgroup\if@endpe。我们可以使用 \if@endpe 作为钩子将代码注入外部作用域。 \endgroup之后\if@endpe自动恢复。

You can do this without redefining \end

\makeatletter
\newenvironment{example}
   {\begin{list}
      {}
      {\setlength\leftmargin{2em}}}
   {\end{list}%
    \def\if@endpe{%
      \@doendpe
      \let\par\@@par
      \iffalse}}
\makeatother

Explanation

\end changes \everypar after expanding \endexample. To make things even more complicated it sets \par to restore \everypar{}. Appearently \@doendpe is ment to make sure that there is no indentation if the paragraph continues after the environment, but to restore normal behavior if there is a \par (or empty line) after the environment.

You may want to avoid changing \end because it would have to be changed at the begining of the environment and may therefore disturb nested environments. Luckily the definition of \end contains \expandafter\endgroup\if@endpe. We can use \if@endpe as a hook to inject our code to the outer scope. After the \endgroup \if@endpe is automatically restored.

你爱我像她 2024-09-06 18:12:34

在定义末尾包含 \@afterindentfalse\@afterheading。

Include \@afterindentfalse\@afterheading at the end of your definition.

虚拟世界 2024-09-06 18:12:34

我也有同样的问题。我刚刚用过这个:

\noindent \newenvironment

I had the same problem. I just used this:

\noindent \newenvironment
带刺的爱情 2024-09-06 18:12:34

你不应该弄乱 \everypar 令牌列表,除非你确切地知道你在做什么。用于

\setlength{\parindent}{0pt}

消除整个文档中的缩进。

You should not mess with the \everypar token list, unless you know exactly what you are doing. Use

\setlength{\parindent}{0pt}

to get rid of indenting in the whole document.

寄意 2024-09-06 18:12:34

以 \noindent 结束你的环境可以帮助你

ending your environment with \noindent could help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文