為什麼在 tcolorbox 中跳過之前會被忽略

為什麼在 tcolorbox 中跳過之前會被忽略

我有這個 MWEtcolorbox

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}

\usepackage{blindtext}

\newcommand{\blsn}[2]{
    \begin{tcolorbox}[
        sharp corners, breakable, frame hidden, enhanced, boxrule=0pt, before skip=30pt, after skip=30pt,
        borderline west={3pt}{0pt}{red},
        segmentation style={red, line width=0.75pt, solid},
        colback=red!10,
        ]
        \textbf{Situation} \textit{(#1)}\\[2mm]
        #2
    \end{tcolorbox}
}

\newcommand{\secc}[1]{\noindent\parbox{\textwidth}{\section{#1}}}

\begin{document}
\blindtext
\blsn{arg1}{arg2}
\blindtext

\section{Skip before ignored}
\blsn{arg1}{arg2}
\blindtext
\end{document}

第一個按其應有的方式工作。但為什麼skip before在第二個中被忽略呢?

第一個盒子沒問題: 好的框

第二個盒子不是: 不好盒子

答案1

tcolorbox.sty第 304 行中,我們有:

before skip balanced/.style={before={%
    \ifnum\lastnodetype=-1\relax%
    \else%
      \par%
      \ifvmode%
        \iftcb@minipage%
          \ifdim\parskip>\z@\relax%
            \addvspace{-\parskip}%
          \fi%
        \else%
          \ifdim\prevdepth<\z@\relax%
            \addvspace{\glueexpr#1-\parskip}%
          \else%
            \ifdim\prevdepth>.3\baselineskip\relax%
              \addvspace{\glueexpr#1-\parskip}%
            \else%
              \addvspace{\glueexpr#1+.3\baselineskip-\prevdepth-\parskip}%
            \fi%
          \fi%
        \fi%
        \nointerlineskip%
      \fi%
    \fi%
    \lineskip\z@skip%
    \noindent%
  }},

我並不聲稱了解所有內容,但我看到了垂直模式的測試。

\leavevmode\par命令中,我們在頁面開頭以及命令之後\bsn獲取此信息before skip\section

\documentclass{article}
%https://tex.stackexchange.com/questions/705470/why-is-before-skip-ignored-in-a-tcolorbox
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}

\usepackage{blindtext}
\usepackage{showframe}%<-- comment in the final document
\newcommand{\blsn}[2]{
    \leavevmode\par%<-- line added
    \begin{tcolorbox}[
        sharp corners, breakable, frame hidden, enhanced, boxrule=0pt, before skip=30pt, after skip=30pt,
        borderline west={3pt}{0pt}{red},
        segmentation style={red, line width=0.75pt, solid},
        colback=red!10,
        ]
        \textbf{Situation} \textit{(#1)}\\[2mm]
        #2
    \end{tcolorbox}
}

\begin{document}
\blsn{arg1}{arg2}
\blindtext

\section{Skip no more ignored}
\blsn{arg1}{arg2}
\blindtext
\end{document}

在此輸入影像描述

答案2

好吧,我找到了問題的答案(儘管我不明白)。如果我使用這個指令就會出現問題

\newcommand{\secc}[1]{\noindent\parbox{\textwidth}{\section{#1}}}

代替\section

相關內容