[Tex/LaTex] Create custom note environment with tcolorbox

environmentsparagraphstcolorbox

I just stumbled upon the tcolorbox package but can't seem to find my way through its extensive manual.

What I'm trying to achieve is to create a custom environment to add short notes into a body of text. The enclosed paragraph should start with the word 'Note:' in bold and appear next to a vertical line flush with the left margin of the surrounding text, like so:

enter image description here

So far, I've been using the mdframed package for this but I am starting to get annoyed by its frequent warnings of the sort

You got a bad break because the last box will be empty you have to
change it manually by changing the text, the space or something else.

Here's the code I've been using so far.

\documentclass[11pt]{scrartcl}

\usepackage[framemethod=tikz]{mdframed}
\newmdenv[
    topline=false,
    bottomline=false,
    rightline=false,
    innerrightmargin=0pt
]{siderule}
\newenvironment{note}%
    {\begin{siderule}\textbf{Note:}}
    {\end{siderule}}

\usepackage{blindtext}

\begin{document}

\blindtext
\begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}
\blindtext

\end{document}

I hope someone can help streamline it and make it more robust with tcolorbox.

Best Answer

Such a box is quite easy with the borderline options, which draw vertical bars (or other stuff)

Since the note environment should be versatile, there might be an optional argument that sets other options if needed, see the second (ugly!) example of the note environment.

The vertical etc. spacings before/after and inside of the box might be change using left=... etc. and before skip={} etc. keys.

\documentclass[11pt]{article}

\usepackage[most]{tcolorbox}

\newtcolorbox{note}[1][]{%
  enhanced jigsaw, % better frame drawing
  borderline west={2pt}{0pt}{red}, % straight vertical line at the left edge
  sharp corners, % No rounded corners
  boxrule=0pt, % no real frame,
  fonttitle={\large\bfseries},
  coltitle={black},  % Black colour for title
  title={Note:\ },  % Fixed title
  attach title to upper, % Move the title into the box
  #1
}

\usepackage{blindtext}

\begin{document}

\blindtext
\begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\begin{note}[borderline west={2pt}{0pt}{blue}, colback=yellow, drop shadow={red,opacity=0.6}]
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\blindtext

\end{document}

enter image description here

For more sophisticated usages of borderline etc. please have a look on Reproduce a PDF table with colored vertical semi lines

Here's a version in which the first line of the note box is aligned with the label of an enumerate environment:

\documentclass[11pt]{scrartcl}

\usepackage{enumitem}
\usepackage[most]{tcolorbox}


\makeatletter
\newtcolorbox{note}[1][]{%
  enhanced jigsaw, % better frame drawing
  borderline west={1pt}{0pt}{black}, % straigh vertical line at the left edge
  sharp corners, % No rounded corners
  boxrule=0pt, % no real frame,
  fonttitle={\large\bfseries},
  coltitle={black},  % Black colour for title
  title={Note:\ },  % Fixed title
  attach title to upper, % Move the title into the box,
  right=0pt,
  top=0pt,
  bottom=0pt,
  frame hidden,
  baseline={\tcb@height-2\kvtcb@boxsep+\baselineskip-2\lineskip}, %%%% Needs more to be done for other setups
  #1
}
\makeatother

\usepackage{blindtext}

\begin{document}

\blindtext
\begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\begin{enumerate}
\item \begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\item \begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\end{enumerate}



\blindtext

\end{document}

enter image description here

Related Question