简体   繁体   English

在LaTeX环境中抑制缩进

[英]Suppress indentation after environment in LaTeX

I'm trying to create a new environment in my LaTeX document where indentation in the next paragraph following the environment is suppressed. 我正在尝试在我的LaTeX文档中创建一个新环境,其中抑制了环境之后的下一段中的缩进。

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: 我被告知(TeXbook和LaTeX源码)通过将\\everypar设置为{\\setbox0\\lastbox} ,TeX排版器将在下一段的开头执行此操作,从而删除缩进:

\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. 我研究过LaTeX的内部结构以及我可以管理的内容。 It seems that the \\end routine says \\endgroup and \\par at some point, which may be the reason LaTeX ignores my \\everypar setting. 似乎\\end例程在某个时刻表示\\endgroup\\par ,这可能是LaTeX忽略my \\everypar设置的原因。 \\global doesn't help either. \\global也没有帮助。 I know about \\noindent but want to do this automatically. 我知道\\noindent但是想要自动执行此操作。

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. 感兴趣的内部例程和开关似乎是\\@endpetrue\\@endparenv和其他人。 Thanks for your help. 谢谢你的帮助。

I couldn't get anything to work without redefining \\end , but I'm certainly no expert. 没有重新定义\\end ,我无法得到任何工作,但我当然不是专家。

The following is quite hacky, but worked in my limited testing. 以下是非常hacky,但在我的有限测试中工作。 Of course this will interfere with nested environments (you should be able to redefine \\begin to restore the old \\end if you have problems). 当然这会干扰嵌套环境(如果遇到问题,你应该可以重新定义\\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
}

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. 但是,它在调用第一个\\ section之前不起作用(或者在“book”和“report”类的情况下为\\ chapter)。 I don't know why. 我不知道为什么。

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.

You can do this without redefining \\end 您可以在不重新定义\\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 . \\end的变化\\everypar扩大 \\endexample To make things even more complicated it sets \\par to restore \\everypar{} . 为了使事情变得更复杂,它设置\\par来恢复\\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. 显然\\@doendpe是确保如果段落在环境之后继续没有缩进,但是如果在环境之后存在\\par (或空行)则恢复正常行为。

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. 您可能希望避免更改\\end因为它必须在环境开始时进行更改,因此可能会干扰嵌套环境。 Luckily the definition of \\end contains \\expandafter\\endgroup\\if@endpe . 幸运的是, \\end的定义包含\\expandafter\\endgroup\\if@endpe We can use \\if@endpe as a hook to inject our code to the outer scope. 我们可以使用\\if@endpe作为钩子将我们的代码注入外部作用域。 After the \\endgroup \\if@endpe is automatically restored. \\endgroup \\if@endpe之后, \\if@endpe自动恢复。

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. 这样做的好处是它可以很好地排版你的blockquotes,并在blockquote之后删除段落中的缩进。

在定义的末尾加上\\ @afterindentfalse \\ @afterheading。

I had the same problem. 我有同样的问题。 I just used this: 我刚用过这个:

\noindent \newenvironment

You should not mess with the \\everypar token list, unless you know exactly what you are doing. 你不应该弄乱\\everypar令牌列表,除非你确切知道你在做什么。 Use 使用

\setlength{\parindent}{0pt}

to get rid of indenting in the whole document. 摆脱整个文件中的缩进。

使用\\ noindent结束您的环境可以帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM