为什么在 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

相关内容