這是一個後續問題控制 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 upper
tcolorbox
兩個框之間的垂直間隙本質上是由於預設值造成的/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}
您將在盒子頂部獲得額外的空間,如下所示: