I had a very difficult time pinning this problem down, because it only exists when certain page-breaking conditions occur.
Problem
Text after environment mysteriously turns white under some conditions.
Hypothesis
When embedded listings code within a tcolorbox, it is possible that the scope is lost when a page break should occur that would split that environment, but instead of page breaking, LaTeX reaches a certain penalty such that LaTeX decides to keep the environment on one page.
I have no idea how to diagnose this issue.
Let me illustrate the problem.
Code with expected Output
This code works fine.
\documentclass{article}
\usepackage{fontspec}%compile with xelatex
\usepackage{listings}%for mylstcode
\usepackage{xparse}%for latex3 NewDocumentEnvironemnt
\usepackage[most]{tcolorbox}% for newtcolorbox and colorlet
\lstnewenvironment{mylstcode}[1][]{\lstset{#1,basicstyle=\ttfamily,columns=fullflexible,keepspaces=true,basewidth=0.5em,breaklines=true}}{} % for general code
\NewDocumentEnvironment{myexample}{ O{} O{} }
{
\colorlet{colexam}{blue!50!black} % The maximal total height of the upper and of the lower part of normal breakable tcolorboxes is about 65536pt (ca. 2300cm).
\newtcolorbox[]{myexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={Example: #1},% use \thetcbcounter to access the ithexample counter text
% Attaching a box requires an overlay
breakable=unlimited,
attach boxed title to top left,
% Ensures proper line breaking in longer titles
minipage boxed title,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=3mm,right=0mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },%
#2
}
\begin{myexamplebox}}
{\end{myexamplebox}\endlist}
\begin{document}
\begingroup
\obeylines
1
1
1
1
\endgroup
%\vspace{-1ex} % Forcefully squeeze the two environments together
\begin{myexample}[for i in \{1..40\}; do echo \$i; done]
\begin{mylstcode}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
\end{mylstcode}
\end{myexample}
Here is a sentence that turns white.
Here is a sentence that turns white.
Here is a sentence that turns white.
Here is a sentence that turns white.
\end{document}
Output
Code with unexpected output
Note that the only change is uncommenting \vspace{-1ex}
This typesets, but please examine the sentences below \end{myexample}. You should notice that the text below the page break becomes white!
\documentclass{article}
\usepackage{fontspec}%compile with xelatex
\usepackage{listings}%for mylstcode
\usepackage{xparse}%for latex3 NewDocumentEnvironemnt
\usepackage[most]{tcolorbox}% for newtcolorbox and colorlet
\lstnewenvironment{mylstcode}[1][]{\lstset{#1,basicstyle=\ttfamily,columns=fullflexible,keepspaces=true,basewidth=0.5em,breaklines=true}}{} % for general code
\NewDocumentEnvironment{myexample}{ O{} O{} }
{
\colorlet{colexam}{blue!50!black} % The maximal total height of the upper and of the lower part of normal breakable tcolorboxes is about 65536pt (ca. 2300cm).
\newtcolorbox[]{myexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={Example: #1},% use \thetcbcounter to access the ithexample counter text
% Attaching a box requires an overlay
breakable=unlimited,
attach boxed title to top left,
% Ensures proper line breaking in longer titles
minipage boxed title,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,left=3mm,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=3mm,right=0mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },%
#2
}
\begin{myexamplebox}}
{\end{myexamplebox}\endlist}
\begin{document}
\begingroup
\obeylines
1
1
1
1
\endgroup
\vspace{-1ex} % Forcefully squeeze the two environments together
\begin{myexample}[for i in \{1..40\}; do echo \$i; done]
\begin{mylstcode}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
\end{mylstcode}
\end{myexample}
Here is a sentence that turns white.
Here is a sentence that turns white.
Here is a sentence that turns white.
Here is a sentence that turns white.
\end{document}
Note to Christian Hupfer: I know you do not like when I wrap tcolorbox with a \NewDocumentEnvironment :)



{ \colorlet{colexam}{blue!50!black} %puts two word spaces before the environment (which may, or may not be relevant but beware need for%at ends of lines!!) – David Carlisle May 09 '16 at 12:58xelatex– May 09 '16 at 12:59externallstenvironment whiletcolorboxprovides the same functionality is 'weird', in my point of view – May 09 '16 at 13:06tcolorbox3.90. I don't say that it's atcolorboxproblem, however – May 09 '16 at 16:04xelatexwithout white lines.XeTeX, Version 3.14159265-2.6-0.99996 (MiKTeX 2.9 64-bit),tcolorbox 2016/07/14 version 3.94. By the way, I did nothing to achieve this, because I would not know what. I guess,XeTeXchanged (?). – Thomas F. Sturm Sep 15 '16 at 10:05XeTeXrelease notes. I do not know either. – Jonathan Komar Sep 15 '16 at 10:06