Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am solving some exercises for my master program. I am including question and answer in a theorem environment. I would also like to have the question inside a gray box. The thing is that the gray box isn't placed well, as indicated in the following figure.

What I would like to have, concerning the gray box, the second line within gray box to start bellow Exercise 1. My code is

\documentclass[a4paper,11pt]{article}
\usepackage{kerkis}
\usepackage{amsmath}
\usepackage{xcolor}
\newcommand*{\mybox}[2]{\colorbox{#1!30}{\parbox{.98\linewidth}{#2}}}
\newtheorem{exe}{Exercise}
\begin{document}
\begin{exe}\mybox{gray}{Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla }
\end{exe}
\end{document}

Any ideas on that?

share|improve this question
    
I suppose that \parbox[t]{.98\linewidth} might do better. –  egreg Dec 9 '12 at 10:48
    
@egreg: Thank you very much for your answer! Perhaps I didn't described it well. I would like the second line to begin under Exercise 1. –  Thanos Dec 9 '12 at 11:05
add comment

2 Answers

up vote 2 down vote accepted

Here's one possible solution such that the environment even admits page breaks (feel free to make the adjustments that best suit your needs):

\documentclass{article}
\usepackage{amsthm}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{thmtools}
\usepackage{lipsum}

\declaretheoremstyle[
  headpunct=,
  headformat={\hspace*{-10pt}\colorbox{white}{\hspace*{6pt}\NAME~\NUMBER.}},
  preheadhook={\begin{mdframed}[%
    backgroundcolor=gray!60,
    hidealllines=true,
    innertopmargin=0pt,
    skipabove=\topsep,skipbelow=\topsep]},
  prefoothook=\end{mdframed}
]{mystyle}
\declaretheorem[style=mystyle,name=Exercise]{exe}

\begin{document}

\lipsum[4]
\begin{exe}
\lipsum[4]
\end{exe}
\lipsum[4]

\end{document}

enter image description here

share|improve this answer
    
That was exactly what I wanted to do!!! Thank you so much! –  Thanos Dec 9 '12 at 13:08
    
I was trying to make an example just to show how bad it appears. ;-) –  egreg Dec 9 '12 at 13:13
    
@egreg: You really think so? What do you think is the best? The thing is that I want to highlight somehow the question, and give the solution bellow... –  Thanos Dec 9 '12 at 13:15
add comment

Is this what you have in mind?

\documentclass[a4paper,11pt]{article}
\usepackage{kerkis}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{lipsum} % for mock text

\newtheorem{exeX}{Exercise}
\newsavebox{\exebox}
\newenvironment{exe}[1]
 {\newcommand{\execolor}{#1}%
  \begin{exeX}\mbox{}\\*[3pt]
  \begin{lrbox}{\exebox}
  \begin{minipage}{\dimexpr\linewidth-2\fboxsep\relax}}
 {\end{minipage}\end{lrbox}\colorbox{\execolor}{\usebox{\exebox}}\end{exeX}}


\begin{document}

\begin{exe}{red}
\lipsum*[2]
\end{exe}

\end{document}

The minipage and the inner theorem environment, together with lrbox avoid the need of stating \mybox inside the exe environment; the color should be specified as the argument to exe.

Note that the minipage width is computed so that it fills correctly the line width.

enter image description here

share|improve this answer
    
Thank you so much for your answer! Actully I would like the first line to be next to Exercise 1 and the second line to begin bellow E in Exercise 1. –  Thanos Dec 9 '12 at 11:25
    
@Thanos Should Exercise 1 be on the same colored background? –  egreg Dec 9 '12 at 11:27
    
No. That is exactly what I was trying to achieve... –  Thanos Dec 9 '12 at 11:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.