[Tex/LaTex] Colorbox inside Multicols

colorline-breakingmulticol

So I am using the multicols environment and I want to have the background of some portions of the column colored.

However, I see that multicols does not play well with xcolor and colored sentences overflow in the next column.
Why is that happening? Would tcolorbox work better in this case?

Example:

\documentclass[10pt,landscape, fleqn]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage{ifthen}
\usepackage[landscape]{geometry}
\usepackage{amsmath,amsthm,amsfonts,amssymb}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{graphicx,overpic}
\usepackage{hyperref}
\usepackage{listings}

%problem statement                                                                                                                                                                                                                                                             
\newcommand{\problem}[1] {
  \rule{1\linewidth}{0.25pt}\\
   \colorbox{yellow}{#1}
}

% -----------------------------------------------------------------------                                                                                                                                                                                                      

\begin{document}
\raggedright
\footnotesize
\begin{multicols*}{3}


\setlength{\premulticols}{1pt}
\setlength{\postmulticols}{1pt}
\setlength{\multicolsep}{1pt}
\setlength{\columnsep}{2pt}

\problem{This is a long sentence that will unfortunately cross the multicols boundaries.}

This is a long sentence that will not cross the multicols boundaries.


\end{multicols*}
\end{document}

Best Answer

The problem is not with a bad interaction; as David mentioned in his comment, \colorbox is unbreakable; you can use a \parbox (of the appropriate width) inside the \colorbox to allow wrapping or you can use the mdframed package to define an environment with the colored background and the top rule; the following example shows both approaches:

\documentclass[10pt,landscape, fleqn]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage{ifthen}
\usepackage[landscape]{geometry}
\usepackage{amsmath,amsthm,amsfonts,amssymb}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{graphicx,overpic}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{hyperref}

%problem statement                                                                                                                                                                                                                                                             
\newcommand{\problem}[1]{%
  \rule{1\columnwidth}{0.25pt}\\
   \colorbox{yellow}{\parbox{\dimexpr\columnwidth-2\fboxsep-2\fboxrule\relax}{#1}}
}

\newmdenv[
  backgroundcolor=yellow,
  skipabove=\topsep,
  skipbelow=\topsep,
  linewidth=0.25pt,
  topline=true,
  bottomline=false,
  leftline=false,
  rightline=false,
]{problemi}


% -----------------------------------------------------------------------                                                                                                                                                                                                      

\begin{document}
\raggedright
\footnotesize
\begin{multicols*}{3}
\setlength{\premulticols}{1pt}
\setlength{\postmulticols}{1pt}
\setlength{\multicolsep}{1pt}
\setlength{\columnsep}{2pt}

\problem{This is a long sentence that will not cross the multicols boundaries.}

This is a long sentence that will not cross the multicols boundaries.

\begin{problemi}
This is a long sentence that will not cross the multicols boundaries built using the \texttt{mdframed} package.
\end{problemi}
\end{multicols*}

\end{document}

enter image description here

Notice that there's some white space between the rule and the yellow background in the first case, using your current definition; if you want to get rid of this space, you'll need something like:

\newcommand{\problem}[1]{%
  \rule{1\columnwidth}{0.25pt}\\[0.25pt]\nointerlineskip
   \colorbox{yellow}{\parbox{\dimexpr\columnwidth-2\fboxsep-2\fboxrule\relax}{#1}}
}
Related Question