我尝试引用此环境,但结果显示有两个问号。此环境是否缺少某些内容,以便我能够引用它?
\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\newcounter{testexample}
\usepackage{xparse}
\def\exampletext{Eksempel} % If English
\NewDocumentEnvironment{testexample}{ O{} }
{
\colorlet{colexam}{red!55!black} % Global example color
\newtcolorbox[use counter=testexample]{testexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={\exampletext\ \thetcbcounter: #1},% use \thetcbcounter to access the
testexample counter text
% Attaching a box requires an overlay
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); },%
}
\begin{testexamplebox}}
{\end{testexamplebox}\endlist}
\begin{document}
\begin{testexample}[Test]
\label{ex:1}
Here is an example
\end{testexample}
Look at example \ref{ex:1}
\end{document}
答案1
这个盒子和环境的规格尚不清楚,但的定义examplebox
应该在环境之外完成,以及使用标签键。
然而,环境的可选参数被滥用为环境的标题,因此说label=ex:1
作为一种选择是不可能的,因为它会被解释为一个标题。
我添加了第二个可选参数\NewTColorBox
,该IfValueTF
选项会检查这一点。
有了更多信息,我会推荐使用独立的盒子环境,而不是这种包装。
离题:ex:1
是一个糟糕的标签名称——名称中带有数字的标签容易出错。
\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\newcounter{testexample}
\usepackage{xparse}
\def\exampletext{Eksempel} % If English
\NewTColorBox[use counter=testexample]{testexamplebox}{O{}o}{%
% Example Frame Start
empty,% Empty previously set parameters
title={\exampletext\ \thetcbcounter: #1},% use \thetcbcounter to access the
% Attaching a box requires an overlay
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); },%
IfValueTF={#2}{#2}{},
}
\NewDocumentEnvironment{testexample}{O{}O{}}
{%
\colorlet{colexam}{red!55!black} % Global example color
\begin{testexamplebox}[#1][#2]
}{\end{testexamplebox}\endlist}
\begin{document}
\begin{testexample}[Test][label=ex:1]
Here is an example
\end{testexample}
Look at example \ref{ex:1}
\end{document}