针对 `kaobook` 文档类的带有颜色框和垂直线的自定义示例环境

针对 `kaobook` 文档类的带有颜色框和垂直线的自定义示例环境

我正在为该kaobook课程创建一个自定义示例环境,并希望修改第二个示例环境这个问题

![在此处输入图片描述

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

我想修改环境以便:

  • 我希望绿色的“示例”框位于垂直线的右侧,而不是在边缘。

  • 我希望绿色的“示例”框和垂直线之间有间隙,而不是连接到垂直线。

  • 我还希望该示例有一个与此类似的“标题”。(例如标题:“评估函数”)

如果有帮助的话,这里是Overleaf 项目。请随意编辑。


编辑:感谢 Alan Xiang 提供的解决方案!

在此处输入图片描述

我想知道是否可以与定理和其他 kaobox 共享编号?我尝试过更改,\newcounter{examplecounter}\counterwithin{examplecounter}{section}\numberwithin{kaocounter}{section}没有成功。

谢谢你!

答案1

如果这是您正在寻找的,请告诉我。它还支持跨多个页面拆分框。

在此处输入图片描述

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

相关内容