[Tex/LaTex] Setting background color of minipage without overflow

colorminipage

I would like to set a color to the leftmost minipage in the following code:

\usepackage{xcolor}
\fcolorbox{red}{gray}{
  \begin{minipage}{0.33\textwidth}
  some text
  \end{minipage}
}

\begin{minipage}{0.67\textwidth}
some other text
\end{minipage}

but the leftmost minipage is displayed over the whole page and the rightmost appears on the second page (given there is enough text in both). I read that one should set innerleftmargin, innerrightmargin and paragraph parameters to 0, but that didn't work either. What is the right way to do it?

Best Answer

You need to take into account \fboxsep (the horizontal length between the frame and the contents) and \fboxrule (the thickness of the rules used by the frame); since the length \fboxsep is applied to the left and right, you need to subtract two of those lengths, as well as two \fboxrules. Also, remove spurious blank spaces (a change of line in the code is tantamount to one space) from your code and blank lines (a blank line in the code ends the previous paragraph, so your second minipage starts a new paragraph). Using e-TeX's \dimexpr you can perform basic arithmetics with lengths:

\documentclass{article}
\usepackage{xcolor}

\begin{document}

\noindent\fcolorbox{red}{gray}{%
\begin{minipage}{\dimexpr0.33\textwidth-2\fboxrule-2\fboxsep\relax}
some text
\end{minipage}}%
\begin{minipage}{0.67\textwidth}
some other text
\end{minipage}

\end{document}