7

I'm new with LaTeX and like to change some parameters of the description environment throughout the document. As minimum, I like to have two arguments: color of text of item and background-color behind text as description.

In other words, I want to give style.

1
  • Welcome to TeX.sx! I took the liberty to reformat and rephrase your question a little, so that it looks better. I hope you don't mind. Commented Jul 12, 2012 at 7:22

3 Answers 3

9

Unfortunately you can not change the style of environments as easy as it is possible using CSS with HTML. However, you can redefine description or define your own variant which adds the color commands.

To set the background color you can use my adjustbox package:

\documentclass{article}
\usepackage{xcolor}
\usepackage{adjustbox}

\newenvironment{cdescription}{%
  \begin{adjustbox}{minipage=\linewidth,bgcolor=blue,vspace=\smallskipamount}
    \begin{description}
      \color{red}%
}{%
    \end{description}
  \end{adjustbox}
}

\begin{document}

Text before

\begin{cdescription}
    \item[Hello] World
    \item[Example] Item
\end{cdescription}

text after

\end{document}

If you want to redefine description instead you can save the old definition away and use it internally.

\let\olddescription\description
\let\endolddescription\enddescription
\renewenvironment{description}{%
  \begin{adjustbox}{minipage=\linewidth,bgcolor=blue,vspace=\smallskipamount}
    \begin{olddescription}
      \color{red}%
}{%
    \end{olddescription}
  \end{adjustbox}
}

If you want to set the colors for every environment simply add [2] after the environment name (e.g., \newenvironment{cdescription}[2]{%) and replace the color names with #1 and #2 in the definition.

1
  • thx you very mach \renewenvironment is very useful
    – Uki
    Commented Jul 12, 2012 at 7:45
2

The description environment itself does not have options for color; however, there are standard constructions you can use to get color. For example, if you want just the two things you mentioned, you can use the xcolor package and the following commands:

\documentclass{article} 
\usepackage{xcolor}
\begin{document}
\noindent
\colorbox{blue}{%
 \begin{minipage}{\textwidth}%
  \begin{description}
   \color{red}
    \item[First] Some text
    \item[Second] Some more text
  \end{description}%
 \end{minipage}%
}
\end{document}

This has a few parts:

  • To get the background color just for the description itself, I put it in a \colorbox.

  • However, a paragraph-constructing environment like description must be in a page-like setting, which \colorbox is not, so I further embed it in a minipage whose width is that of the text.

  • To get the text color, I issue \color{red} just inside the description. Every environment is a group and shields the exterior from changes made in its contents, so the color won't continue outside the description itself.

You say you also want to make these changes throughout the document, to every description. For that, you can define a command that encapsulates this:

% #2 is background color, #3 is text color
% Optional #1 is width
% #4 is the contents of the description
\newcommand\colordesc[4][\textwidth]{%
 \noindent
 \colorbox{#2}{%
  \begin{minipage}{#1}%
   \begin{description}
    \color{#3} 
    #4
   \end{description}%
  \end{minipage}%
 }%
}

% Use like:
\colordesc[5in]{blue}{red}{
 \item[First] some text
 \item[Second] some more text
}

(I made this a command rather than an environment because it is irritating to split the braces of a \colorbox across the ends of an environment.)

2
  • The lrbox environment was created for this kind of things.
    – egreg
    Commented Jul 12, 2012 at 8:46
  • @egreg: Ah, now I know what that environment does. :)
    – Ryan Reich
    Commented Jul 12, 2012 at 15:18
2

At the moment all provided answers can't break across pages. There are some packages which can handle page breaks as well. The most common are

For a simple background can be achieved with framed. If you want to draw a more power full frame you may use mdframed or tcolorbox. Instead of redefining all description environments I recommend to provide an new environment. If you redefine all description environments the following solutions don't work well.

To setup the new description environment I am using the package enumitem.

framed

The package framed defines the environment shaded. To use the environment you have to declare the shadecolor first:

\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{framed}

\newlist{cdescription}{description}{1}
\definecolor{shadecolor}{gray}{0.95}%color of the background
\setlist[cdescription,1]{before=\begin{shaded},after=\end{shaded}}
\usepackage{lipsum}
\begin{document}
\begin{cdescription}
\item[first lipsum] \lipsum[1]
\item[second lipsum] \lipsum[2]
\item[third lipsum] \lipsum[3]
\item[fourth lipsum] \lipsum[4]
\item[fifth lipsum] \lipsum[5]
\end{cdescription}
\end{document}

mdframed

The package mdframed provide his own command to surround any environment. This is done by \surroundwithmdframed:

\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage[framemethod=TikZ]{mdframed}

\newlist{cdescription}{description}{1}
\mdfdefinestyle{cdescription}{%
  hidealllines=true,%no lines drawn
  backgroundcolor=gray!15,
  skipabove=\topskip,
  skipbelow=\topskip,
  roundcorner=15pt,
}
\surroundwithmdframed[style=cdescription]{cdescription}

\usepackage{lipsum}
\begin{document}
\begin{cdescription}
\item[first lipsum] \lipsum[1]
\item[second lipsum] \lipsum[2]
\item[third lipsum] \lipsum[3]
\item[fourth lipsum] \lipsum[4]
\item[fifth lipsum] \lipsum[5]
\end{cdescription}
\end{document}

tcolorbox

\documentclass{article}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage[]{tcolorbox}
\tcbuselibrary{breakable}
\newlist{cdescription}{description}{1}
\setlist[cdescription,1]{before={\begin{tcolorbox}[breakable,boxrule=0pt]},after=\end{tcolorbox}}
\usepackage{lipsum}
\begin{document}
\begin{cdescription}
\item[first lipsum] \lipsum[1]
\item[second lipsum] \lipsum[2]
\item[third lipsum] \lipsum[3]
\item[fourth lipsum] \lipsum[4]
\item[fifth lipsum] \lipsum[5]
\end{cdescription}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .