7

I'm trying to put a long text of two columns in a tcolorbox box with breakable option. The text comes out of the box, and LaTeX does not produce a page break!

This is my MWE :

\documentclass{article}
\usepackage{multicol,lipsum}
\usepackage[all]{tcolorbox}

\begin{document}

\begin{tcolorbox}[breakable]
\begin{multicols}{2}
\lipsum[1-20]
\end{multicols}
\end{tcolorbox}


\end{document}

The result :

enter image description here

Any advice ? Thank you

  • 1
    multicols inside tcolorbox is basically not possible (or very difficult, as far as I know, but the other way round works) – user31729 Mar 20 '18 at 4:49
  • Related: tex.stackexchange.com/questions/340191/… – Ignasi Mar 20 '18 at 8:59
  • Thank you Christian Hupfer and Ignasi. In the related link the title appears in all pages and the lower part of the box is not broken. tcolorbox is incompatible with multicol as I know. Another solution ? – Haouam Mar 20 '18 at 11:21
  • @Haouam: You can as well claim that multicol is not compatible to tcolorbox. Page breaking etc. is a difficult issue – user31729 Mar 21 '18 at 19:23
  • No solution then ? – Haouam Mar 22 '18 at 22:16
8
+100

I didn't nothing more than I copied the macros from OPmac \begmulti...\endmulti which implement multicolumn typesetting in plain TeX. And it works:

\documentclass{article}
\usepackage{multicol,lipsum}
\usepackage[all]{tcolorbox}

\newdimen\colsep \colsep=2em
\newcount\mullines
\newcount\tmpnum
\def\corrsize #1{%% #1 := #1 + \splittopskip - \topskip
   \advance #1 by \splittopskip \advance #1 by-\topskip
}
\def\begmulti #1 {\par\bgroup\penalty0 \def\Ncols{#1}
   \setbox6=\vbox\bgroup\penalty0
   %% \hsize := Sirka sloupce = (\hsize+\colsep) / n - \colsep
   \advance\hsize by\colsep
   \divide\hsize by\Ncols  \advance\hsize by-\colsep
   \mullines=0
   \def\par{\ifhmode\endgraf\global\advance\mullines by\prevgraf\fi}%
}
\def\endmulti{\vskip-\prevdepth\vfil
   \expandafter\egroup\expandafter\baselineskip\the\baselineskip\relax 
   \dimen0=.8\maxdimen \tmpnum=\dimen0 \divide\tmpnum by\baselineskip 
   \splittopskip=\baselineskip
   \setbox1=\vsplit6 to0pt
   %% \dimen1 := the free space on the page
   \vsize=.95\vsize
   \ifdim\pagegoal=\maxdimen \dimen1=\vsize \corrsize{\dimen1}
   \else \dimen1=\pagegoal \advance\dimen1 by-\pagetotal \fi
   \ifdim \dimen1<2\baselineskip
     \vfil\break \dimen1=\vsize \corrsize{\dimen1} \fi
   \ifnum\mullines<\tmpnum \dimen0=\ht6 \else \dimen0=.8\maxdimen \fi
   \divide\dimen0 by\Ncols \relax
   %% split the material to more pages?
   \ifdim \dimen0>\dimen1 \splitpart
   \else \balancecolumns \fi  % only balancing
   \egroup
}
\def\makecolumns{\bgroup % full page, destination height: \dimen1
   \vbadness=20000 \setbox1=\hbox{}\tmpnum=0
   \loop \ifnum\Ncols>\tmpnum
      \advance\tmpnum by1
      \setbox1=\hbox{\unhbox1 \vsplit6 to\dimen1 \hss}
   \repeat
   \hbox{}\nobreak\vskip-\splittopskip \nointerlineskip
   \null\nobreak\medskip\hbox to\hsize{\unhbox1\unskip}
   \dimen0=\dimen1 \divide\dimen0 by\baselineskip \multiply\dimen0 by\Ncols
   \global\advance\mullines by-\dimen0
   \egroup
}
\def\splitpart{%
   \makecolumns % full page
   \vskip 0pt plus 1fil minus\baselineskip \break
   \ifnum\mullines<\tmpnum \dimen0=\ht6 \else \dimen0=.8\maxdimen \fi
   \divide\dimen0 by\Ncols \relax
   \ifx\balancecolumns\flushcolumns \advance\dimen0 by-.5\vsize \fi
   \dimen1=\vsize \corrsize{\dimen1}\dimen2=\dimen1
   \advance\dimen2 by-\Ncols\baselineskip
   %% split the material to more pages?
   \ifvoid6 \else
      \ifdim \dimen0>\dimen2 \expandafter\expandafter\expandafter \splitpart
      \else \balancecolumns % last balancing
   \fi \fi
}
\def\balancecolumns{\bgroup \setbox7=\copy6 % destination height: \dimen0
   \ifdim\dimen0>\baselineskip \else \dimen0=\baselineskip \fi
   \vbadness=20000
   \def\tmp{%
      \setbox1=\hbox{}\tmpnum=0
      \loop \ifnum\Ncols>\tmpnum
         \advance\tmpnum by1
         \setbox1=\hbox{\unhbox1
              \ifvoid6 \hbox to\wd6{\hss}\else \vsplit6 to\dimen0 \fi\hss}
      \repeat
   \ifvoid6 \else
      \advance \dimen0 by.2\baselineskip
      \setbox6=\copy7
      \expandafter \tmp \fi}\tmp
   \hbox{}\nobreak\vskip-\splittopskip \nointerlineskip
   \null\nobreak\medskip\hbox to\hsize{\unhbox1\unskip}%
   \egroup
}

\begin{document}

\begin{tcolorbox}[breakable]
%\begin{multicols}{2}
\begmulti 2
\lipsum[1-20]
%\end{multicols}
\endmulti
\end{tcolorbox}
  • Is there a way for the begmulti environment not to start on a newpage? – mac Aug 9 at 21:06
  • It works in middle of the page too (in plain TeX). – wipet Aug 21 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.