latex3 / xcolor

Driver-independent color extensions for LaTeX and pdfLaTeX
LaTeX Project Public License v1.3c
28 stars 5 forks source link

Create an environment for colored boxes #27

Open tobiasdiez opened 1 year ago

tobiasdiez commented 1 year ago

It would be nice if in addition to the colorbox command, one could also have it as an environment:

\begin{colorbox}[blue]
.... some longer content goes here ...
\end{colorbox}

This is especially handy in a setting where the content is long, or if one wants to define own environments using colored boxes.

The package tcolorbox provides such a functionality, but it seems a bit wasteful to load it in addition to xcolor.

Udi-Fogiel commented 1 year ago

You can simply define your own environment. For example

\documentclass{article}

\usepackage{xcolor}

\NewDocumentEnvironment{cb}{O{blue}b}{%
    \colorbox{#1}{#2}%
}

\begin{document}
    \begin{cb}
        Test
    \end{cb}

       \begin{cb}[red]
        Test
    \end{cb}
\end{document}
dbitouze commented 11 months ago

@Udi-Fogiel It doesn't give the expected result in case long text: it is composed on a single line and protrudes into the right margin. An improvement would be to rely on \parbox.