7

I would like to set off certain parts of my document by giving them a colored background. With the help of adjustbox I was able to write an environment that does more or less what I want: It puts its contents in a borderless, unpadded box with a colored background.

\usepackage{adjustbox}
\newenvironment{shaded}
  {\par\noindent\adjustbox{frame=0pt,bgcolor=yellow,minipage=\textwidth}\bgroup}
  {\egroup\par}

The problem is that this is, well, a box. It cannot be broken across pages, and it doesn't play nice with floats etc., which cannot be encountered while creating a box. (Thanks to the minipage option, footnotes are captured at the bottom of the shaded area instead of disappearing entirely). For my particular use, restructuring the document to avoid these problems is not an option.

So the question is: Are there any solutions for creating a colored background that are as transparent as possible? I've seen a number of answers for coloring tables, code snippets, boxing that supports page breaks, complete pages(a, b), etc., but nothing I can see how to adapt for more general use.

Complete MWE:

\documentclass{article}

\usepackage{xcolor,adjustbox}

\newenvironment{shaded}
  {\par\noindent\adjustbox{frame=0pt,bgcolor=yellow,minipage=\textwidth}\bgroup}
  {\egroup\par}

\begin{document}

\section{Regular section}
This section is not highlighted. 
This text is just padding to indicate the natural line length. 

\begin{shaded}
This is set with a colored background.

\section{Highlighted section}
This entire section is, 
actually.\footnote{This footnote is trapped in the minipage.}

\begin{table}
This float triggers an error and disappears.
\end{table}

\end{shaded}

\section{The rest of the document}
\end{document}
alexis
  • 7,961

1 Answers1

7

The following packages can be used for shade environments and they allow page breaks:

Here a suggestion using framed:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{framed,xcolor}
\colorlet{shadecolor}{red!20}
\usepackage{blindtext}

\begin{document}
\blindtext
\begin{shaded}
\blindtext\blindtext
\blindtext\blindtext
\end{shaded}
\blindtext
\end{document}

A more modern package which allows you a comfortable configuration of the frame is the package mdframed. Below is a simple example with a shaded background.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{mdframed}
\newmdenv[hidealllines=true,backgroundcolor=red!20]{shaded}
\usepackage{blindtext}

\begin{document}
\blindtext
\begin{shaded}
\blindtext\blindtext
\blindtext\blindtext
\end{shaded}
\blindtext
\end{document}

Related questions are:

Marco Daniel
  • 95,681
  • Really??? No mdframed example? :-) Made me think that there were perhaps two users with this userid!! – Peter Grill Mar 02 '13 at 19:43
  • @PeterGrill: Can I use my own package? I think it's a little bit egocentric. – Marco Daniel Mar 02 '13 at 19:50
  • @MarcoDaniel The boites package is a good illustration of how good TeX is at avoiding (unintentionally) rivers. The examples all use blah, blah...thanks for reminding me about the package. – yannisl Mar 02 '13 at 19:54
  • @YiannisLazarides: The package boites uses an interesting splitting algorithm which makes it interesting ;-) Of course the user won't see it ;-) – Marco Daniel Mar 02 '13 at 19:59
  • @MarcoDaniel: Nah I don't think so. It's not as if you are trying to sell it. If it bothers you, you can always add a disclaimer that you are the author. But, if I was new and didn't know aboutmdframed, and came across this question I would conclude that framed wass being recommended over mdframed. Plus mdframed is such an awesome package. I haven't used framed so I am not implying that there is anything wrong with that package. – Peter Grill Mar 02 '13 at 20:08
  • @marco, I tried your code for mdframed and the line breaks are great, but the box contents are indented from the boundary. I'm aiming for a "tight box" (as with my adjustbox example). Is there a quick way to remove all padding, or do I have to zero each spacing option separately? – alexis Mar 06 '13 at 14:26
  • @alexis: There is no indention. Can you add an example to your previous answer or ask a new one to show the issue. I can't reproduce it. – Marco Daniel Mar 06 '13 at 18:04
  • I guess it's what you call the innermargins (innerleftmargin, innertopmargin, etc.), which are non-zero by default according to the documentation. Do I need to zero each one separately? (My example uses adjustbox, so the colored text is exactly as wide as the surrounding text.) – alexis Mar 10 '13 at 00:43
  • @alexis: Yes. The two length are non zero length. leftmargin and rightmargin are 0pt. – Marco Daniel Mar 10 '13 at 09:56