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 themdframed
package to define an environment with the colored background and the top rule; the following example shows both approaches: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: