I am trying to write a simple environment for the solutions of assessments I give. The code below defines such an environment:
\newenvironment{answerenv}[1][Answer]{% Sets the default "Answer" but can be changed to be reused.
\vskip1.5\baselineskip
\MakeFramed {\FrameRestore}
\noindent\tikz\node[inner sep=1ex, draw=black!20,fill=white,
anchor=west, overlay] at (0em, 2em) {\normalcolor\sffamily#1};}%
{\endMakeFramed}
\newcommand{\answer}[1]{% This is to avoid typing then environment over and over
{\color{red}%
\begin{answerenv}
{#1}
\end{answerenv}
}}
This is how I implement the code above:
\documentclass[letterpaper]{article}
\usepackage[top=1.5cm,bottom=1.5cm,left=1.5cm,right=1.5cm]{geometry}
\usepackage{amsmath,amssymb,enumitem}
\usepackage[dvipsnames]{xcolor}
\usepackage{framed,tikz}
\parindent0pt
\newenvironment{answerenv}[1][Answer]{%
\vskip1.5\baselineskip
\MakeFramed {\FrameRestore}
\noindent\tikz\node[inner sep=1ex, draw=black!20,fill=white, anchor=west, overlay] at (0em, 2em) {\color{black}\sffamily#1};}%
{\endMakeFramed}
\newcommand{\answer}[1]{%
{\color{red}%
\begin{answerenv}
{#1}
\end{answerenv}
}}
\begin{document}
\begin{enumerate}
\item Is $5x\left(x^{-\frac{1}{2}}yz^3\right)^2$ equal to $5y^2z^6$?
\answer{Let us simplify $5x\left(x^{-\frac{1}{2}}yz^3\right)^2$ and verify whether or not it is equal to $5y^2z^6$. Thus,
\begin{align*}
5x\left(x^{-\frac{1}{2}}yz^3\right)^2&=5x\left(x^{-1}y^2z^6\right)\\
&=5y^2z^6
\end{align*}
Hence, $5x\left(x^{-\frac{1}{2}}yz^3\right)^2=5y^2z^6$
}
\item Solve $2x+3=-2(4-7x)-2$.
\answer{
\begin{align*}
2x+3&=-2(4-7x)-2\\
2x+3&=-8+14x-2\\
14x-2x&=3+8+2\\
12x&=13\\
x&=\frac{13}{12}
\end{align*}}
\item Is division commutative? Support your conclusion with an example.
\answer{If division is commutative then this means that \[a\div b=b\div a\] is true. It will suffice to say that \[4=8\div 2\neq 2\div 8=0.25\].}
\item Is 1 prime or composite?
\answer{1 is neither prime nor composite. If it were prime then it would have only two distinct factors, one and itself, which does not have. To be composite would mean to have more than two factors and thus does not satisfy any of the above. }
\end{enumerate}
\end{document}
This is the output:
As you can see there are several problems:
In the first
item
the word Answer is more or less where I would want it but you can still see the red frame. I would want the box containing the word Answer to be centered vertically with the frame line.The red frame is not indented with the enumerate environment. Note I would like the content in the environment to be indented as shown below.
Inconsistency of the environment.
The below is what I intend to achieve:
framed
or would anmdframed
solution also work for you?mdframed
. But if it can be done using either or it would help understanding the advanced features of both packages.framed
is more robust. The packagemdframed
is growing from version to version :-(. An other difference is the bottom/top line.mdframed
doesn't draw a bottom/top line at splitted frames.framed
package?framed
package, I have been usingmdframed
.