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'm trying to write a collection of problems and their solutions for my course's textbook. I've created an exercise environment. I'm currently using the mdframed package to create a leftbar that spans the exercise descriptions.

Here are some of my issues and questions:

  • Is it possible to create an exercise environment like mine so that "Exercise ---" appears on the same line as the exercise description (so no line breaks)?
  • Should the leftbar in my example be changed any? Does it go too far above/below the text? It seems like the bar goes farther above the text than it does below the text.
  • How would I change my example so that instead of a leftbar, the entire exercise environment (not including the solutions) is shaded a light gray?

    \documentclass[11pt]{article}
    
    \usepackage{amsmath,amssymb,amsfonts}
    \usepackage{color, linegoal}
    \usepackage{amsthm,array}
    \usepackage[framemethod=tikz]{mdframed}
    \usepackage[margin=1.2in]{geometry}
    
    % Exercise environment
    \definecolor{ExerciseColor}{gray}{0.65}
    
    \newenvironment{exercise}[1]
    {\mdfsetup{
    skipabove=\topsep,
    skipbelow=\topsep,
    innertopmargin=0pt,
    innerbottommargin=4pt,
    leftmargin=-13pt,
    splitbottomskip=0ex,
    splittopskip=0ex,
    topline=false,
    leftline=true,
    bottomline=false,
    rightline=false,
    innerrightmargin=0pt,
    innerlinewidth=2pt,
    font=\normalfont,
    frametitle={\textbf{Exercise #1.}}, 
    linecolor=ExerciseColor,
    }
    \begin{mdframed}%
    }
    {\end{mdframed}}
    
    % Solution environment
    \newenvironment{solution}{\begin{proof}[\itshape Solution]}{\end{proof}}
    
    \begin{document}
    \title{Title}
    \author{Author}
    \date{\today}
    \maketitle
    
    \section*{\centering \bfseries \S 1.3 Exercises}
    
    \begin{exercise}{1.3.1}
    Here is an exercise. Here is an exercise. Here is an exercise. Here is an exercise. Here is an exercise. Here is an equation:
    \[
    a + b = c.
    \]
    Here is an exercise. 
    \end{exercise}
    \begin{solution}
    Here is my solution. Here is an equation:
    \[
    1+2 = 3.
    \]
    Here is my solution. 
    \end{solution}
    
    \begin{exercise}{1.3.5}
    Here is an exercise. Here is an exercise. Here is an exercise. Here is an exercise. Here is an exercise. Here is an equation:
    \[
    a + b = c.
    \]
    Here is an exercise. 
    \end{exercise}
    \begin{solution}
    Here is my solution.
    \end{solution}
    
    \end{document}
    

Output:

share|improve this question
    
Why are you numbering the exercises manually? –  Gonzalo Medina Jun 13 '13 at 18:03
    
I want a collection of solved problems to distribute to students. I only intend on solving a select few of the exercises in the textbook. –  Noob Jun 13 '13 at 18:05

2 Answers 2

up vote 5 down vote accepted

Here's another similar approach, but this time using the interface provided by the thmtools package; the idea is to define the structure as a theorem-like one, and use the mdframed key provided by thmtools:

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage[framemethod=tikz]{mdframed}

\definecolor{ExerciseColor}{gray}{0.65}

\declaretheoremstyle[
headfont=\normalfont\bfseries,
notefont=\mdseries, notebraces={(}{)},
bodyfont=\normalfont,
postheadspace=0.5em,
mdframed={
  skipabove=\topsep,
  skipbelow=\topsep,
  hidealllines=true,
  backgroundcolor={ExerciseColor!20},
  innerleftmargin=0pt,
  innerrightmargin=0pt}
]{mystyle}
\declaretheorem[style=mystyle,name=Exercise]{exer}
\newenvironment{exercise}[1]
  {\renewcommand\theexer{#1}\begin{exer}}
  {\end{exer}}

\begin{document}

\begin{exercise}{1.3.5}
Here is an exercise. Here is an exercise. Here is an exercise. Here is an exercise. Here is an exercise. Here is an equation:
\[
a + b = c.
\]
Here is an exercise. 
\end{exercise}

\begin{exercise}{1.3.7}
Here is an exercise. Here is an exercise. Here is an exercise. Here is an exercise. Here is an exercise. Here is an equation:
\[
a + b = c.
\]
Here is an exercise. 
\end{exercise}

\end{document}

enter image description here

share|improve this answer
    
Did you notice my message in the chat. I uploaded the new version with a lot of new hooks ;-) –  Marco Daniel Jun 13 '13 at 18:29

A lot of questions.

  • Is it possible to create an exercise environment like mine so that "Exercise ---" appears on the same line as the exercise description (so no line breaks)?

In this case I suggest to define a normal theorem environment with newtheorem and surround the environment with mdframed: \definecolor{ExerciseColor}{gray}{0.65}

\newtheorem{exercise}{Exercise}
\mdfdefinestyle{exercise}{%
skipabove=\topsep,skipbelow=\topsep,
innertopmargin=0pt,innerbottommargin=4pt,
leftmargin=-13pt,splitbottomskip=0ex,
splittopskip=0ex,topline=false,leftline=true,
bottomline=false,rightline=false,
innerrightmargin=0pt,innerlinewidth=2pt,
font=\normalfont,linecolor=ExerciseColor,
}
\surroundwithmdframed[style=exercise]{exercise}

Of course the numbering scheme of exercise must be modified to your needs. I don't know why you are using a manual count.

  • Should the leftbar in my example be changed any? Does it go too far above/below the text? It seems like the bar goes farther above the text than it does below the text.

This can be fixed if you use ntheorem and mdframed with the option ntheorem=true to declare your theorems.

\usepackage[amsmath]{ntheorem}
\usepackage[framemethod=tikz]{mdframed}\newtheorem{exercise}{Exercise}
\mdfdefinestyle{exercise}{%
skipabove=\topsep,skipbelow=\topsep,
innertopmargin=0pt,innerbottommargin=4pt,
leftmargin=-13pt,splitbottomskip=0ex,
splittopskip=0ex,topline=false,leftline=true,
bottomline=false,rightline=false,
innerrightmargin=0pt,innerlinewidth=2pt,
font=\normalfont,linecolor=ExerciseColor,ntheorem=true,
}
\surroundwithmdframed[style=exercise]{exercise}
  • How would I change my example so that instead of a leftbar, the entire exercise environment (not including the solutions) is shaded a light gray?

Also with \surroundwithmdframed.

share|improve this answer

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.