防止 tcolorbox 位於頁面底部

防止 tcolorbox 位於頁面底部

有沒有什麼方法可以防止 tcolorbox 位於頁面底部,而是自動將其推送到下一頁?

微量元素:

\documentclass[a4paper, 10pt, oneside, twocolumn]{memoir}

\usepackage{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{outcome}{before skip=2em, after skip=1em}

\begin{document}

\section{Sample}

\begin{outcome}
    This is some sample text. This is some sample text. This is some sample text. 
\end{outcome}

\lipsum[1-6]

This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. 

\begin{outcome}
    Sample text.
\end{outcome}

This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. 

\end{document}

在此輸入影像描述

答案1

嘗試這個:

\makeatletter
\newtcolorbox{outcome}{before skip=2em, after={%
    \tcb@parfillskip@check\par\ifvmode\nointerlineskip
    \nobreak
    \addvspace{\glueexpr 1em-\parskip}\fi
    }}
\makeatother

after skip/.style該代碼是從in的定義中逐字複製的tcolorbox.sty,並#1替換為您的硬編碼值1em,並添加一個\nobreak以將框與以下段落保持在一起。

答案2

這是我的舊答案的一種方法這裡:

\documentclass[a4paper, 10pt, oneside, twocolumn]{memoir}

\usepackage{tcolorbox}
\usepackage{lipsum}

\newsavebox{\mybottombox} % Box to save the text of the command 
\newlength{\mybottomlength} % The length of our text inside the command
\newlength{\availafter} % The available length left on the page after placing our text


% Optional argument is the minimum length after the nobottom text for not pagebreak. Change it to your needs
\newcommand{\nobottom}[2][60pt]{\savebox{\mybottombox}{\vbox{#2}}\setlength{\mybottomlength}{\ht\mybottombox}%
\setlength{\availafter}{\dimexpr\textheight-\mybottomlength-\pagetotal\relax}\ifdim\availafter<#1%
\pagebreak\noindent\usebox{\mybottombox}%
\else%
\noindent\usebox{\mybottombox}%
\fi%
}%

\newtcolorbox{outcome}{before skip=2em, after skip=1em}

\begin{document}

\section{Sample}

\begin{outcome}
    This is some sample text. This is some sample text. This is some sample text. 
\end{outcome}

\lipsum[1-6]

This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. 

\nobottom{%
\begin{outcome}
    Sample text.
\end{outcome}}

This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. This is some sample text. 

\end{document}

在此輸入影像描述

相關內容