10

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.

Janosh
  • 4,042

1 Answers1

11

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 –  Apr 01 '16 at 17:56
  • you could use a detached title for that? – naphaneal Apr 01 '16 at 18:00
  • @Naphaneal: I know ;-) Thanks anyway –  Apr 01 '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! – Janosh Apr 01 '16 at 18:10
  • Remark: I also added the options frame hidden, opacityfill=0 (from your comment here), changed borderline west={1pt}{0pt}{black}, and set all sides except left to 0pt, to reproduce my old environment exactly. – Janosh Apr 01 '16 at 18:12
  • @Casimir: Yes, of course -- you define the settings ;-) –  Apr 01 '16 at 18:18
  • @ChristianHupfer One thing I just noticed: When beginning the note right after an \item with no text in between, the item label is moved down vertically to align with the lower edge of the note. Do you of any way to make the item label align with the top of the note? – Janosh Apr 01 '16 at 18:21
  • @ChristianHupfer I just noticed that adding the keyword breakable to note overrides your baseline adjustment. Do you know why that is? – Janosh Apr 04 '16 at 12:50
  • @Casimir: breakable boxes have different height etc. settings and it's not clear how large the box will be. I am not sure there's an easy way to achieve this. –  Apr 04 '16 at 13:32
  • I don't quite see where the complication comes from. Whether or not the box brakes across a page does not affect the position of its upper edge, right? Shouldn't something like box align=top with the boxe's baseline lowered from the top by one \baselineskip still work? – Janosh Apr 04 '16 at 13:37
  • @Casimir: I can't check this right now. It was difficult enough to align it already in the non breakable version (the alignment requirement was an extra issue anyway, not in your question) –  Apr 04 '16 at 13:41
  • 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
  • @DavidC. Thanks for your really appreciating comments -- it's happens very rarely to get such a positive feedback in the last time. Well, the background colour is controlled by the colback option, i.e. try something like colback={white!70!black} as an option to your tcolorbox definition. The larger the number (70 here), the brighter the 'gray' will be. Since you're using a colour laser printer you can try any other colour of course as well. –  Sep 26 '16 at 18:14