# Page headings in between sections of a tcolorbox

I've set up my LaTeX document to repeat the current chapter heading on every left hand page, and place whitespace on the right hand page for alignment reasons. To do this I have used EveryShipout.

I have sections of my document that I need to put in boxes, with a dashed border around the edge. Sometimes these boxes can be very long, spanning multiple pages. When this happens, my headings don't get inserted. I have a macro called \custompageheader that I can use to display my header at any time, and I'd like for a way to call this macro every time my breakable box breaks. Additionally, I need the positioning of the header to be consistent with the rest of my document, so the header must appear outside of the box.

Current behaviour:

Is it possible to achieve this? I've looked through the tcolorbox documentation and seen several potentially helpful items, but none of them have quite done the job.

• /tcb/underlay would render my title within the tcolorbox

• /tcb/overlay has the same issue

• I think /tcb/finish have the same issue

• /tcb/before has the same issue

• /tcb/extras possibly, but I can't see to output text from within an extra

• /tcb/frame code pre seems promising, but I can't get it to work

My code is below:

% IMPORTANT: To build, run XeLaTeX twice!
\documentclass[twoside]{memoir}
\usepackage[papersize={8.5in,11in}, vmargin=0.5in, outer=1in, inner=0.5in, includehead, includefoot]{geometry}
\usepackage{color}
\usepackage{fontspec}
\usepackage{xifthen}
\usepackage{ifoddpage}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{fp}
\usepackage{xparse}
\usepackage[skins,breakable]{tcolorbox}
\usepackage{tikz}
\graphicspath{ {images/} }

\tracingpatches

\setmainfont{OpenSans}
\newfontfamily{\fallbackfont}{Arial}
\DeclareTextFontCommand{\textfallback}{\fallbackfont}
\newunicodechar{█}{\textfallback{█}}

\makeatletter

\widowpenalty10000
\clubpenalty10000

\makechapterstyle{mychapterstyle}{
% Remove 'Chapter 1' from beginning of chapters
{%\parindent \z@ \raggedright \normalfont
%\parskip \z@
\parindent \z@ \memRTLraggedright \normalfont%
\printchaptertitle{##1}% \Huge \bfseries #1
\afterchaptertitle% \par\nobreak \vskip 40\p@
}
}

\setlength{\afterchapskip} {10pt}% Reduce spacing after headings

% Remove spacing before chapter titles
%\setlength{\beforechapskip}{0pt} % Doesn't work

% Allow new chapter to start on right side page
\renewcommand\clearforchapter{
\clearpage
}
}

\NewEnviron{blockquote}{
\par
%\begin{tikzpicture}
%\node[rectangle,minimum width=0.9\textwidth] (m) {%
\centering{
\begin{tcolorbox}[blanker, breakable,
skin=freelance,
boxsep=5pt,
borderline={0mm}{0mm}{black!80!white,dashed},
width=0.9\textwidth,
every float={%
}
]%
\BODY%
\end{tcolorbox}%
}
%};
%\draw[dashed] (m.south west) rectangle (m.north east);
%\end{tikzpicture}
}

