The syntax is \myFloat[optArgs left]{text left}[optArgs right]{text right}{caption}{label}
the last two can be empty.
\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}% only for demo
\usepackage{xcolor,mdframed,caption}
\makeatletter
\def\myFLoat{\@ifnextchar[{\myFloat@i}{\myFloat@i[]}}
\def\myFloat@i[#1]#2{\@ifnextchar[%
{\myFloat@ii[#1]{#2}}%
{\myFloat@ii[#1]{#2}[]}}
\def\myFloat@ii[#1]#2[#3]#4#5#6{\par\medskip%
% \mdfsetup{innerleftmargin=10pt,innerrightmargin=10pt}%
\noindent\begin{minipage}[t]{0.475\textwidth}\leavevmode
\begin{mdframed}[#1]#2\end{mdframed}
\end{minipage}\hfill
\begin{minipage}[t]{0.475\textwidth}\leavevmode
\begin{mdframed}[#3]#4\end{mdframed}
\end{minipage}
\ifx\relax#5\relax\else\captionof{figure}{#5}\fi%
\ifx\relax#6\relax\else\label{#6}\fi\par}
\makeatother
\begin{document}
\blindtext
\myFLoat[linecolor=blue,linewidth=2pt,backgroundcolor=yellow]{\blindtext}%
[linecolor=red,linewidth=3pt]{\blindtext}%
{my caption for this float}{myLabel}
as seen in my Figure~\ref{myLabel} it works.
\blindtext
\myFLoat[linecolor=blue,linewidth=2pt,backgroundcolor=yellow]{\blindtext}%
[linecolor=red,linewidth=3pt]{\begin{align}y=\int\limits_1^2 f(x)dx\end{align}}%
{my caption for this float}{myLabelB}
\end{document}
Basic Solution:
I would recommend that you use the mdframed
package instead (as it will work across page breaks), and use the resume*
option with lists via the enumitem
package:
Better Solution:
As @MarcoDaniel suggested, a better looking solution is to use the frametitle
option with mdframed
.
\newenvironment{Question}[1]{
\begin{mdframed}[
frametitle={#1},
frametitlerule=true,
frametitlebackgroundcolor=red!20,
frametitlebelowskip=2pt,
innerlinewidth=1.0pt
]
}{
\end{mdframed}
}
With this you simply enclose the part you wanted shaded in the first parameter to the Question
environment, and the body of this environment is the answer, i.e., the part you don't wanted shaded. This yields:
- For some reason there is a display issue with this. There are lines at the top and bottom but with increasing magnification from
150%
and incrementing by 1%
with Acrobat Reader 10.1.2 on Mac different lines appear and disappear. I have updated this to use framemthod=tikz
and innerlinewidth=1.0pt
option as this make this display problem less noticeable (but problem is still there).
Even Better Solution (Custom Counter):
If you are only using the enumerate
to create a numbered list, you could simplify things even further by using a custom counter. This is also courtesy of @MarcoDaniel.
Notes:
Code: Basic Solution
\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{mdframed}
\newmdenv[backgroundcolor=yellow]{shaded}
\begin{document}
\begin{shaded}
Starting text...
\begin{enumerate}[series=MyQuestions,leftmargin=*]
\item This is the first problem.
\end{enumerate}
\end{shaded}
\begin{enumerate}[resume*=MyQuestions]
\item[] This is the first solution (not shaded)
\end{enumerate}
%
\begin{shaded}
\begin{enumerate}[resume*=MyQuestions]
\item This is the second problem.
\end{enumerate}
\end{shaded}
\begin{enumerate}[resume*=MyQuestions]
\item[] Second solution (not shaded)
\end{enumerate}
\end{document}
Code: Better Solution:
\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage[framemethod=tikz]{mdframed}
\newenvironment{Question}[1]{
\begin{mdframed}[
frametitle={#1},
frametitlerule=true,
frametitlebackgroundcolor=red!20,
frametitlebelowskip=2pt,
innerlinewidth=1.0pt
]
}{
\end{mdframed}
}
\begin{document}
\begin{Question}{
Starting text...
\begin{enumerate}[series=MyQuestions,leftmargin=*]
\item This is the first problem.
\end{enumerate}
}
This is the first solution (not shaded)
\end{Question}
\bigskip
\begin{Question}{
\begin{enumerate}[resume*=MyQuestions]
\item This is the second problem.
\end{enumerate}
}
\begin{enumerate}[resume*=MyQuestions]
\item[] Second solution (not shaded)
\end{enumerate}
\end{Question}
\end{document}
Code: Even Better Solution (Custom Counter):
\documentclass{article}
\usepackage{mdframed}
\mdfdefinestyle{QuestionsStyle}{%
frametitlerule=true,
frametitlebackgroundcolor=yellow,
linewidth=2pt,
frametitlerulewidth=1pt,
innerleftmargin=30pt,
frametitlebelowskip=.5\topskip,
innertopmargin=\topskip
}
\newcounter{QuestionCounter}
\setcounter{QuestionCounter}{0}
\newrobustcmd*\SetQuestionNum{\mbox{}\llap{\stepcounter{QuestionCounter}\theQuestionCounter\hspace*{10pt}}}
\newmdenv[style=QuestionsStyle]{Question}
\begin{document}
\begin{Question}[frametitle={
Some Text\\
\\
\SetQuestionNum This is the first problem.}]
This is the first solution (not shaded)
\end{Question}
\bigskip
\begin{Question}[frametitle={
\SetQuestionNum This is the second problem.}]
Second solution (not shaded)
\end{Question}
\end{document}
Best Answer
The package
framed
uses the command\centerline
which always starts at the outer left margin. I recommend to switch to the new packagetcolorbox
.