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 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.
   \MakeFramed {\FrameRestore}
   \noindent\tikz\node[inner sep=1ex, draw=black!20,fill=white,
          anchor=west, overlay] at (0em, 2em) {\normalcolor\sffamily#1};}%

 \newcommand{\answer}[1]{% This is to avoid typing then environment over and over

This is how I implement the code above:

\MakeFramed {\FrameRestore}
\noindent\tikz\node[inner sep=1ex, draw=black!20,fill=white, anchor=west, overlay] at (0em, 2em) {\color{black}\sffamily#1};}%
    \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,
          Hence, $5x\left(x^{-\frac{1}{2}}yz^3\right)^2=5y^2z^6$
    \item Solve $2x+3=-2(4-7x)-2$.
    \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. }

This is the output:enter image description here

As you can see there are several problems:

  1. 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.

  2. 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.

  3. Inconsistency of the environment.

The below is what I intend to achieve:

enter image description here

share|improve this question
Are you stuck with framed or would an mdframed solution also work for you? –  Peter Grill Mar 9 '12 at 16:34
@PeterGrill Would it matter? I sincerely don't mind mdframed. But if it can be done using either or it would help understanding the advanced features of both packages. –  azetina Mar 9 '12 at 16:42
@azetina: In my opinion the package framed is more robust. The package mdframed 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. –  Marco Daniel Mar 9 '12 at 17:11
@PeterGrill Could you recreate what @MarkS.Everitt provided but with the framed package? –  azetina Mar 9 '12 at 19:26
@azetina: Sorry, don't know the framed package, I have been using mdframed. –  Peter Grill Mar 9 '12 at 19:55

2 Answers 2

up vote 5 down vote accepted

Here's a quick and dirty solution with mdframed


\renewrobustcmd\mdfcreateextratikz{\node[black,fill=white,xshift=1cm] at (P-|O) {\mdf@frametitlefont{Answer}};}




enter image description here

share|improve this answer
You can use \color{\mdf@frametitlefontcolor}. The benefit you can use the option frametitlefontcolor. Maybe also for draw=\mdf@frametitlefontcolor –  Marco Daniel Mar 9 '12 at 17:06
Thanks. I was just looking through the manual for that. –  qubyte Mar 9 '12 at 17:08
It doesn't like draw=\mdf@frametitlefontcolor with the title node though. –  qubyte Mar 9 '12 at 17:12
@azetina: I am the author of mdframed not framed ;-) –  Marco Daniel Mar 9 '12 at 17:23
If ou mean that you want the width of the text in the box to be the same as the width of the text outside the box, then you can set the margins. See the updated answer. –  qubyte Mar 10 '12 at 4:54

An example using tcolorbox for framed boxes.


    enhanced, title=Answer, colframe=red, 
    colback=white, sharp corners, colupper=red,
    attach boxed title to top left = {xshift=5mm,yshift=-2.5mm} ,
    boxed title style={size=small, colframe=gray, sharp corners,colback=white},


\item Is $5x\left(x^{-\frac{1}{2}}yz^3\right)^2$ equal to $5y^2z^6$?

Let us simplify $5x\left(x^{-\frac{1}{2}}yz^3\right)^2$ and verify whether or not is equal to $5y^2z^6$. Thus,
\[\begin{split}5x\left(x^{-\frac{1}{2}}yz^3\right)^2 & = 5x(x^{-1}y^2z^6)\\
& = 5y^2z^6\end{split}\]
Hence, $5x\left(x^{-\frac{1}{2}}yz^3\right)^2 = 5y^2z^6$


enter image description here

share|improve this answer

Your Answer


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.