取消以下段落的缩进

取消以下段落的缩进

我制作了一个environment将一些示例文本格式化为一个方框,使其与书中的正文区分开。它变成了其他文本引用的一种图形。它周围有很多空白。由于有空白,后面的段落不需要缩进。我是否可以在这个环境定义中放置一个命令抑制缩进下一段,并且仅缩进下一段。

以下是环境的定义:

\newenvironment{urlstyle}
    {\begin{center}
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline\\
    \ttfamily
    }
    { 
    \\\\\hline
    \end{tabular} 
    \end{center}
    }

文档主体可能会像这样使用:

Sample text of a paragraph before the URL is mentioned in the code.

\begin{urlstyle}
/api/t=\{tenant\}/a=\{app\}/
\end{urlstyle}

Here is a description of what this URL does.  Because of the 
space around the URL in a box, I do not want this paragraph
to be indented.

But this later paragraph should be indented because it follows directly
after the preceding one, without a blank line and that needs
an indent.

当然,我可以将其放入\noindent下面的段落中,但我更希望将其内置到环境命令中,这样我就可以保证它在整本书中都是一致的,而不需要每次使用 URL 环境时都进行繁琐的检查。我尝试将其放在\noindent环境定义的末尾,但那行不通,因为它无法影响下面的段落。您必须将其放在\noindent您想要影响的段落中。

标题和副标题以这种方式工作得很好。不知何故,它们抑制了下一段(本节中的第一段)的缩进。是否有任何命令说:我希望将紧随此段之后的段落视为段落块中的第一段?

答案1

您只向我们提供了一小段代码。因此,我只能猜测可能会出现什么样的复杂情况。但是,下面我创建了一个 MWE,它对您使用的包没有做出任何特别的假设。(显然我必须假设要使用一个类。)

您将通过在此处留​​下空行来创建缩进。空行表示新段落。您可以删除空行,或者,如果在视觉上想要中断,请注释掉空行,如下所示:

\documentclass{article}

\newenvironment{urlstyle}
    {\begin{center}
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline\\
    \ttfamily
    }
    { 
    \\\\\hline
    \end{tabular} 
    \end{center}
    \aftergroup\ignorespaces
    }


\pagestyle{empty}
\begin{document}

Sample text of a paragraph before the URL is mentioned in the code.
%%
\begin{urlstyle}
/api/t=\{tenant\}/a=\{app\}/
\end{urlstyle}
%%
Here is a description of what this URL does.  Because of the 
space around the URL in a box, I do not want this paragraph
to be indented.

But this later paragraph should be indented because it follows directly
after the preceding one, without a blank line and that needs
an indent.

\end{document}

在此处输入图片描述

答案2

所有标题都调用\@afterheading来控制后面的缩进,但需要一些帮助才能将设置从环境中提取出来(因为所有标题命令当前都是命令而不是环境)。

在此处输入图片描述

\documentclass{article}

\makeatletter
\newenvironment{urlstyle}
    {\par
    \begin{center}%
    \begin{tabular}{|p{0.9\textwidth}|}%
    \hline\\%
    \ttfamily
    }
    { 
    \\\\\hline
    \end{tabular}%
    \end{center}%
    \par
    \everypar{}%
    \aftergroup\@nobreaktrue
    \aftergroup\global\aftergroup\@afterindentfalse
    \ignorespacesafterend
    \aftergroup\@afterheading
    }
%\makeatother

\pagestyle{empty}
\begin{document}

Sample text of a paragraph before the URL is mentioned in the code.

\begin{urlstyle}
/api/t=\{tenant\}/a=\{app\}/
\end{urlstyle}


Here is a description of what this URL does.  Because of the 
space around the URL in a box, I do not want this paragraph
to be indented.

But this later paragraph should be indented because it follows directly
after the preceding one, without a blank line and that needs
an indent.

\end{document}

相关内容