[Tex/LaTex] New environment using mdframed

environmentsmdframed

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:

Best Answer

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

Related Question