平均能量损失

平均能量损失

这是控制 tcolorbox 顶部的缩进和垂直空间

trivlist@egreg 为原始问题提供了一个很好的解决方案,但是当某些环境(可能是基于 的任何环境)出现在 中时,会产生一个副作用tcolorbox。第一个环境上方的间距丢失了。

我该如何调整@egreg 的答案以便这些环境间隔正确?

平均能量损失

\documentclass{article}

\usepackage{tcolorbox}

\tcbset{mybox/.style={colback=white, colframe=blue, left=2mm, right=2mm,
  fonttitle=\bfseries}, fontupper=\small,
  before upper=\setlength{\parindent}{1em}\everypar{{\setbox0\lastbox}\everypar{}},
}

\newtcolorbox{mybox}[1][]{mybox,#1}

\begin{document}
\begin{mybox}
  This line should not be indented.

  This line should be indented.

  \begin{center}
    This line should be centred. The spacing above is wrong.
  \end{center}

  This line should be indented.

  \begin{center}
    This line should be centred. The spacing above is right.
  \end{center}

  This line should be indented.
\end{mybox}

\begin{mybox}
  \section{Should not have gap above this heading}
\end{mybox}
\end{document}

MWE 输出

答案1

内部环境中缺少垂直空格center是因为\@item使用\addvspace\@topsep在 之前留空行时插入您想要的空格\begin{center},而\addvspace在垂直模式下不会添加此空格,并且\if@minipage为真—这是 内部的情况mybox。因此,为了插入此空格,您可以在 的选项\@minipagefalse中设置。before uppertcolorbox

两个框之间的垂直间隙本质上是由于 的默认值/tcb/noparskip(请参阅手册/tcb/autoparskip中的 文档tcolorbox)。如果您设置before=\par\noindent,则删除添加的\smallskip

\documentclass{article}
\usepackage{tcolorbox}

\makeatletter

\tcbset{mybox/.style={colback=white, colframe=blue, left=2mm, right=2mm,
  fonttitle=\bfseries}, fontupper=\small,
  before upper={\setlength{\parindent}{1em}%
                \everypar{{\setbox0\lastbox}\@minipagefalse\everypar{}}},
  before=\par\noindent, after=\par
}

\makeatother

\newtcolorbox{mybox}[1][]{mybox,#1}

\begin{document}
\begin{mybox}
  This line should not be indented.

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is correct.
  \end{center}

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is right.
  \end{center}

  This line should be indented.
\end{mybox}

\begin{mybox}
  \section{Should not have gap above this heading}
\end{mybox}
\end{document}

截屏

如果你真的想摆脱所有的差距,抑制此处附加到主垂直列表的两个感兴趣的框之间的所有垂直粘连。您可以使用 执行此操作\nointerlineskip,由于after=\par我们设置了,因此在垂直模式下可以正确使用:

\documentclass{article}
\usepackage{tcolorbox}

\makeatletter

\tcbset{mybox/.style={colback=white, colframe=blue, left=2mm, right=2mm,
  fonttitle=\bfseries}, fontupper=\small,
  before upper={\setlength{\parindent}{1em}%
                \everypar{{\setbox0\lastbox}\@minipagefalse\everypar{}}},
  before=\par\noindent, after=\par
}

\makeatother

\newtcolorbox{mybox}[1][]{mybox,#1}

\begin{document}
\begin{mybox}
  This line should not be indented.

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is correct.
  \end{center}

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is right.
  \end{center}

  This line should be indented.
\end{mybox}
\nointerlineskip
\begin{mybox}
  \section{Should not have gap above this heading}
\end{mybox}
\end{document}

在此处输入图片描述

注意:我将 设置\@minipagefalse在临时文件内\everypar,以便如果以环境开头,则框内不会有多余的垂直空间center。但是,如果您直接在 的开头设置它before upper,如下所示:

\tcbset{mybox/.style={colback=white, colframe=blue, left=2mm, right=2mm,
  fonttitle=\bfseries}, fontupper=\small,
  before upper={\@minipagefalse\setlength{\parindent}{1em}%
                \everypar{{\setbox0\lastbox}\everypar{}}},
  before=\par\noindent, after=\par
}

并使用以下环境启动你的盒子center

\begin{mybox}
  \begin{center}
    This line should be centered. The spacing above is correct.
  \end{center}

  This line should be indented.

  \begin{center}
    This line should be centered. The spacing above is correct.
  \end{center}
...
\end{mybox}

您将在盒子顶部获得额外的空间,如下所示:

在此处输入图片描述

相关内容