How to construct a coloured box with rounded corners

I come here looking for the answer to the same question. I wanted it to look nice as well, have colour on the inside too, but unfortunately when trying to implement the suggestions above it didn't seem to work for multiple lines.

What I did was go to the tcolorbox manual http://get-software.net/macros/latex/contrib/tcolorbox/tcolorbox.pdf and had a quick read through the beginning. It tells you how to make very nice looking textboxes very quickly (all of which with rounded corners).

The simplest thing to do is just to include the \usepackage{tcolorbox} and then where you want the coloured textbox you just type

\begin{tcolorbox}
A physical explanation the \emph{dynamic matrix}\\
lots of text\\
a new line\\
equation
\begin{equation}
    %\label{eq:dynamic_diag}
    \nonumber
        \bm C \bm D \bm C^{\dagger}=\bm \Omega = \left(\begin{array}{cccc}
            \omega^2_1 & 0 & ... & 0\\
            0 & \omega^2_2 & ... & 0\\
            \vdots &  & \ddots & \vdots \\
            0 & 0 & ... & \omega^2_{Nd}
        \end{array}\right),
\end{equation}
where $\bm C$ is a unitary matrix (each column is one of the eigenvectors of the dynamic matrix $\bm D$), $Nd$ is the product of the number of particlces, $N$, and the number of dimensions, $d$.
\end{tcolorbox}

Which gives you something like

Picture of tcolorbox created with default settings

One can also customise it, either with "[options]" after the the "\begin{tcolorbox}" or one can create a "mybox" environment. So, if I want it to be a clear red background with a darker red frame then I type

\usepackage{tcolorbox} \newtcolorbox{mybox}{colback=red!5!white,colframe=red!75!black}

where the "newtcolorbox" creates a new environment, "mybox" is the name, "colback" is the background colour of the box, "colframe" is the colour of the frame. See the manual for a huge number of further customisations. Then include the "mybox" environment somewhere in the tex document

\begin{mybox}
A physical explanation the \emph{dynamic matrix}\\
lots of text\\
a new line\\
equation
\begin{equation}
    %\label{eq:dynamic_diag}
    \nonumber
        \bm C \bm D \bm C^{\dagger}=\bm \Omega = \left(\begin{array}{cccc}
            \omega^2_1 & 0 & ... & 0\\
            0 & \omega^2_2 & ... & 0\\
            \vdots &  & \ddots & \vdots \\
            0 & 0 & ... & \omega^2_{Nd}
        \end{array}\right),
\end{equation}
where $\bm C$ is a unitary matrix (each column is one of the eigenvectors of the dynamic matrix $\bm D$), $Nd$ is the product of the number of particlces, $N$, and the number of dimensions, $d$.
\end{mybox}

Which gives you something like: Picture of tcolorbox created with mybox environment

It looks really cool! Adding titles to the boxes is straightforward too. This time use \newtcolorbox{mybox}[1]{colback=red!5!white,colframe=red!75!black,fonttitle=\bfseries,title=#1}, where the "1" means one argument is to be expected after "\begin{mybox}", "fonttitle" is the font type and "title=#1" tell it that the first argument after "\begin{mybox}" is the title, i.e.

\begin{mybox}{A physical explanation of the \emph{dynamic matrix}}

This looks like

Picture of tcolorbox with title


The tcolorbox package does a beautiful job of setting coloured boxes:

enter image description here

\documentclass{article}
\usepackage{tcolorbox}% http://ctan.org/pkg/tcolorbox
\definecolor{mycolor}{rgb}{0.122, 0.435, 0.698}% Rule colour
\makeatletter
\newcommand{\mybox}[1]{%
  \setbox0=\hbox{#1}%
  \setlength{\@tempdima}{\dimexpr\wd0+13pt}%
  \begin{tcolorbox}[colframe=mycolor,boxrule=0.5pt,arc=4pt,
      left=6pt,right=6pt,top=6pt,bottom=6pt,boxsep=0pt,width=\@tempdima]
    #1
  \end{tcolorbox}
}
\makeatother
\begin{document}
\mybox{Here is some fancy box text.}
\end{document}

The tcolorbox options are:

  1. color=mycolor
  2. arc=4pt
  3. boxrule=0.5pt
  4. left=6pt, right=6pt, top=6pt, bottom=6pt

The macro \mybox will always set the box on a single line. If you want more than one line, some changes are required. To remove the background colour, add the option colback=white.


Have a look at the mdframed package:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\definecolor{mycolor}{rgb}{0.122, 0.435, 0.698}

\newmdenv[innerlinewidth=0.5pt, roundcorner=4pt,linecolor=mycolor,innerleftmargin=6pt,
innerrightmargin=6pt,innertopmargin=6pt,innerbottommargin=6pt]{mybox}

\begin{document}

\lipsum[2]
\begin{mybox}
\lipsum[4]
\end{mybox}
\lipsum[2]

\end{document}

enter image description here

Tags:

Color

Boxes