2

I would like to create box of example using tcolorbox as the picture below:

enter image description here

and so far this what i got

enter image description here

Here is my code :

\documentclass[justified]{tufte-book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcolorbox[auto counter, number within=chapter]{example}[1][]{
    enhanced,
    breakable,
    left=0.5em,right=0pt,top=1pt,bottom=1pt,    
    attach title to upper={},
    coltitle={black},
    fonttitle={\bfseries},
    title={EXAMPLE \thetcbcounter\ },
    sharp corners,
    frame hidden,
    boxrule=0pt,
    borderline north={1pt}{0pt}{gray},
    borderline west={1pt}{0pt}{blue},
    borderline south={1pt}{0pt}{red},
    colback={white},
    #1
    }
\newcommand\solution{\par\textsf\bfseries{\textbf{\textcolor{black}{SOLUTION }}}}

\begin{document}
    \begin{example}
        \lipsum[2]
        \solution \lipsum[4]
    \end{example}
\end{document}

I want the length of borderline north as long as the "example 0.1" text and the length of borderline south just arbitrary horizontal line.

Could someone please improve my code to create the same picture as I post on top. Thanks!

1

Unless you use a boxed title, there's no node title to be used as reference. Therefore I've changed your style to use a boxed title an inserted a hspace before upper part instead of attach title to upper option. This way has been possible to draw the line over title box. The line below the box is added with a overlay command.

\documentclass[justified]{tufte-book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcolorbox[auto counter, number within=chapter]{example}[1][]{
    enhanced,
    breakable,
    left=0.5em, right=0pt, top=1pt, bottom=1pt,    
    attach boxed title to top left={yshift=-\tcboxedtitleheight},
     boxed title style={%
        empty,
        right=0pt,
        frame code={\draw[line width=1pt, gray] (frame.north west)--(frame.north east);}},
    before upper=\hspace{\tcboxedtitlewidth},
     colbacktitle=white,
    coltitle={black},
    colback={white},
    fonttitle={\bfseries},
    title={EXAMPLE \thetcbcounter\ },
    sharp corners,
    frame hidden,
    boxrule=0pt,
    borderline west={1pt}{0pt}{blue},
     overlay unbroken and last={%
        \draw[line width=1pt, red] (frame.south west)   -- ++(0:1cm);},
    #1
    }
\newcommand\solution{\par\textsf\bfseries{\textbf{\textcolor{black}{SOLUTION }}}}

\begin{document}
    \begin{example}
        \lipsum[2]
        \solution \lipsum[4]
    \end{example}
\end{document}

enter image description here

  • wow thanks @Ignasi, thats exactly what i was looking for. btw if you dont mind, i have another question. What to do if i want to fill the boxed title with some color? – alitob Sep 11 '18 at 1:31
  • Comment empty inside boxed title style and change colbacktitle to something not white. – Ignasi Sep 11 '18 at 6:38
  • oh i see, thx @Ignasi! – alitob Sep 16 '18 at 13:33

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.