\newcommand{\beginsection}[1]{%
\let\@section@title@\relax%
\normalfont\normalsize%
\par%
\vspace{0.5pt}% Required for vertical alignment, not quite sure why it's necessary
%
\gdef\@section@title@{#1}%
}

\newcommand{\begindocument}[1]{%
\let\@section@title@\relax%
\newpage%
\beginsection{#1}%
}

\makepagestyle{headings} {} % Disable default Memoir repeated headings, we'll make our own

\definecolor{grey}{gray}{0.8}
\definecolor{invis}{gray}{1}

\begingroup\let\protect\@typeset@protect
\checkoddpage
\ifoddpage
{
}
\else
{
\textcolor{invis}{\chaptitlefont\f@rhdr\ (cont'd.)}% Header not displayed but still present for spacing reasons
}
\fi
\afterchaptertitle
\vspace{5pt} % Required for vertical alignment, not quite sure why it's necessary
\if\relax\@section@title@%
\relax
\else%
\@tempskipa \beforesecskip\relax%
\ifoddpage
\else
{\textcolor{invis}{\normalfont\secheadstyle \@section@title@\ (cont'd.)}}% Header not displayed but still present for spacing reasons
\fi
\fi%
\vspace{7pt}
\endgroup
}

\EveryShipout{%
\fi%
}

\makeatother

%\raggedright

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{mychapterstyle}
\chapter{My Document}
\beginsection{Normal content}
\lipsum[10-29]
\beginsection{Transcript}
\begin{blockquote}
\lipsum[30-54]
\end{blockquote}
\end{document}


2017 UPDATE:

I took a bit of a break from this project, but I'd really like to finish it and this issue is standing in my way. Just to summarise and elaborate on the progress I've made in the comments, I've now put my headings inside the memoir header system using \makeoddhead and \makeevenhead. This has the improvement that my headings now show up on every page, but it seems that Memoir requires headings to be a constant size and can't size them based on the contents. When a new chapter starts, I want my header to disappear and simply to use the \chapter{My heading} block to render my text, and the fixed height requirement prevents this. In order to compensate, I now render my headers twice, once in memoir which visually displays the header, and a second time in EveryShi, but invisible, which I just use for spacing. My current code is here:

% IMPORTANT: To build, run XeLaTeX twice!
\documentclass[twoside]{memoir}
\usepackage{color}
\usepackage{fontspec}
\usepackage{xifthen}
\usepackage{ifoddpage}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{fp}
\usepackage{xparse}
\usepackage[skins,breakable]{tcolorbox}
\usepackage{tikz}
\usepackage{pgf}
\graphicspath{ {images/} }

\tracingpatches

\setmainfont{OpenSans}
\newfontfamily{\fallbackfont}{Arial}
\DeclareTextFontCommand{\textfallback}{\fallbackfont}
\newunicodechar{█}{\textfallback{█}}

\makeatletter

\widowpenalty10000
\clubpenalty10000

\makechapterstyle{mychapterstyle}{
% Remove 'Chapter 1' from beginning of chapters
{%\parindent \z@ \raggedright \normalfont
%\parskip \z@
\parindent \z@ \memRTLraggedright \normalfont%
\printchaptertitle{##1}% \Huge \bfseries #1
\afterchaptertitle% \par\nobreak \vskip 40\p@
}
}

\setlength{\afterchapskip} {10pt}% Reduce spacing after headings

% Remove spacing before chapter titles
%\setlength{\beforechapskip}{0pt} % Doesn't work

% Allow new chapter to start on right side page
\renewcommand\clearforchapter{
\clearpage
}
}

\NewEnviron{blockquote}{
\par
%\begin{tikzpicture}
%\node[rectangle,minimum width=0.9\textwidth] (m) {%
\centering{
\begin{tcolorbox}[blanker, breakable,
skin=freelance,
boxsep=5pt,
borderline={0mm}{0mm}{black!80!white,dashed},
width=0.9\textwidth,
extras middle and last={before skip=45pt}
]%
\BODY%
\end{tcolorbox}%
}
%};
%\draw[dashed] (m.south west) rectangle (m.north east);
%\end{tikzpicture}
}

\newcommand{\beginsection}[1]{%
\let\@section@title@\relax%
\normalfont\normalsize%
\par%
\vspace{0.5pt}% Required for vertical alignment, not quite sure why it's necessary
%
\gdef\@section@title@{#1}%
}

\newcommand{\begindocument}[1]{%
\let\@section@title@\relax%
\newpage%
\beginsection{#1}%
}

\makepagestyle{headings} {} % Disable default Memoir repeated headings, we'll make our own

\definecolor{grey}{gray}{0.8}
\definecolor{invis}{gray}{1}

\begingroup\let\protect\@typeset@protect%
\pgfsetfillopacity{#1}%
\pgfsetfillopacity{0}%

\afterchaptertitle
\vspace{5pt} % Required for vertical alignment, not quite sure why it's necessary
\if\relax\@section@title@%
\relax
\else%
\@tempskipa \beforesecskip\relax%
\pgfsetfillopacity{#1}%
\pgfsetfillopacity{1}%
\fi%
\vspace{7pt}
\endgroup
}

\par%
\vspace{5pt}%
}

\EveryShipout{%
\fi%
}

%\makepagestyle{scpbookpage}

\makeatother

%\raggedright

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{mychapterstyle}
\chapter{My Document}
\beginsection{Normal content}
\lipsum[10-29]
\beginsection{Transcript}
\begin{blockquote}
\lipsum[30-54]
\end{blockquote}
\end{document}


The issue with this is that EveryShipout still doesn't run in the middle of a tcolorbox, so on subsequent pages my heading renders behind the tcolorbox:

The nice thing here is that I know that on a page that starts partway through a tcolorbox, the header will always have the same size. So all I need to do is make the tcolorbox on subsequent pages start a bit lower down. I tried a suggestion from Ulrike Fischer in the comments to use topsep at break=3cm but that didn't quite have the right effect:

Anyway, I'd really appreciate any help people can give. Thanks!

• A little test with memoir and tcolorbox shows me that broken tcolorboxes respect normal headers and footers. So, I imagine that the problem appears because you don't use them and forces/breaks page geometry with EveryShipout. – Ignasi Nov 3 '16 at 9:46
• You can move the text in the tcolorbox down with topsep at break=3cm, and you could adapt the skins of the middle and last parts to get the correct frames and use overlay to insert your chapter heading (it hasn't problem to draw something outside a frame, but you are inside anyway). But imho it would be much more saner and easier to do as Ignasi suggested and to enlarge the headers if you want this whitespace everywhere anyway. – Ulrike Fischer Nov 3 '16 at 9:50
• @Ignasi Sounds like using proper headers would be a much less dumb approach. The reason I'm using EveryShi is because I couldn't work out how headers and footers are meant to work usually. Is there some documentation you could point me to? – Joshua Walsh Nov 3 '16 at 10:06
• @YM_Industries Of course, memoir documentation explains how to define your own headers and footers, and also how to define page geometry with own memoir commands, and ... – Ignasi Nov 3 '16 at 10:10
• @UlrikeFischer I've started using proper headers, but I've run into a problem where headers have a fixed height, instead of being sized based on the contents. I've therefore had to go with a hybrid solution where I still use EveryShi in order to make the empty space at the top of the page, but that's okay. I'm having trouble getting the topsep at break=3cm you suggested to work though. It lowers the text content of the frame by 3cm, but the border and background still start at the same position. Is there a way I can add vertical spacing before the frame? i.imgur.com/maw0tyr.png – Joshua Walsh Nov 4 '16 at 11:02

I was just about to put this question up for bounty, but then I figured out a solution. It's ugly, but it works.

I'm not going to re-explain the changes I'd made in response to the comments on my question, so if you have a similar problem you're trying to solve, please read the "2017 UPDATE" section of my question for a recap of the changes I've already made.

Essentially the change I've made since then is that I've added a fixed-margin of 65pt to the top of every page. Then, in my EveryShipout code that I use for spacing, I apply negative vspace in order to negate this margin. This allows me to have custom-sized headings based on whether the page is the start of a new chapter or not, but it also takes advantage of the fact that if a page starts with the continuation of a tcolorbox then it's guaranteed to have the maximum sized header.

Updated code:

% IMPORTANT: To build, run XeLaTeX twice!
\documentclass[twoside]{memoir}
\usepackage{color}
\usepackage{fontspec}
\usepackage{xifthen}
\usepackage{ifoddpage}
\usepackage{newunicodechar}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage[none]{hyphenat}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{fp}
\usepackage{xparse}
\usepackage[skins,breakable]{tcolorbox}
\usepackage{tikz}
\usepackage{pgf}
\graphicspath{ {images/} }

\tracingpatches

\setmainfont{OpenSans}
\newfontfamily{\fallbackfont}{Arial}
\DeclareTextFontCommand{\textfallback}{\fallbackfont}
\newunicodechar{█}{\textfallback{█}}

\makeatletter

\widowpenalty10000
\clubpenalty10000

\makechapterstyle{mychapterstyle}{
% Remove 'Chapter 1' from beginning of chapters
\vspace*{-91pt}
{%\parindent \z@ \raggedright \normalfont
%\parskip \z@
\parindent \z@ \memRTLraggedright \normalfont%
\printchaptertitle{##1}% \Huge \bfseries #1
\afterchaptertitle% \par\nobreak \vskip 40\p@
}
}

\setlength{\afterchapskip} {10pt}% Reduce spacing after headings

% Remove spacing before chapter titles
%\setlength{\beforechapskip}{0pt} % Doesn't work

% Allow new chapter to start on right side page
\renewcommand\clearforchapter{
\clearpage
}
}

\NewEnviron{blockquote}{
\par%
%\begin{tikzpicture}
%\node[rectangle,minimum width=0.9\textwidth] (m) {%
\centering{
\begin{tcolorbox}[blanker, breakable,
skin=freelance,
boxsep=5pt,
borderline={0mm}{0mm}{black!80!white,dashed},
width=0.9\textwidth,
extras middle and last={before skip=45pt}
]%
\BODY%
\end{tcolorbox}%
}
%};
%\draw[dashed] (m.south west) rectangle (m.north east);
%\end{tikzpicture}
}

\newcommand{\beginsection}[1]{%
\let\@section@title@\relax%
\normalfont\normalsize%
\par%
\vspace{8.5pt}% Required for vertical alignment, not quite sure why it's necessary
%
\gdef\@section@title@{#1}%
}

\newcommand{\begindocument}[1]{%
\let\@section@title@\relax%
\newpage%
\beginsection{#1}%
}

\makepagestyle{headings} {} % Disable default Memoir repeated headings, we'll make our own

\definecolor{grey}{gray}{0.8}
\definecolor{invis}{gray}{1}

\begingroup\let\protect\@typeset@protect%
\pgfsetfillopacity{#1}%
\pgfsetfillopacity{0}%

\afterchaptertitle
\vspace{5pt} % Required for vertical alignment, not quite sure why it's necessary
\if\relax\@section@title@%
\relax
\else%
\@tempskipa \beforesecskip\relax%
\pgfsetfillopacity{#1}%
\pgfsetfillopacity{1}%
\fi%
\vspace{15pt}%
\endgroup%
}

\par%
\vspace{5pt}%
}

\EveryShipout{%
\begingroup\let\protect\@typeset@protect%
\vspace*{-75pt}%
\endgroup%
\fi%
}

%\makepagestyle{scpbookpage}

\makeatother

%\raggedright

\setlength{\parindent}{0pt}%
\nonzeroparskip

\begin{document}
\chapterstyle{mychapterstyle}
\chapter{My Document}
\beginsection{Normal content}
\lipsum[10-29]
\beginsection{Transcript}
\begin{blockquote}
\lipsum[30-54]
\end{blockquote}
\end{document}