8

How do create a colourful example environment like the one here?

enter image description here

  • that is a simple colored table – user2478 Nov 19 '14 at 13:44
14

Below I present two options, both of them admitting page breaks.

  1. A simple example using tcolorbox (admits page breaks); adjust the settings according to your needs:

    enter image description here

    The code:

    \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. A simple example using mdframed (admits page breaks); adjust the settings according to your needs:

    enter image description here

    The code:

    \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}
    
  • @Gauss You're welcome! I did some improvements to the code in my updated answer that might be of interest for you. – Gonzalo Medina Nov 20 '14 at 1:00
  • @Gauss Now I added another option to my answer, using this time mdframed. – Gonzalo Medina Nov 20 '14 at 1:29
  • @cfr No problem. I've deleted my previous comment, and I will delete this one soon. – Gonzalo Medina Nov 20 '14 at 21:32
  • How can I put the word example out of the box? – azetina Jun 20 '16 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.