2

I like the nice yellow margins (top and bottom) and spacings from the yellow tcolorbox in "Subsection 2". Unfortunately I get this margins and spacing only directly after a \section or \subsection. Problems: a) Without a \section or \subsection in front of a tcolorbox the top margin is greater b) I lose the white spacing between two tcolorboxes in some cases, see my examples. Especially the grey tcolorboxes have a white spacing between, but yellow ones do not.

How to get the same nice margins and spacings in any case? Of course there is no problem with additional white spacings after/ before a headings like \section or \subsection to respect the headings.

\documentclass[a4paper]{scrbook}

\usepackage{xparse}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,xparse}

\NewDocumentEnvironment{Material}{o}% recommend using xparse's arg-type o
    {% begin-Befehle
        \begin{tcolorbox}[%
            parbox=false,% wenn dieser Befehl deaktiviert ist, dann sollte unten setlength{\parskip}{0.5\baselineskip plus2pt minus2pt} aktiviert sein.
            enhanced jigsaw,%
            breakable,% auskommentieren, wenn die Aufgaben in einer einzigen Box zusammenhängen sollen
            grow to left by=2mm,%
            left*=0mm,%
            grow to right by=2mm,%
            right*=0mm,%
            boxrule=0pt,% keine Rahmenlinie
            arc=0mm,% keinen Rundung in den Ecken
            colback=gray!30,% Hintergrundfarbe im Body
            %beforeafter skip balanced=0pt,% kein Abstand davor und dahinter
            ]%
        \subsubsection{Material}
        \IfValueTF{#1}
            {\begin{itemize}[#1]}% optional #1 is given by user
            {\begin{itemize}}% optional #1 has no value
    }%
    {% end-Befehle
        \end{itemize}%
        \end{tcolorbox}%
    }%
    
\NewDocumentEnvironment{Content}{o}% recommend using xparse's arg-type o
    {% begin-Befehle
        \begin{tcolorbox}[%
            parbox=false,% wenn dieser Befehl deaktiviert ist, dann sollte unten setlength{\parskip}{0.5\baselineskip plus2pt minus2pt} aktiviert sein.
            enhanced jigsaw,%
            breakable,% auskommentieren, wenn die Aufgaben in einer einzigen Box zusammenhängen sollen
            grow to left by=2mm,%
            left*=0mm,%
            grow to right by=2mm,%
            right*=0mm,%
            boxrule=0pt,% keine Rahmenlinie
            arc=0mm,% keinen Rundung in den Ecken
            colback=yellow!30,% Hintergrundfarbe im Body
            beforeafter skip balanced=0pt,% kein Abstand davor und dahinter
            ]%
        \subsubsection{Content}
        \IfValueTF{#1}
            {\begin{itemize}[#1]}% optional #1 is given by user
            {\begin{itemize}}% optional #1 has no value
    }%
    {% end-Befehle
        \end{itemize}%
        \end{tcolorbox}%
    }%

\begin{document}

\begin{Content}
\item Content 1
\end{Content}

\begin{Content}
\item Content 1
\end{Content}

\begin{Material}
\item Material 1
\end{Material}

\begin{Material}
\item Material 1
\end{Material}

\subsection{Subsection 1}

\begin{Content}
\item Content 1
\end{Content}

\begin{Content}
\item Content 1
\end{Content}

\begin{Material}
\item Material 1
\end{Material}


\begin{Material}
\item Material 1
\end{Material}

\end{document}

enter image description here

1 Answer 1

3
+50

For problem a), leave out the space before \subsubsection with: \RedeclareSectionCommand[beforeskip=0pt]{subsubsection}.

For problem b), comment out the line beforeafter skip balanced=0pt,. To avoid affecting the rest of your document, do it only before the subsections in your boxes and then restore the original value of the class.

I also added frame empty, to avoid the little frame rules still visible with boxrule=0pt.

\documentclass[a4paper]{scrbook}
\usepackage{xparse}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,xparse}

\NewDocumentEnvironment{Material}{o}% recommend using xparse's arg-type o
    {% begin-Befehle
        \begin{tcolorbox}[%
            parbox=false,% wenn dieser Befehl deaktiviert ist, dann sollte unten setlength{\parskip}{0.5\baselineskip plus2pt minus2pt} aktiviert sein.
            enhanced jigsaw,%
            breakable,% auskommentieren, wenn die Aufgaben in einer einzigen Box zusammenhängen sollen
            grow to left by=2mm,%
            left*=0mm,%
            grow to right by=2mm,%
            right*=0mm,%
            boxrule=0pt,% keine Rahmenlinie
            frame empty,% to avoid the little frame rules still visible
            arc=0mm,% keinen Rundung in den Ecken
            colback=gray!30,% Hintergrundfarbe im Body
            %beforeafter skip balanced=0pt,% kein Abstand davor und dahinter
            ]%
        \let\savedsubsubsection\subsubsection%
        \RedeclareSectionCommand[beforeskip=0pt]{subsubsection}%
        \subsubsection{Material}
        \let\subsubsection\savedsubsubsection%
        \IfValueTF{#1}
            {\begin{itemize}[#1]}% optional #1 is given by user
            {\begin{itemize}}% optional #1 has no value
    }%
    {% end-Befehle
        \end{itemize}%
        \end{tcolorbox}%
    }%
    
\NewDocumentEnvironment{Content}{o}% recommend using xparse's arg-type o
    {% begin-Befehle
        \begin{tcolorbox}[%
            parbox=false,% wenn dieser Befehl deaktiviert ist, dann sollte unten setlength{\parskip}{0.5\baselineskip plus2pt minus2pt} aktiviert sein.
            enhanced jigsaw,%
            breakable,% auskommentieren, wenn die Aufgaben in einer einzigen Box zusammenhängen sollen
            grow to left by=2mm,%
            left*=0mm,%
            grow to right by=2mm,%
            right*=0mm,%
            boxrule=0pt,% keine Rahmenlinie
            frame empty,% to avoid the little frame rules still visible
            arc=0mm,% keinen Rundung in den Ecken
            colback=yellow!30,% Hintergrundfarbe im Body
            %beforeafter skip balanced=0pt,% kein Abstand davor und dahinter
            ]%
        \let\savedsubsubsection\subsubsection%
        \RedeclareSectionCommand[beforeskip=0pt]{subsubsection}%
        \subsubsection{Content}
        \let\subsubsection\savedsubsubsection%
        \IfValueTF{#1}
            {\begin{itemize}[#1]}% optional #1 is given by user
            {\begin{itemize}}% optional #1 has no value
    }%
    {% end-Befehle
        \end{itemize}%
        \end{tcolorbox}%
    }%

\begin{document}

\begin{Content}
\item Content 1
\end{Content}

\begin{Content}
\item Content 1
\end{Content}

\begin{Material}
\item Material 1
\end{Material}

\begin{Material}
\item Material 1
\end{Material}

\subsection{Subsection 1}

\begin{Content}
\item Content 1
\end{Content}

\begin{Content}
\item Content 1
\end{Content}

\begin{Material}
\item Material 1
\end{Material}


\begin{Material}
\item Material 1
\end{Material}

\subsubsection{Ordinary subsubsection}
Something something

\subsection{Another subsection}
\subsubsection{With a subsubsection}
Something something
\subsubsection{And a second subsubsection}
Something something
\subsubsection{And a third subsubsection}
Something something
\subsubsection{And another subsubsection}
Something something

\end{document}

enter image description here enter image description here

6
  • \RedeclareSectionCommand[beforeskip=0pt]{subsubsection} seems to be a global redeclaration with effects to the other parts of my text. Is there a way to focus the redeclaration only to the content of the tcolorboxes? I do not want to miss the normal space before a subsubsection.
    – Michael.H
    Oct 7, 2022 at 20:43
  • In general it should be better to save the actual value of beforeskip in a variable and then restore the value with the help of the variable. In my case I really use a subsection instead of a subsubsection and I do not know the default value to restore. How to save and restore the value of beforeskip?
    – Michael.H
    Oct 8, 2022 at 20:40
  • possible bug in your renewed Code: please look at the number of the part "sub" in your code in several places.
    – Michael.H
    Oct 18, 2022 at 14:39
  • I think \let\savedsubsubsection\subsection should be \let\savedsubsubsection\subsubsection. There is more than one error, even in your comment. Or maybe I didn´t understand the program code?
    – Michael.H
    Oct 18, 2022 at 17:20
  • @Michael.H You're right, I copied-pasted what I did without reading, I'll correct
    – CarLaTeX
    Oct 18, 2022 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .