1

I'm trying to make a reference to this environment, but it comes out with the double question mark. Is there anything missing from this environment for me to be able to refer to it?

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

enter image description here

6
  • 1
    Hi, welcome. Can you turn your snippet into a complete example, including a tcolorbox and a reference to it? Edit: that said, you need a label, see section 4.21 in the tcolorbox manual. Mar 14, 2018 at 20:54
  • 2
    Is there any reason of putting a \newtcolorbox inside a \NewDocumentCommand? In any case, the label=foo option should work
    – user31729
    Mar 14, 2018 at 20:57
  • I meant \NewDocumentEnvironment, but my question remains the same
    – user31729
    Mar 14, 2018 at 21:46
  • I tried to turn the code into a complete snippet, am i doing something wrong with the label? Mar 15, 2018 at 6:23
  • @kasp You can use the label key to add so label to a tcolorbox as shown here: tex.stackexchange.com/a/231376/134144
    – leandriis
    Mar 15, 2018 at 6:44

1 Answer 1

3

The specifications of this box and environment are unclear, but the definition of examplebox should be done outside of the environment, as well using the label key.

However, the optional argument of the environment is abused as title of the environment, so saying label=ex:1 is not possible as an option, since it would be interpreted as a title.

I added a second optional argument with \NewTColorBox, the IfValueTF option checks for this.

With more information I would recommend a standalone box environment instead of this wrapping.

Off-topic: ex:1 is a bad label name -- labels with numbers in their name are error prone.

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

enter image description here

2

You must log in to answer this question.

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