Multicols 中的 Colorbox

Multicols 中的 Colorbox

所以我正在使用multicols环境,并且我想让列的某些部分的背景有颜色。

但是,我发现这样效果multicols不太好xcolor,彩色句子溢出到了下一列。为什么会这样?tcolorbox在这种情况下效果会更好吗?

例子:

\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

问题并不在于糟糕的互动;大卫在他的评论中提到,\colorbox是牢不可破的;你可以在里面使用\parbox(适当宽度)\colorbox来允许包装,或者你可以使用mdframed包来定义具有彩色背景和顶部规则的环境;以下示例显示了这两种方法:

\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}

在此处输入图片描述

请注意,在第一种情况下,使用您当前的定义,规则和黄色背景之间存在一些空白;如果您想摆脱这个空间,您将需要类似以下内容:

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

相关内容