- 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.
- Puppet 4 Essentials(Second Edition)
- Microsoft Application Virtualization Cookbook
- 單片機C語言程序設計實訓100例:基于STC8051+Proteus仿真與實戰
- Java Web開發之道
- Java Web及其框架技術
- 數據結構習題精解(C語言實現+微課視頻)
- 區塊鏈:以太坊DApp開發實戰
- Apache Karaf Cookbook
- 嚴密系統設計:方法、趨勢與挑戰
- Learning Python Design Patterns
- Python忍者秘籍
- Scala Reactive Programming
- Java程序設計與項目案例教程
- Android高級開發實戰:UI、NDK與安全
- Scratch編程從入門到精通