问题

问题

我很难确定这个问题,因为它只在发生某些分页条件时才会存在。

问题

在某些情况下,环境后的文本会神秘地变成白色。

假设

当嵌入listings code到 中时tcolorbox,当应该发生分页符来分割该环境时,范围可能会丢失,但是 LaTeX 不会分页,而是达到一定的惩罚,因此 LaTeX 决定将环境保留在一页上。

我不知道如何诊断这个问题。

让我来说明一下这个问题。

具有预期输出的代码

该代码运行良好。

\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}

输出

在此处输入图片描述 在此处输入图片描述

具有意外输出的代码

请注意,唯一的变化是取消注释\vspace{-1ex}

这是排版,但请检查下面的句子\end{myexample}。您应该注意到分页符下方的文本变成了白色!

\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}

给 Christian Hupfer 的备注:我知道你不喜欢我tcolorbox\NewDocumentEnvironment:)

答案1

listings包裹在 atcolorbox和中之间存在一些奇怪的交互xelatex。我建议使用特殊的\newtcblisting并使用它(在外部包装中,如果真的需要;-))

在这种情况下,文本不会消失,无论是用还是pdflatex进行编译。xelatexlualatex

\documentclass{article}
\usepackage{fontspec}%compile with xelatex
\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

\newtcblisting{myexamplelisting}[3][]{%
  listing only,
  listing options={#1,basicstyle=\ttfamily,columns=fullflexible,keepspaces=true,basewidth=0.5em,breaklines=true},
  empty,% Empty previously set parameters
  title={Example: #2},% 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); },%
    #3%
}

\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).
  \myexamplelisting{#1}{#2}%
}
{\endmyexamplelisting\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}

在此处输入图片描述

答案2

更新 2016-09-16

Thomas F. Sturm 在问题评论中提到,这个问题可能不再存在,可能是由于 的改变xelatex,但不是由于 的改变tcolorbox


解决方案直至兼容性修复

在你的序言中添加以下行明确重置字体颜色每行后面都变成黑色tcolorbox

  • \AfterEndEnvironment{myexample}{\color{black}}

嵌入tcolorbox 在列表中将在列表范围之外否定上述修复。因此还添加以下内容:

  • \AfterEndEnvironment{itemize}{\color{black}}
  • \AfterEndEnvironment{enumerate}{\color{black}}
  • \AfterEndEnvironment{description}{\color{black}}

加载时xcolor明确调用\color{black}

更新 2016-05-12:您无需明确设置“黑色”,而是可以使用\AtBeginDocument{\colorlet{defaultcolor}{.}}来制作一个defaultcolor可以在每次之后使用的颜色myexample,以恢复默认颜色 - 至少直到某天软件包/编译器协调工作为止。请参阅以下答案xcolor - 默认文本颜色相当于什么?


相关问题


mdframed方法

请注意,这有一个破坏交易的限制

它的缺点是嵌入式列表环境无法正确分页

此外,我对此包的抱怨已经到了无以复加的地步listings。它似乎经常引发神秘的性格问题。

\documentclass{article}
\usepackage{fontspec}% typeset with xelatex
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[framemethod=tikz]{mdframed} % Add easy frames to paragraphs
\usepackage{xparse}
\usepackage{listings}

\lstnewenvironment{mylstcode}[1][]{\lstset{#1,basicstyle=\ttfamily,columns=fullflexible,keepspaces=true,basewidth=0.5em,breaklines=true}}{} % for general code

\mdfdefinestyle{exampledefault}{%
hidealllines=true,
linecolor=blue!20!black,
fontcolor=blue!20!black,
rightline=false,
leftline=true,
bottomline=false,
topline=false,
innerleftmargin=10pt,innerrightmargin=10,
frametitlerule=false,
frametitlerulecolor=blue!20!black,
frametitlebackgroundcolor=white,
frametitlerulewidth=1pt,
 firstextra={\path let \p1=(P), \p2=(O) in ($(\x2,0)+0.5*(0,\y1)$)
                           node[] {};},%
 secondextra={\path let \p1=(P), \p2=(O) in ($(\x2,0)+0.5*(0,\y1)$)
                           node[] {};},%
 middleextra={\path let \p1=(P), \p2=(O) in ($(\x2,0)+0.5*(0,\y1)$)
                           node[] {};},%
 singleextra={\path let \p1=(P), \p2=(O) in ($(\x2,0)+0.5*(0,\y1)$)
                           node[] {};},%
}
\NewDocumentEnvironment{myexample}{ O{} O{} }
{\begin{mdframed}[style=exampledefault,frametitle={Example: #1}]}
{\end{mdframed}}

\begin{document}
\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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
\end{mylstcode}
\end{myexample}
\end{document}

相关内容