[Tex/LaTex] How to create a framed proof attached to a theorem

amsthmenvironmentsmdframedtheorems

I am trying to create a customized "proof" environment attached to a theorem. It looks like this:
What it should look like.

So there is first the theorem, and then a gray bar starts on the inner margin. It ends, when the proof of the theorem is finished. It should also be possible to give a theorem without proof of cause.

The document you see above was created from the following code:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% HEADER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[a4paper,twoside,11pt]{article}
\special{papersize=210mm,297mm}


%% Language %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[USenglish]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern} % Type1-font for non-english texts and characters

%% Math Symbols %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}

%% Other Packages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{color}                          % colors
\usepackage{framed}                         % frames
\usepackage{mdframed}                       % better frames

\usepackage{fancyhdr}                   % Fancy headings

%% Style Definitions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\numberwithin{equation}{section}

% My Theorem Style ============================================================

\newtheoremstyle{myThmStyle}% name of the theorem
{6pt}% habovespacei
{6pt}% hbelowspacei
{\itshape}% font
{}% hindenti
{}% hheadfonti
{}% hheadpuncti
{ }% hheadspacei
{\thmname{\normalfont\large\textsc{#1}\normalsize} \thmnote{#3:} \thmnumber{ \hfill \normalfont(#2)}\\[1pt]}

% Frame Styles ================================================================

\definecolor{thmBgColor}{RGB}{250,250,250}
\definecolor{thmLnColor}{RGB}{200,200,200}

\mdfdefinestyle{MDFStyGrayScreen}{%
    linecolor=thmLnColor,
        backgroundcolor=thmBgColor,
        linewidth=1pt,
        topline=true,
    bottomline=true,
    rightline=false,
        leftline=false,
    outerlinewidth=2pt,
    roundcorner=0pt,
    innertopmargin=4pt, %\baselineskip
    innerbottommargin=4pt, %\baselineskip,
    innerrightmargin=3pt,
    innerleftmargin=3pt,
        skipabove=\topskip,
        skipbelow=\topskip,
        nobreak=true
        }

% Thorems =====================================================================

\theoremstyle{myThmStyle}
\newtheorem{myTheorem}[equation]{theorem}

% Proof environment ===========================================================
% (attached to theorem)

\newenvironment{myTheoremEnvEx}[1][\hsize]
{% 
\def\FrameCommand 
{%
    {\color{thmLnColor}\vrule width 8pt}%
    \hspace{0pt}%must no space.
    \fboxsep=5pt\colorbox{white}%
}%
\vspace{-\topskip}
\vspace{-\topskip}
\vspace{-1pt}
\MakeFramed{\hsize#1\advance\hsize-\width\noindent\FrameRestore}%
}
{\endMakeFramed}
\setlength{\FrameSep}{0pt}

% Theorem environment =========================================================

\newenvironment{theorem}[1]
{
\begin{mdframed}[style=MDFStyGrayScreen]
\begin{myTheorem}#1
}
{
\end{myTheorem}
\end{mdframed}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DOCUMENT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}


\section{Section A}
\label{sec:SectionA}

\begin{theorem}[about something important] \label{thm:important}
This is some important theorem with equations
\[
    1 + 1 = 0 \;,
\]
and text.
\end{theorem} 

Some text with reference to theorem \eqref{thm:important}.

\begin{theorem}[about something important] \label{thm:veryImportant}
This is some important theorem with equations
\[
    1 + 1 = 0 \;,
\]
and text.
\end{theorem}%
\begin{myTheoremEnvEx}
Proof of something important... The proof may exceed several pages. The proof may exceed several pages. The proof may exceed several pages. 

The proof may exceed several pages. The proof may exceed several pages. The proof may exceed several pages.
\end{myTheoremEnvEx}

\end{document}

There are two things that do not work yet:

  1. When the text within the proof exceeds one page, then there appears a small gap between theorem and proof.
  2. The gray bar of the proof is currently always on the left side. But I want it to be on the inner margin (left for right pages and right for left pages).

The first problem is certainly due to my dirty bugfix

\vspace{-\topskip}
\vspace{-\topskip}
\vspace{-1pt}

within \newenvironment{myTheoremEnvEx}[1][\hsize].

Can you help me with this? I'd also be happy to see better solutions.

Best Answer

This is somewhat of an old question but I was interested in how to put a line down the margin, where the side changes depending on the parity of the page number.

Rather than using the mdfamed package I have used tcolorbox as I am more used to using it. For answer this question, tcolorbox has one particularly useful switch, toggle left and right, that can be used to swap the role of left and right in various instances. This is what is used to address point #2 in the OPs question.

Apart from this it is fairly straightforward except for the creation of a new theorem "style" for use by \newtcbtheorem to cater for the particular style required by the question. This is done by the line

\tcbset{theorem style/theorem wide name and number/.code={
                \let\tcb@theo@title=\tcb@theo@widetitle}
}

with the associated macro \tcb@theo@widetitle doing the actual work. This the output

Sample output

from the following code (updated for tcolorbox version 1.14, with breakable added to the theorem style to allow it also to break over pages)

\documentclass[a4paper,twoside,11pt]{article}

\usepackage{amsmath,amsthm,amsfonts}
\usepackage{lipsum}

%% Other Packages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,theorems}
\usepackage{changepage}% for use of \ifoddpage below

% Theorems ==============
\definecolor{thmBgColor}{RGB}{250,250,250}
\definecolor{thmLnColor}{RGB}{200,200,200}

\makeatletter
% define a new theorem style
\def\tcb@theo@widetitle#1#2#3{\hbox to \textwidth{\textsc{\large#1}\normalsize\space#3\hfil(#2)}}
\tcbset{
  theorem style/theorem wide name and number/.code={ \let\tcb@theo@title=\tcb@theo@widetitle},
  proofbox/.style={skin=enhancedmiddle,breakable,parbox=false,boxrule=0mm,
    check odd page, toggle left and right, colframe=thmLnColor,
    leftrule=8pt, rightrule=0mm, boxsep=0mm,arc=0mm, outer arc=0mm,
    left=3mm,right=0mm,top=0mm,bottom=0mm, toptitle=0mm,
    bottomtitle=0mm,colback=white,
    before={\par\vskip-2pt},after={\par\smallbreak},
  },
}
\newtcolorbox{ProofBox}{proofbox}
\makeatother

\newtcbtheorem[use counter=equation, number within=section]{mytheorem}{Theorem}%
  {theorem style=theorem wide name and number,enhanced,breakable,
  arc=0mm,outer arc=0mm,parbox=false,
  boxrule=0mm,toprule=2pt,bottomrule=2pt,left=1mm,right=1mm,
  titlerule=0mm,toptitle=0mm,bottomtitle=0mm,top=0mm,
  colback=thmBgColor,colframe=thmLnColor,
  title style={color=thmBgColor}, coltitle=black,
  attach title to upper={\par\noindent},
  before={\par\vfill\smallbreak\noindent}, after={\par},
  fonttitle=\normalfont\large,
  }{thm}

\newenvironment{theorem}[2][]{\noindent\mytheorem{#1}{#2}}{\endmytheorem}

\let\realproof\proof
\let\realendproof\endproof
\renewenvironment{proof}[1][Proof]{\ProofBox\strut\textsc{#1}\space}{\endProofBox}

%% Style Definitions
\numberwithin{equation}{section}

\begin{document}
\quad\clearpage % to make it easier to get a screenshot with two pages

\section{Section A}
\label{sec:SectionA}

\begin{theorem}[about something important]{important}
This is some important theorem with equations
\[
    1 + 1 = 0 \;,
\]
and text.
\end{theorem}

\begin{proof}
  \lipsum*[1-8] Theorem \eqref{thm:important}.
\end{proof}

\begin{theorem}[about something important]{veryImportant}
This is some important theorem with equations
\[
    1 + 1 = 0 \;,
\]
and text.

It continues for some more lines, so that a page break occurs during
its statement.
\end{theorem}

\begin{proof}[Proof of something important]
\lipsum[1]
\end{proof}

\end{document}