The tcolorbox package
Draws coloured boxes with a frame, a background and an optional title: the tcolorbox environment and \newtcolorbox for box types of your own.
Loading the package
\usepackage{tcolorbox}\fbox draws a black frame around one line. A tcolorbox holds paragraphs, lists and formulas, has rounded corners, separate colours for frame and background, and a title bar. The options are keys in square brackets: colback for the background, colframe for the frame, title for the title.
\newtcolorbox{name}[1]{options} defines an environment with fixed options, for example one box type for notes and one for warnings, so the layout is defined in one place.
The base package covers boxes on one page. Further functions are libraries, loaded with \tcbuselibrary{...}: breakable for boxes that continue on the next page, skins for drawing with TikZ, theorems for numbered boxes. \usepackage[most]{tcolorbox} loads these three and several more.
Main commands
\begin{tcolorbox}[title=Note] ... \end{tcolorbox} | Box with a title bar. |
colback=blue!5, colframe=blue!60!black | Background colour and frame colour. |
\newtcolorbox{notebox}[1]{colback=blue!5, title={#1}} | Defines the environment notebox with one argument for the title. |
sharp corners, boxrule=1pt | Square corners and a frame of 1 pt. |
\tcblower | Splits the box into an upper and a lower part with a dashed line between them. |
\tcbox{text} | Box that is as wide as its content; \tcbox[on line]{text} sits inside a line of text. |
\tcbuselibrary{breakable} | Loads a library; with it, the key breakable lets a box break across pages. |
Complete example
\documentclass{article}
\usepackage{tcolorbox}
\newtcolorbox{notebox}[1]{colback=blue!5, colframe=blue!60!black, title={#1}}
\begin{document}
\begin{tcolorbox}
A box with the default colours.
\end{tcolorbox}
\begin{notebox}{Note}
The title comes from the argument of the environment.
\tcblower
The lower part follows after the dashed line.
\end{notebox}
\begin{tcolorbox}[colback=yellow!10, colframe=orange,
sharp corners, boxrule=1pt, title=Warning]
Square corners and a thin frame.
\end{tcolorbox}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- A box does not break across pages by default. A box that is longer than the rest of the page moves to the next page, and one that is longer than a page runs off its lower edge. Load the library
breakableand set the keybreakable. - A key from a library that is not loaded, such as
breakableorenhanced, gives I do not know the key '/tikz/thik' and I am going to ignore it. Perhaps you misspelled it. - A float such as
figureinside a box gives Not in outer par mode. Put the image in without the float and write its caption with\captionoffromcaption.