4

I want to make a document with exercises and solutions in a breakable box like this picture:

enter image description here

Here is a minimal example which reproduces my problem :

\documentclass[12pt,a4paper,twoside,openright]{book}
\usepackage[utf8]{inputenc}
\usepackage[frenchb]{babel}
\usepackage[most]{tcolorbox}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage{geometry}
\usepackage{etoolbox}
\definecolor{tcbcolback}{RGB}{255,128,0}

 \usepackage{varwidth}
\newtcolorbox{mybox}[2][]{enhanced,
    before skip=2mm,after skip=2mm,
    colback=black!5,colframe=black!50,boxrule=0.2mm,
    attach boxed title to top left={xshift=1cm,yshift*=1mm-\tcboxedtitleheight},
    varwidth boxed title*=-3cm,boxrule=1pt,breakable,colframe=green!75!black,drop lifted shadow,
    boxed title style={frame code={
            \path[fill=tcbcolback!60!black]
            ([yshift=-1mm,xshift=-1mm]frame.north west)
            arc[start angle=0,end angle=180,radius=1mm]
            ([yshift=-1mm,xshift=1mm]frame.north east)
            arc[start angle=180,end angle=0,radius=1mm];
            \path[left color=tcbcolback!90!black,right color=tcbcolback!90!black,
            middle color=tcbcolback!99!black]
            ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
            [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
            -- (frame.south east) -- (frame.south west)
            -- ([xshift=-1mm,yshift=-1mm]frame.north west)
            [sharp corners]-- cycle;
        },interior engine=empty,
    },
    fonttitle=\bfseries,
    title={#2},#1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NewTColorBox[auto counter,number within=chapter]{exercise}{m+O{}}{%
    enhanced,
    colframe=green!20!black,
    colback=yellow!10!white,
    coltitle=green!40!black,
    fonttitle=\bfseries,
    underlay={\begin{tcbclipinterior}
            \shade[inner color=green!80!yellow,outer color=yellow!10!white]
            (interior.north west) circle (2cm);
            \draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
            (interior.south west) grid (interior.north east);
    \end{tcbclipinterior}},
    title={Exercise~ \thetcbcounter:},
    label={exercise:#1},
    attach title to upper=\quad,
    after upper={\par\hfill\textcolor{green!40!black}%
        {\itshape Solution on page~\pageref{solution:#1}}},
    lowerbox=ignored,
    savelowerto=solutions/exercise-\thetcbcounter.tex,
    record={\string\solution{#1}{solutions/exercise-\thetcbcounter.tex}},
    #2
}

\NewTotalTColorBox{\solution}{mm}{%
    enhanced,
    colframe=red!20!black,
    colback=yellow!10!white,
    coltitle=red!40!black,
    fonttitle=\bfseries,
    underlay={\begin{tcbclipinterior}
            \shade[inner color=red!50!yellow,outer color=yellow!10!white]
            (interior.north west) circle (2cm);
            \draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
            (interior.south west) grid (interior.north east);
    \end{tcbclipinterior}},
    title={Solution of Exercise~\ref{exercise:#1} on page~\pageref{exercise:#1}:},
    phantomlabel={solution:#1},
    attach title to upper=\par,
}{\input{#2}}

\tcbset{no solution/.style={no recording,after upper=}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{mybox}[]{Exercises }
\begin{exercise}{Ex1}[coltitle=cyan!80!black]
        \lipsum[1-6]
\end{exercise}  
\end{mybox}
\end{document}
moumou
  • 63
  • 8
    Welcome to TeX.se. You've tagged the question tcolorbox which seems to imply that you know the right package to make such boxes. Can you please edit your question to show a compilable example and explain exactly what you're having difficulty with. – Alan Munn May 31 '19 at 17:34
  • Read the tcolorbox manual on page 133. – Display Name Jun 01 '19 at 08:26
  • I want to make a document with clickable corrected exercises like the image shown above. the problem is that boxes are not breakable when the text of the exercise is long it appears on the footer. – moumou Jun 01 '19 at 08:26
  • From the tcolorbox documentation, Section 18.2 Limitations and Known Bugs: "You can nest an unbreakable tcolorbox inside another tcolorbox, even inside a breakable one. But you cannot not nest a breakable box inside a breakable box. The /tcb/breakable key for a nested box is ignored automatically, i. e. inner boxes are always unbreakable. After all, in the unlikely case you really want to have the nested box to be breakable, use /tcb/enforce breakable for the nested box4. But, a breakable box inside a breakable box will usually give a mess." – CarLaTeX Jun 02 '19 at 08:08
  • This answer offers an alternative to nest breakable tcolorboxes. It consists in building inner boxes with a superposed external box which mimics two level nested boxes. Hope it helps. – Ignasi Jun 03 '19 at 07:59

1 Answers1

2

I think that I find the solution (I'm not sure that this solution works for all the shots), just add : "enforce breakable,pad at break=0mm,break at=-20pt" in the code of the inner box and everything goes in the right order, this is a minimal example :

\documentclass[12pt,a4paper,oneside,openright]{book}
\usepackage[utf8]{inputenc}
\usepackage[frenchb]{babel}
\usepackage[most]{tcolorbox}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage{geometry}

\usepackage{etoolbox}
\definecolor{tcbcolback}{RGB}{255,128,0}

 \usepackage{varwidth}
\newtcolorbox{mybox}[2][]{enhanced,
    before skip=0mm,after skip=0mm,
    colback=black!5,colframe=black!50,boxrule=0.2mm,
    attach boxed title to top left={xshift=1cm,yshift*=1mm-\tcboxedtitleheight},
    varwidth boxed title*=-3cm,boxrule=1pt,breakable,pad at break*=0mm,colframe=green!75!black,drop lifted shadow,
    boxed title style={frame code={
            \path[fill=tcbcolback!60!black]
            ([yshift=-1mm,xshift=-1mm]frame.north west)
            arc[start angle=0,end angle=180,radius=1mm]
            ([yshift=-1mm,xshift=1mm]frame.north east)
            arc[start angle=180,end angle=0,radius=1mm];
            \path[left color=tcbcolback!90!black,right color=tcbcolback!90!black,
            middle color=tcbcolback!99!black]
            ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
            [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
            -- (frame.south east) -- (frame.south west)
            -- ([xshift=-1mm,yshift=-1mm]frame.north west)
            [sharp corners]-- cycle;
        },interior engine=empty,
    },
    fonttitle=\bfseries,
    title={#2},#1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NewTColorBox[auto counter,number within=chapter]{exercise}{m+O{}}{%
    enhanced jigsaw,enforce breakable,pad at break=0mm,break at=-20pt,
    colframe=green!20!black,
    colback=yellow!10!white,
    coltitle=green!40!black,
    fonttitle=\bfseries,
    underlay={\begin{tcbclipinterior}
            \shade[inner color=green!80!yellow,outer color=yellow!10!white]
            (interior.north west) circle (2cm);
            \draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
            (interior.south west) grid (interior.north east);
    \end{tcbclipinterior}},
    title={Exercise~ \thetcbcounter:},
    label={exercise:#1},
    attach title to upper=\quad,
    after upper={\par\hfill\textcolor{green!40!black}%
        {\itshape Solution on page~\pageref{solution:#1}}},
    lowerbox=ignored,
    savelowerto=solutions/exercise-\thetcbcounter.tex,
    record={\string\solution{#1}{solutions/exercise-\thetcbcounter.tex}},
    #2
}

\NewTotalTColorBox{\solution}{mm}{%
    enhanced,
    colframe=red!20!black,
    colback=yellow!10!white,
    coltitle=red!40!black,
    fonttitle=\bfseries,
    underlay={\begin{tcbclipinterior}
            \shade[inner color=red!50!yellow,outer color=yellow!10!white]
            (interior.north west) circle (2cm);
            \draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
            (interior.south west) grid (interior.north east);
    \end{tcbclipinterior}},
    title={Solution of Exercise~\ref{exercise:#1} on page~\pageref{exercise:#1}:},
    phantomlabel={solution:#1},
    attach title to upper=\par,
}{\input{#2}}

\tcbset{no solution/.style={no recording,after upper=}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{mybox}[]{Exercises }
\begin{exercise}{Ex1}[coltitle=cyan!80!black]
        \lipsum[1-6]
\end{exercise}  
\end{mybox}
\section{sec}
\lipsum[1-6]
\end{document}
moumou
  • 63