[Tex/LaTex] Box or sidebar for additional text

boxesfloats

I'm not sure of the name of what I need. I would like to set some text in a box or sidebar, as I've seen in some textbooks. The main text will refer the reader to the sidebar for more details. I would like this shaded. There is too much text for marginpar. I could just use a float figure? I'd prefer it to be half the width of the page though. Is there an easy way to achieve this? I'm not sure what search terms to use to find out about this.

Best Answer

You can do this, with certain restrictions, using the wrapfig package together with mdframed or tcolorbox. Here's a little example using tcolorbox:

\documentclass{article}
\usepackage{wrapfig}
\usepackage{tcolorbox}
\usepackage{lipsum}

\newenvironment{WrapText}[1][r]
  {\wrapfigure{#1}{0.5\textwidth}\tcolorbox}
  {\endtcolorbox\endwrapfigure}

\newcommand\Text{% some text for the example
tesque cursus luctus mauris. Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum felis. Donec nonummy pellentesque ante. Phasellus adipiscing semper elit. Proin fermentum massa ac quam. Sed diam turpis, molestie vitae, placerat a, molestie nec, leo. Maecenas lacinia. Nam ipsum ligula, eleifend at, accumsan nec, suscipit a, ipsum.}

\begin{document}

\begin{WrapText}
\Text
\end{WrapText}
\lipsum[2]
\lipsum[2]
\begin{WrapText}
\Text
\end{WrapText}
\lipsum[1-3]

\end{document}

enter image description here

And a solution with mdframed:

\documentclass{report}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\newenvironment{WrapText}[1][r]
  {\wrapfigure{#1}{0.5\textwidth}\mdframed[backgroundcolor=gray!20,skipabove=0pt,skipbelow=0pt]}
  {\endmdframed\endwrapfigure}

\newcommand\Text{% some text for the example
tesque cursus luctus mauris. Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum felis. Donec nonummy pellentesque ante. Phasellus adipiscing semper elit. Proin fermentum massa ac quam. Sed diam turpis, molestie vitae, placerat a, molestie nec, leo. Maecenas lacinia. Nam ipsum ligula, eleifend at, accumsan nec, suscipit a, ipsum.}

\begin{document}

\begin{WrapText}
\Text
\end{WrapText}
\lipsum[2]
\lipsum[2]
\begin{WrapText}
\Text
\end{WrapText}
\lipsum[1-3]

\end{document}

enter image description here

Since both mdframed and tcolorbox are highly customizable, you can obtain pretty easily the desired layout for the colored box.

And now, the bad news. Since wrapfig is being used, some restrictions will have to be observed; in particular, the package documentation mentions these:

• The environment should be placed so as to not run over a page break.

• The environment must not be placed in special places like lists.

• For esthetic reasons, only plain text should wrap around the figure. Section titles and big equations look bad; lists are bad if the figure is on the left. (All these function properly, they just don’t look very good.) Small equations look fine.

• It is convenient to begin the environment between paragraphs, but if you want placement in the middle of a paragraph, you must put the environment between two words where there is a natural line break.