# how to eliminate the horizontal spacing of the frame in the framed package?

I'm using the framed package and I found it difficult to adjust the width of a frame to adapt to the current environment, e.g. if I use a shaded environment within the enumerate environment, I will get a frame box as wide as the current page (actually slightly wider) -- it will not indent accordingly; then I found a solution to make the frame of the same width as the current environment, but a new problem arised: there seems to be extra horizontal spacing inside the frame. A MWE is below:

\documentclass{article}
\usepackage{framed,color}
\definecolor{shadecolor}{rgb}{.9, .9, .9}
\newenvironment{myframe}{%
\def\FrameCommand{\fboxsep=\FrameSep \colorbox{shadecolor}}%
\MakeFramed {\hsize\linewidth\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}

\begin{document}
test a frame within an enumerate environment
\begin{enumerate}
\item abc
\begin{myframe}
def
\end{myframe}
\end{enumerate}
\end{document}


Can anybody tell me how to eliminate the extra left padding so that the content of the frame can align to the list item? Thanks a lot!

A screenshot is here:

• We'd like to keep answers separate from questions, so you should write a separate answer instead of editing your answer into the question. Self-answers are perfectly admissible, and a well-written answer may earn you additional reputation. – Werner Nov 8 '11 at 21:29
• Thanks for the reminder! I tried to answer directly but got a message box, and I thought it recommended me to edit my original post. I will extract the answer out then. – Yihui Xie Nov 9 '11 at 1:45

## 3 Answers

I asked the author of the framed package, and his solution worked for me (although I do not really understand it):

\documentclass{article}
\usepackage{framed,color}
\definecolor{shadecolor}{rgb}{.9, .9, .9}
\makeatletter

\renewenvironment{snugshade}{%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed}

\makeatother

\begin{document}
test a frame within an enumerate environment test a frame within an enumerate environment
\begin{enumerate}
\item abc
\begin{snugshade}
def
\end{snugshade}
\end{enumerate}
\end{document}


I suggest using the mdframed package:

\documentclass{article}
\usepackage{xcolor} % better than color
\usepackage{mdframed}
\definecolor{shadecolor}{gray}{.9}

\begin{document}
test a frame within an enumerate environment
\begin{enumerate}
\item abc
\begin{mdframed}[
backgroundcolor=shadecolor,
leftmargin=\dimexpr\leftmargin-2pt\relax,
innerleftmargin=1.6pt,
innertopmargin=2pt,
skipabove=3pt,skipbelow=3pt
]
def
\end{mdframed}
\end{enumerate}
\end{document}


Appending tilde after \vspace{-4mm} removes the problem:

\item abc \\[-8mm]~


or

 \item abc \vspace{-4mm}~


Here is the complete text

  \documentclass{article}
\usepackage{framed,color}
\definecolor{shadecolor}{rgb}{.9, .9, .9}
\newenvironment{myframe}{%
\def\FrameCommand{\fboxsep=\FrameSep \colorbox{shadecolor}}%
\MakeFramed {\hsize\linewidth\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}

\begin{document}
test a frame within an enumerate environment
\begin{enumerate}
\item abc\\[-8mm]~
\begin{myframe}
def
\end{myframe}
\end{enumerate}
\end{document}