官术网_书友最值得收藏!

  • 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:

  1. Create a small document based on any document class. The article package is a simple choice. Load the blindtext package to generate dummy text. This time, we will use the pangram option to create short pangrams as dummy text. The blindtext package requires the babel package, so we load it before. We also set English as the language. Furthermore, load the tcolorbox package. Our base document looks like this:
    \documentclass{article}
    \usepackage[english]{babel}
    \usepackage[pangram]{blindtext}
    \usepackage{tcolorbox}
    \begin{document}
    \end{document}
  2. 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:

    How to do it...
  3. 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:

    How to do it...
  4. 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:

    How to do it...
  5. 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 to do it...

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 the tcolorbox package with the \tblower command
  • \tcbbox: Using this command cannot be broken across pages, in contrast to the tcolorbox 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.

主站蜘蛛池模板: 津南区| 十堰市| 乌拉特前旗| 汝阳县| 周口市| 扎赉特旗| 寻甸| 静安区| 吉安县| 成安县| 金川县| 怀来县| 康马县| 甘孜| 穆棱市| 启东市| 山东省| 长武县| 马公市| 河北省| 琼结县| 罗平县| 洛阳市| 启东市| 安义县| 将乐县| 苏尼特左旗| 和顺县| 南华县| 从化市| 扎赉特旗| 雅江县| 武义县| 民和| 綦江县| 安陆市| 上饶县| 台中市| 陇南市| 固安县| 贡嘎县|