10

I'm trying to write up solutions where I include the original problem text, in a light gray shaded box. I had been creating the shading with the 'framed' package shaded environment, but some of the problems have enumerated parts, with some text before the parts. I would like to have the starting text and the first part continuously shaded, but apparently the shaded environment can't cross a \begin{enumerate}. So what I have right now is

\begin{shaded}Starting text...\end{shaded}
\begin{enumerate}
\begin{shaded}\item This is the first problem.\end{shaded}
This is the first solution.
\begin{shaded}\item This is the second problem.\end{shaded}
Second solution.
\end{enumerate}

Is there a way to have the shading continuous instead of that first gap?

1
  • Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Commented Jan 31, 2012 at 19:14

1 Answer 1

11

Basic Solution:

I would recommend that you use the mdframed package instead (as it will work across page breaks), and use the resume* option with lists via the enumitem package:

enter image description here


Better Solution:

As @MarcoDaniel suggested, a better looking solution is to use the frametitle option with mdframed.

\newenvironment{Question}[1]{
    \begin{mdframed}[
       frametitle={#1},
       frametitlerule=true,
       frametitlebackgroundcolor=red!20,
       frametitlebelowskip=2pt,
       innerlinewidth=1.0pt
    ]
}{
    \end{mdframed}
}

With this you simply enclose the part you wanted shaded in the first parameter to the Question environment, and the body of this environment is the answer, i.e., the part you don't wanted shaded. This yields:

enter image description here

  • For some reason there is a display issue with this. There are lines at the top and bottom but with increasing magnification from 150% and incrementing by 1% with Acrobat Reader 10.1.2 on Mac different lines appear and disappear. I have updated this to use framemthod=tikz and innerlinewidth=1.0pt option as this make this display problem less noticeable (but problem is still there).

Even Better Solution (Custom Counter):

If you are only using the enumerate to create a numbered list, you could simplify things even further by using a custom counter. This is also courtesy of @MarcoDaniel.

enter image description here

Notes:


Code: Basic Solution

\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{mdframed}

\newmdenv[backgroundcolor=yellow]{shaded}

\begin{document}
\begin{shaded}
    Starting text...
    \begin{enumerate}[series=MyQuestions,leftmargin=*]
    \item This is the first problem.
    \end{enumerate}
\end{shaded}
\begin{enumerate}[resume*=MyQuestions]
\item[] This is the first solution (not shaded)
\end{enumerate}
%
\begin{shaded}
    \begin{enumerate}[resume*=MyQuestions]
    \item This is the second problem.
    \end{enumerate}
\end{shaded}
\begin{enumerate}[resume*=MyQuestions]
\item[] Second solution (not shaded)
\end{enumerate}
\end{document}

Code: Better Solution:

\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage[framemethod=tikz]{mdframed}

\newenvironment{Question}[1]{
    \begin{mdframed}[
        frametitle={#1},
        frametitlerule=true,
        frametitlebackgroundcolor=red!20,
        frametitlebelowskip=2pt,
        innerlinewidth=1.0pt
    ]
}{
    \end{mdframed}
}

\begin{document}
\begin{Question}{
        Starting text...
        \begin{enumerate}[series=MyQuestions,leftmargin=*]
        \item This is the first problem.
        \end{enumerate}
    }
    This is the first solution (not shaded)
\end{Question}

\bigskip
\begin{Question}{
        \begin{enumerate}[resume*=MyQuestions]
        \item This is the second problem.
        \end{enumerate}
    }
    \begin{enumerate}[resume*=MyQuestions]
    \item[] Second solution (not shaded)
    \end{enumerate}
\end{Question}
\end{document}

Code: Even Better Solution (Custom Counter):

\documentclass{article}
\usepackage{mdframed}
\mdfdefinestyle{QuestionsStyle}{%
    frametitlerule=true,
    frametitlebackgroundcolor=yellow,
    linewidth=2pt,
    frametitlerulewidth=1pt,
    innerleftmargin=30pt,
    frametitlebelowskip=.5\topskip,
    innertopmargin=\topskip
}
\newcounter{QuestionCounter}
\setcounter{QuestionCounter}{0}
\newrobustcmd*\SetQuestionNum{\mbox{}\llap{\stepcounter{QuestionCounter}\theQuestionCounter\hspace*{10pt}}}
\newmdenv[style=QuestionsStyle]{Question}
\begin{document}
\begin{Question}[frametitle={
    Some Text\\
    \\
    \SetQuestionNum This is the first problem.}]
This is the first solution (not shaded)
\end{Question}

\bigskip
\begin{Question}[frametitle={
    \SetQuestionNum This is the second problem.}]
Second solution (not shaded)
\end{Question}
\end{document}
5
  • A solution by using frametitle seems to be possible. So you can set the frametitlebackgroundcolor. Commented Jan 31, 2012 at 19:54
  • @MarcoDaniel: Excellent idea. I have replaced the earlier Better Solution to use frametitle. However, I notice a slight display issue as I pointed out in the solution. Definitely a problem with the Acrobat display as different magnifications show different lines. Commented Jan 31, 2012 at 20:17
  • Have you recognize my example in the chat? Commented Jan 31, 2012 at 20:20
  • Hmm, I can't typeset the 'better solution', I get frametitle undefined. Commented Jan 31, 2012 at 23:12
  • @MicahDombrowski: You probably have an older version of mdframed. I think that was a recent enhancement. Commented Jan 31, 2012 at 23:43

You must log in to answer this question.

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