- LaTeX Cookbook
- Stefan Kottwitz
- 645字
- 2021-07-09 21:25:00
Putting text into a colorful box
You often see important content put into a colored box, especially on posters and slides, although it's used in other documents, too. In this recipe, we will put a little text and also whole paragraphs into a colored box. They can also have titles.
How to do it...
We will use the tcolorbox
package. It is based on pgf
, so you need to have that package installed as well.
We will create a box with the defaults, a titled box with split content, and boxes placed inline that fit the width of the content. Proceed as follows:
- Create a small document based on any document class. The
article
package is a simple choice. Load theblindtext
package to generate dummy text. This time, we will use thepangram
option to create short pangrams as dummy text. Theblindtext
package requires thebabel
package, so we load it before. We also set English as the language. Furthermore, load thetcolorbox
package. Our base document looks like this:\documentclass{article} \usepackage[english]{babel} \usepackage[pangram]{blindtext} \usepackage{tcolorbox} \begin{document} \end{document}
- Create a simple box by inserting this code right after the
\begin{document}
command:\begin{tcolorbox} \blindtext \end{tcolorbox}
This gives you this box with text:
- Change it to, or add, the following code snippet:
\begin{tcolorbox}[title=\textbf{Examples}, colback=blue!5!white,colframe=blue!75!white] The text below consists of pangrams. \tcblower \blindtext[3] \end{tcolorbox}
The new box has a title and is divided into two parts, as shown here:
- Now try this setting and box command in the same document:
\tcbset{colframe=green!50!black,colback=white, colupper=green!30!black,fonttitle=\bfseries, center title, nobeforeafter, tcbox raise base} Normal text \tcbox{Boxed text}
It results in this:
- Then, try this variation:
\tcbox[left=0pt,right=0pt,top=0.5ex,bottom=0pt,boxsep=0pt, toptitle=0.5ex,bottomtitle=0.5ex,title=Sample table]{ \begin{tabular}[t]{rl} Number & 100 \\ Sum & 350 \end{tabular}}
You can see that we can create boxes with titles, even with nontrivial content, such as tabular material:
How it works...
We loaded the blindtext
package with the pangram
option, which gives us short sample text snippets. We used them as dummy box content.
We loaded the tcolorbox
package. The LaTeX environment with the same name then generates boxes for us.
The first box was done without any option with default settings. In the next box, we used the key=value
interface to specify a title and the colors for the frame and the background. The \tcblower
command split the box for us with a dashed line.
Then, we used the \tcbset
command to define options which are valid for all following boxes. It's a good idea to use it in the preamble when all boxes would be similar. We set the color, applied a bold typeface, and centered the title. The nobeforeafter
command implies no additional space before and after the box. The raise base
command raises the whole box so that the baseline of the content matches the outer baseline. You will notice this when you look at our last boxes.
The last boxes were created using the \tcbbox
command. It understands most options of the tcolorbox
package; however, there are two differences:
\tcbbox
: It doesn't provide a lower part, unlike thetcolorbox
package with the\tblower
command\tcbbox
: Using this command cannot be broken across pages, in contrast to thetcolorbox
package
The \tcbbox
command is a good choice for boxes within text. As in our example, it works fine around tabular code. It also works with images.
There's more...
The tcolorbox
package is extremely capable and provides a lot of options and styles. The latter are also called skins. Refer to the package manual to explore further details. The wealth of its features is described over more than 300 pages. You can open the manual by typing the texdoc tcolorbox
command into the command line or by visiting http://texdoc.net/pkg/tcolorbox.
The mdframed
package offers a similar approach. Based on the classic framed
package, it works with modern graphics packages such as TikZ and PSTricks to create colored boxes which can be broken across pages.
- Oracle WebLogic Server 12c:First Look
- 華為HMS生態(tài)與應(yīng)用開發(fā)實(shí)戰(zhàn)
- 程序員面試算法寶典
- Python網(wǎng)絡(luò)爬蟲從入門到實(shí)踐(第2版)
- MATLAB實(shí)用教程
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實(shí)訓(xùn)版)
- Instant RubyMotion App Development
- C# and .NET Core Test Driven Development
- Unity 2018 Augmented Reality Projects
- JavaScript悟道
- Clojure High Performance Programming(Second Edition)
- Visual Basic 程序設(shè)計(jì)實(shí)踐教程
- Learning D
- Unreal Engine Game Development Cookbook
- TypeScript High Performance