[Tex/LaTex] Set varwidth to column width with multicol package

boxesmulticolvarwidth

I'm using two columns with the multicol package. I have some text that I put inside a \colorbox as below, and I use varwidth to make it include line breaks.

If the lines are short, the box width expands just enough to include the text. Is there a way to make the box have the width of the column?

Below is the command I use for the box.

\newcommand{\songbox}[1]{
\colorbox{light-gray}{
\begin{varwidth}{\textwidth}
\textit{#1}
\end{varwidth}}}

I'm new to latex, so simplicity would be appreciated.

Sample

Best Answer

You don't want to use varwidth, if your purpose is that the grey box covers the whole column width.

The purpose of varwidth is to make the enclosing box as wide as necessary to encompass the material inside.

\documentclass{article}
\usepackage{xcolor,multicol}

\usepackage{lipsum}

\definecolor{light-gray}{gray}{.85}

\newsavebox{\songboxbox}
\newenvironment{songbox}
 {\begin{lrbox}{\songboxbox}\begin{minipage}{\dimexpr\columnwidth-2\fboxsep}\itshape}
 {\end{minipage}\end{lrbox}%
  \par\medskip\noindent
  \colorbox{light-gray}{\usebox{\songboxbox}}%
  \par\medskip}

\begin{document}

\begin{multicols}{2}
Some text some text some text some text
some text some text some text some text
some text some text some text some text

\begin{songbox}
In this long line the box goes all the way
to the right margin. And it wraps nicely.
\end{songbox}

Some text some text some text some text
some text some text some text some text
some text some text some text some text

\begin{songbox}
This line ends here.

Another line.
\end{songbox}

Now we fill the columns. \lipsum[1-10]

\end{multicols}

\end{document}

The environment form seems better, in my opinion.

enter image description here