2

How would i go about adding a vertical line next to my example environment like in this post: Example environment?. The reason I have not just copied the environment from that post is that, it isn't possible(at least to my knowledge) to label and refer to that environment.

\documentclass{memoir}
\usepackage[most]{tcolorbox}
\newcounter{testexample}
\usepackage{xparse}
\usepackage{lipsum}

\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}
\lipsum[1]
\begin{testexample}[Latin Text][label=ex:latintext]
\lipsum[2]
\end{testexample}
I'm here refering to example \ref{ex:latintext}
\end{document}

enter image description here

What i would like to add is this:

enter image description here

1 Answers1

3

There is a typo after

parbox=true.

The . should be a ,

The rest of the code is not executed until it finds the next , after an option, since the first overlay specification is meant for the unbroken box and the example is not in a broken box the code for this is never executed.

The better variant is using borderline keys, I'll update later on.

\documentclass{memoir}
\usepackage[most]{tcolorbox}
\newcounter{testexample}
\usepackage{xparse}
\usepackage{lipsum}

\def\exampletext{Eksempel} % If English
\NewTColorBox[use counter=testexample]{testexamplebox}{O{}o}{%
% Example Frame Start
empty,% Empty previously set parameters
%enhanced,
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,
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}
\lipsum[1]
\begin{testexample}[Latin Text][label=ex:latintext]
\lipsum[2]
\end{testexample}
I'm here refering to example \ref{ex:latintext}
\end{document}
  • Surely it is an answer that I will need later in my work. My upvote +1 there is always. – Sebastiano Mar 17 '18 at 08:22
  • This solution seems not to be compatible with a kaobox environment from the kaobook document class. If you try to use an enumerate environment inside of a kaobox, this solution will cause the bullets and items to display outside (left of) the kaobox. Is there any workaround? https://tex.stackexchange.com/questions/604550/vertical-line-next-to-example-environment-changes-indentation-of-itemize-and-enu – EthanAlvaree Jul 11 '21 at 00:51
  • I would also like to customize the environment to display the word "example" inside of a color box on the right side of the vertical line. That is a separate question: https://tex.stackexchange.com/questions/603869/kaobook-example-environment-with-colorbox-and-vertical-line – EthanAlvaree Jul 11 '21 at 00:52