8

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.

  • 1
    Yes, the tcolorbox manual is overwhelming – user31729 Apr 1 '16 at 17:45
8

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

  • I just saw, that I missed the Note: lead in – user31729 Apr 1 '16 at 17:56
  • you could use a detached title for that? – naphaneal Apr 1 '16 at 18:00
  • @Naphaneal: I know ;-) Thanks anyway – user31729 Apr 1 '16 at 18:01
  • @ChristianHupfer Yes, I was about to ask about that. :) In any case, I've been doing some testing with this and have to say it works great! Way better than mdframed, as I had hoped. Thanks for your help! – Casimir Apr 1 '16 at 18:10
  • 1
    @ChristianHupfer Thanks for all this work, I really appreciated your detailed answer. I have implemented your main answer (the preamble part) to produce these type of "notes". The problem is that when you print the document in paper, (with a HQ laser colour printer) the grey boxes are hardly seen in that grey background. Therefore, I would like to increase the grey background to make it more darker. I have tried adding opacityback=10, just after title={Note:\ }, but this does not work. Is there a way to make the grey background, more intense, darker? Thanks for your more than useful help – DavidC. Sep 26 '16 at 14:56

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.