精美的示例环境

精美的示例环境

如何创建像这样的彩色示例环境?

在此处输入图片描述

答案1

下面我给出了两个选项,它们都允许分页符。

  1. 一个简单的例子tcolorbox(允许分页符);根据需要调整设置:

    在此处输入图片描述

    代码:

    \documentclass{article}
    \usepackage{lipsum}
    \usepackage[many]{tcolorbox}
    
    \definecolor{greentitle}{RGB}{61,170,61}
    \definecolor{greentitleback}{RGB}{216,233,213}
    
    \newtcolorbox[
      auto counter,
      number within=section
    ]{myexample}[2][]{%
      breakable,
      enhanced,
      colback=white,
      colbacktitle=white,
      arc=0pt,
      leftrule=1pt,
      rightrule=0pt,
      toprule=0pt,
      bottomrule=0pt,
      titlerule=0pt,
      colframe=greentitleback,
      fonttitle=\normalcolor,
      overlay={
        \node[
          outer sep=0pt,
          anchor=east,
          text width=2.5cm,
          minimum height=4ex,
          fill=greentitleback,
          font=\color{greentitle}\sffamily\scshape
        ] at (title.west) {example~\thetcbcounter};
      },
      title=#2,
      #1
    }
    \newcommand\Solution{\par\textbf{\textsf{Solution}}\par\medskip}
    
    \begin{document}
    
    \section{A test section}
    \begin{myexample}{Factorise $x^2-2x+1$}
    \Solution
     \lipsum[4]
    \end{myexample}
    
    \end{document}
    
  2. 一个简单的例子mdframed(允许分页符);根据需要调整设置:

    在此处输入图片描述

    代码:

    \documentclass{article}
    \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}}
    \newcommand\Solution{\par\textbf{\textsf{Solution}}\par\medskip}
    
    \begin{document}
    
    \section{A test section}
    \begin{myexample}{Factorise $x^2-2x+1$}
    \Solution
     \lipsum[4]
    \end{myexample}
    
    \end{document}
    

相关内容