2

I am making a custom example environment for the kaobook class and would like to modify the second example environment from this question:

![enter image description here

\usepackage{chngcntr}
\usepackage[tikz]{mdframed}
\usepackage{lipsum}

\definecolor{greentitle}{RGB}{61,170,61} \definecolor{greentitleback}{RGB}{216,233,213}

\newcounter{mdexample} \counterwithin{mdexample}{section}

\newenvironment{myexample}[1] {\stepcounter{mdexample}\begin{mdframed}[ frametitle=#1, frametitlefont=\normalfont, topline=false, bottomline=false, rightline=false, linecolor=greentitleback, linewidth=2pt, singleextra={ \node[ overlay, outer sep=0pt, anchor=north east, text width=2.5cm, minimum height=4ex, fill=greentitleback, font=\color{greentitle}\sffamily\scshape ] at (O|-P) {example~\themdexample}; }, firstextra={ \node[ overlay, outer sep=0pt, anchor=north east, text width=2.5cm, minimum height=4ex, fill=greentitleback, font=\color{greentitle}\sffamily\scshape ] at (O|-P) {example~\themdexample}; } ] } {\end{mdframed}}

I would like to modify the environment so that:

  • I would like the green "Example" box to be on the right side of the vertical line, not in the margin.

  • I would like there to be a gap between the green "Example" box and the vertical line, instead of being connected to the vertical line.

  • I would also like there to be a "title" for the example, similar to this. (e.g. title: "Evaluating a Function")

If it helps, here is a link to the Overleaf project. Feel free to edit it.


Edit: Thank you Alan Xiang for your solution!

enter image description here

I am wondering if it is possible to share numbering with theorems and other kaoboxes? I tried to change \newcounter{examplecounter}\counterwithin{examplecounter}{section} to \numberwithin{kaocounter}{section} but this didn't work.

Thank you!

EthanAlvaree
  • 1,367

1 Answers1

2

Let me know if this is what you are looking for. It also supports breaking the box across multiple pages.

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[skins,hooks,xparse,breakable]{tcolorbox}
\usepackage{xcolor}
\usepackage{chngcntr}
\usepackage{blindtext}
\usepackage{tikz}

\newcounter{examplecounter} \counterwithin{examplecounter}{section}

\usetikzlibrary{calc}

\definecolor{greentitle}{RGB}{61,170,61} \definecolor{greentitleback}{RGB}{216,233,213} \definecolor{drakgreentitle}{RGB}{24,131,80}

\DeclareTColorBox{bookexample}{O{}}{ enhanced, breakable, arc=0pt, boxrule=0pt, colback=white, before skip={2em}, % increase spacing before the env overlay unbroken and first={ % draw box on the left \node[fill=greentitleback, font=\color{greentitle}\sffamily\bfseries\large, anchor=south west, xshift=2mm ] (titlebox) at (frame.north west) {Exercise \refstepcounter{examplecounter}\theexamplecounter};

    % write title on the right
    \node[font=\color{drakgreentitle}\sffamily\large, 
    anchor=west
    ] at ($(titlebox.east)+(2mm,0mm)$) {#1};

    % draw the vertical line on the left
    \draw[draw=greentitleback, line width=2pt] 
        (titlebox.north west-|frame.north west)--(frame.south west);
},
overlay middle and last={
    \draw[draw=greentitleback, line width=2pt] 
        (frame.north west)--(frame.south west);
}
fonttitle=\color{greentitle}\sffamily\bfseries,

}

\DeclareTColorBox{genericbox}{m}{ enhanced, breakable, arc=0pt, boxrule=0pt, colback=white, attach boxed title to top left, boxed title style={ colback=white, colframe=white, }, title={#1}, fonttitle=\color{greentitle}\sffamily\bfseries\large }

\begin{document}

\section{First Section}

\null \begin{bookexample}[My Title] \Blindtext[4] \end{bookexample}

\begin{genericbox}{Solution} \Blindtext[2] \end{genericbox}

\section{Second Section}

\null \begin{bookexample}[Another Title] \Blindtext[4] \end{bookexample}

\begin{genericbox}{Solution} \Blindtext[2] \end{genericbox}

% No title \begin{bookexample} \Blindtext[1] \end{bookexample}

\end{document}

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[skins,hooks,xparse,breakable]{tcolorbox}
\usepackage{xcolor}
\usepackage{chngcntr}
\usepackage{blindtext}
\usepackage{tikz}

% comment this line during integration because kaocounter would have been declared
\newcounter{kaocounter}
% you may not need the line below as well if it has already been configured using other approaches
\counterwithin{kaocounter}{section}

\usetikzlibrary{calc}

\definecolor{greentitle}{RGB}{61,170,61}
\definecolor{greentitleback}{RGB}{216,233,213}
\definecolor{drakgreentitle}{RGB}{24,131,80}

\DeclareTColorBox{bookexample}{O{}o}{
    enhanced,
    breakable,
    arc=0pt,
    boxrule=0pt,
    colback=white,
    top=3em, % you can change the top spacing
    overlay unbroken and first={
        % draw box on the left
        \node[fill=greentitleback,
            font=\color{greentitle}\sffamily\bfseries\large,
            anchor=south west,
            xshift=2mm,
            yshift=-2em, % you can change xshift and yshift to adjust the title placement
        ] (titlebox) at (frame.north west) 
        % notice the difference here, kaocounter is used instead
        {Exercise \refstepcounter{kaocounter}\thekaocounter
        % add cross-referencing support
        \IfValueT{#2}{\label{#2}}
        };

        % write title on the right
        \node[font=\color{drakgreentitle}\sffamily\large, 
        anchor=west
        ] at ($(titlebox.east)+(2mm,0mm)$) {#1};

        % draw the vertical line on the left
        \draw[draw=greentitleback, line width=2pt] 
            (titlebox.north west-|frame.north west)--(frame.south west);
    },
    overlay middle and last={
        \draw[draw=greentitleback, line width=2pt] 
            (frame.north west)--(frame.south west);
    }
    fonttitle=\color{greentitle}\sffamily\bfseries,
}

\DeclareTColorBox{genericbox}{m}{
    enhanced,
    breakable,
    arc=0pt,
    boxrule=0pt,
    colback=white,
    attach boxed title to top left,
    boxed title style={
        colback=white,
        colframe=white,
    },
    title={#1},
    fonttitle=\color{greentitle}\sffamily\bfseries\large
}

\begin{document}

\section{First Section}

\begin{bookexample}[My Title]
\Blindtext[4]
\end{bookexample}

\begin{genericbox}{Solution}
\Blindtext[2]
\end{genericbox}

\section{Second Section}

% an example with label
\begin{bookexample}[Another Title][ex:one]
\Blindtext[4]
\end{bookexample}

\begin{genericbox}{Solution}
\Blindtext[2]
\end{genericbox}

\textbf{I want to refer to Exercise \ref{ex:one}}.

% No title
\begin{bookexample}
\Blindtext[1]
\end{bookexample}

% No title, but has label
\begin{bookexample}[][ex:two]
\Blindtext[1]
\end{bookexample}

\textbf{I want to refer to Exercise \ref{ex:two}}.

\end{document}
Alan Xiang
  • 5,227
  • 1
    Hi Alan, thank you for your solution! Is it possible to share numbering with theorems and other kaoboxes? I tried to change \newcounter{examplecounter}\counterwithin{examplecounter}{section} to \numberwithin{kaocounter}{section} but this didn't work. Also wondering if this can support labeling & referencing? I tried \label{example2} and \ref{example2} but it only printed "1.1" instead of "1.1.2". Also it needs a tiny bit more padding at the top. updated the original question with a new screenshot -- notice how the example box is touching the yellow theorem box above it.) Thanks again! – EthanAlvaree Jul 11 '21 at 20:06
  • 1
    @EthanAlvaree I have modified the code, see the second code listing. The counter is referenced and incremented in the definition of bookexample, therefore changing to \numberwithin{kaocounter}{section} would not work. In the new example, I have assumed that the counter you are working with is kaocounter. I added a second optional argument for cross-referencing support. I also changed the layout of the box so that the top spacing is more controllable. You can change top and yshift to adjust the header position (see more in code comments). – Alan Xiang Jul 12 '21 at 01:35
  • 1
    Hi Alan, thank you! Everything is working except counter is not shared between Definition, Theorem, Example, etc. Any ideas what I am doing wrong? You are welcome to test out ideas in the Overleaf, if that would help. Thank you again. https://www.overleaf.com/3212563377jyzdcgshhhbn – EthanAlvaree Jul 12 '21 at 06:26
  • 2
    I changed the definition of the environment and some settings in styles/mdftheorems.sty, now theorem, definition and example share the same counter. – Alan Xiang Jul 12 '21 at 14:23
  • It worked! Thank you!! – EthanAlvaree Jul 12 '21 at 17:52
  • Everything looks amazing! Does mathematicians have a crush on Palatino fonts in general? I think it is a little bit too wide. – Alan Xiang Jul 12 '21 at 17:55
  • Hi Alan! If you have time, I opened a follow-up question about how to print lists of definitions, theorems, examples, etc. at the front of the book: https://tex.stackexchange.com/questions/606098/list-of-definitions-list-of-theorems-list-of-examples-and-list-of-activities – EthanAlvaree Jul 23 '21 at 08:15