3

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}
1
  • 3
    \colorbox is like \mbox and produces a box which is by definition unbreakable and does not wrap over the line. This is unrelated to multicols you would see the same in any paragraph. Mar 11, 2013 at 20:22

1 Answer 1

5

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}}
}
2
  • Thank you for the explanation. Do you know if it is the same reason why you can not have floats inside multicols?
    – igon
    Mar 11, 2013 at 20:59
  • @igon no. As mentioned your problem has nothing to do with multicols whatsoever, just with the plain fact that a \colorboxdoes not do linebreaking inside. The reason that multicols doesn't support column floats is twofold: a) the column size can vary but the float size is fixed and b) balancing column data and float placement kind of contradict each other without very elaborate algorithms beyond what is easily possible in TeX. Mar 12, 2013 at 12:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .