10

I'm writing solutions to an exam. For one of the multiple-choice problems, I've decided to shade all correct answers using colorbox. By the way, more than one choice for the problem may be correct (not that LaTeX cares).

I have two issues: (1) colorbox makes my text run off the page; (2) Even when breaking the line, there is a thin white space (which should be shaded) between the lines. Moreover, the width of the shaded lines are different.

How can I make my colorbox break lines automatically? How can I make the entire correct answer (even after a line break) be shaded? Is there a nicer, prettier way to shade answers to multiple-choice questions?

Below is an example.

\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{color}

\definecolor{SolutionColor}{gray}{0.85}

\begin{document}

Choose all correct statements.
\begin{enumerate}
\item \colorbox{SolutionColor}{\bfseries Here is a true sentence.}
\item \colorbox{SolutionColor}{\bfseries If the rank of a $\boldsymbol{2 \times 2}$ matrix $\boldsymbol{A}$ is 1, then one can always find a vector $\boldsymbol{\mathbf{b} \in \mathbb{R}^2}$ so that $\boldsymbol{A \mathbf{x} = \mathbf{b}}$ does not have a solution.}
\end{enumerate}

Choose all correct statements.
\begin{enumerate}
\item \colorbox{SolutionColor}{\bfseries Here is a true sentence.}
\item \colorbox{SolutionColor}{\bfseries If the rank of a $\boldsymbol{2 \times 2}$ matrix $\boldsymbol{A}$ is 1, then one can always find a vector $\boldsymbol{\mathbf{b} \in \mathbb{R}^2}$} \par \colorbox{SolutionColor}{\bfseries so that $\boldsymbol{A \mathbf{x} = \mathbf{b}}$ does not have a solution.}
\end{enumerate}


\end{document}

enter image description here

Noob
  • 450
  • 4
  • 13

2 Answers2

7

A box (here, color box) will not break across lines. You can take shelter inside a parbox for this:

\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{color,linegoal}

\definecolor{SolutionColor}{gray}{0.85}

\begin{document}

Choose all correct statements.
\begin{enumerate}
\item \colorbox{SolutionColor}{\parbox[t]{\linegoal}{\bfseries Here is a true sentence.}}
\item \colorbox{SolutionColor}{\parbox[t]{\linegoal}{\bfseries If the rank of a $\boldsymbol{2 \times 2}$ matrix $\boldsymbol{A}$ is 1, then one can always find a vector $\boldsymbol{\mathbf{b} \in \mathbb{R}^2}$ so that $\boldsymbol{A \mathbf{x} = \mathbf{b}}$ does not have a solution.}}
\end{enumerate}

Choose all correct statements.
\begin{enumerate}
\item \colorbox{SolutionColor}{\parbox[t]{\linegoal}{\bfseries Here is a true sentence.}}
\item \colorbox{SolutionColor}{\parbox[t]{\linegoal}{\bfseries If the rank of a $\boldsymbol{2 \times 2}$ matrix $\boldsymbol{A}$ is 1, then one can always find a vector $\boldsymbol{\mathbf{b} \in \mathbb{R}^2}$ \par so that $\boldsymbol{A \mathbf{x} = \mathbf{b}}$ does not have a solution.}}
\end{enumerate}


\end{document}

enter image description here

  • This looks really great. The only other problem I have is this: I'm using the exam document class, so the margins are different. How do I set the length of the linegoal to the margins used with exam doc? – Noob Mar 19 '13 at 01:02
  • @Nathan linegoal computes the remaining length of the line automatically. Just use it and see. –  Mar 19 '13 at 01:16
6

Here's another option using the mdframed package; the command \mysolu receives as mandatory argument the colored box content and typesets it with the desired specifications:

\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{xcolor}
\usepackage{mdframed}

\definecolor{SolutionColor}{gray}{0.85}

\makeatletter
\renewrobustcmd*\mdf@makebox@out[2][\linewidth]{%
 \vskip-\baselineskip\noindent\hb@xt@\z@{%
    \noindent\makebox[\dimexpr #1\relax][l]{#2}}%
}%
\makeatother
\newmdenv[
  backgroundcolor=SolutionColor,
  hidealllines=true,
  innerleftmargin=3pt,
  innerrightmargin=3pt,
  skipabove=0pt,
  font=\bfseries
]{mysol}

\newcommand\mysolu[2][]{%
  \begin{mysol}[#1]#2\end{mysol}}

\begin{document}

\noindent Choose all correct statements.
\begin{enumerate}
\item\mysolu{Here is a true sentence.}
\item\mysolu{If the rank of a $\boldsymbol{2 \times 2}$ matrix $\boldsymbol{A}$ is 1, then one can always find a vector $\boldsymbol{\mathbf{b} \in \mathbb{R}^2}$ so that $\boldsymbol{A \mathbf{x} = \mathbf{b}}$ does not have a solution.}
\end{enumerate}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128