微量元素

微量元素

這是一個後續問題控制 tcolorbox 頂部的縮排和垂直空間

@egreg 為原始問題提供了一個很好的解決方案,但是當某些環境(可能是基於 a 的任何環境trivlist)出現在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}

微波能量輸出

答案1

內部環境中缺少垂直空間center是因為\@item使用\addvspace\@topsep在之前留下空行時插入您想要的空間\begin{center},並且\addvspace在垂直模式下不會添加此空間並且\if@minipage是 true - 這是您的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}

您將在盒子頂部獲得額外的空間,如下所示:

在此輸入影像描述

相關內容