I would like some adjacent tcolorbox
es to be connected, but not by using the option beforeafter skip=0pt
, because I also want those boxes to have normal spacing from other text. That is, I expect the boxes as in this picture:
automatically changing to the looking in the picture below:
How can one achieve this?
Below is a MWE to play with.
\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{blindtext}
\newtcolorbox{definition}{enhanced jigsaw,pad at break*=1mm,breakable,
left=4mm,right=4mm,top=1mm,bottom=1mm,
% beforeafter skip balanced=0pt,
colback=orange!10,boxrule=0pt,frame hidden,
borderline west={1.5mm}{-1mm}{green!50!black},arc=.7mm}
\begin{document}
\begin{definition}
...
\end{definition}
\begin{definition}
\blindtext
\end{definition}
\end{document}
Old:
I have written the following (very ugly) code:
\documentclass{article}
\usepackage[many]{tcolorbox}
% \newenvironment{testbox}
% {\begin{tcolorbox}
% [enhanced jigsaw,pad at break*=1mm,breakable,
% colback=orange!10!white,boxrule=0pt,frame hidden,
% borderline west={1.5mm}{-1mm}{green}]}
% {\end{tcolorbox}}
\usepackage[user,savepos]{zref}
\usepackage{xifthen}
\newlength{\deftop}
\newlength{\defbot}
\newlength{\defsep}
\newcounter{def}
\newenvironment{testbox}
{\vspace{-1mm}\begin{tcolorbox}[blank,breakable]
\stepcounter{def}
\zsavepos{def\arabic{def}1}
\setlength{\global\deftop}{
\dimexpr\paperheight - \zposy{def\arabic{def}1} sp}
\setlength{\global\defsep}{\deftop-\defbot}
\ifthenelse{\lengthtest{\defsep > -0.1mm}}
{\ifthenelse{\lengthtest{\defsep < 3mm}}{\vspace{-4mm}}{}}
{}\par
% This should be a dynamical quantity,
% adjusting according to the page's situation
\begin{tcolorbox}
[enhanced jigsaw,pad at break*=1mm,breakable,
colback=orange!10!white,boxrule=0pt,frame hidden,
borderline west={1.5mm}{-1mm}{green}]}
{\end{tcolorbox}
\zsavepos{def\arabic{def}2}
\setlength{\global\defbot}{
\dimexpr\paperheight - \zposy{def\arabic{def}2} sp}
\end{tcolorbox}\vspace{-1mm}}
\begin{document}
Some texts.
\begin{testbox}box 1\end{testbox}
Some texts.
\begin{testbox}box 2\end{testbox}%\vspace{-4mm}
\begin{testbox}box 3\end{testbox}
\end{document}
It uses zref
to record the position of each box so as to check whether two boxes are adjacent. However this solution does not always work. This code doesn't check if two boxes are on the same page (I did write a version that tries to check the page number issue, but since page number checking in LaTeX doesn't work pretty well on the edge of the page, I disgarded it). Also, the 4mm
is not a good idea because sometimes when a page is very loose, \vspace{4mm}
is not enough to connect them.
I wish to know if you have a better and prettier way to achieve this effect.
box2\\ box3
could work.definition
environment. When there're adjacent definitions I want them to be connected. If put in one box, the appearance of those definitions would be different.