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 like to use mdframed to make a framed environment with an optional title. The title has to be in its own box with a gray background, like this: example

I hope that this is possible.

MWE

\documentclass{article}

\usepackage[framemethod=TikZ]{mdframed}
\newenvironment{Frame}[1][]{%
    \begin{mdframed}[%
        frametitle={#1},
        skipabove=\baselineskip plus 2pt minus 1pt,
        skipbelow=\baselineskip plus 2pt minus 1pt,
        linewidth=0.5pt,
    ]%
}{%
    \end{mdframed}
}

\begin{document}
Text
\begin{Frame}[with Title]
    Text
\end{Frame}
Text
\begin{Frame}% no Title
    Text
\end{Frame}
Text
\end{document}

I like to use mdframed instead of the package and hack to make the picture above because it is more flexible :-)

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Update

The version 1.2 of mdframed has new possibilities for setting the frame title. Based of your request you can simple use the option frametitlerule=true and frametitlebackgroundcolor=gray!20. The current version is available at github.

\documentclass{article}

\usepackage[framemethod=TikZ]{mdframed}
\newenvironment{Frame}[1][]{%
    \begin{mdframed}[%
        frametitle={#1},
        skipabove=\baselineskip plus 2pt minus 1pt,
        skipbelow=\baselineskip plus 2pt minus 1pt,
        linewidth=0.5pt,
        frametitlerule=true,
        frametitlebackgroundcolor=gray!30
    ]%
}{%
    \end{mdframed}
}

\begin{document}
Text
\begin{Frame}[with Title]
    Text
\end{Frame}
Text
\begin{Frame}% no Title
    Text
\end{Frame}
Text
\end{document}

enter image description here


By using tikz to modify the header here one suggestion.

You have to modify the command \mdframetitleenv to influence the output. The package itself controls whether the frame title is empty or not.

\documentclass{article}

\usepackage[framemethod=TikZ]{mdframed}
\makeatletter
%ORIGINAL
%\providecommand*\mdframedtitleenv[1]{%
%  \@afterindentfalse
%   {\parindent \z@
%         \setlength{\parfillskip}{\z@ plus 1fil}%
%         \mdraggedtitle\nobreak\mdf@frametitlefont#1\relax%
%    \par}%
%  \@afterheading}
\tikzstyle{mybox} = [draw=black, %
                    fill=gray!20,%
                    line width=\mdf@middlelinewidth@length,
                    rectangle,
                    inner sep=0pt,
                    inner ysep=0.333em,
                    minimum height=1cm,
                    text width = \linewidth
                                 +\mdf@innerleftmargin@length
                                 +\mdf@innerrightmargin@length
                                 +\mdf@linewidth@length,
                    align=left,
                    ]
\renewcommand*{\mdframedtitleenv}[1]{%
\vspace*{\dimexpr-\mdf@innertopmargin@length-\mdf@linewidth@length\relax}%
\makebox[0pt][l]{%
\hspace*{\dimexpr-\mdf@innerleftmargin@length-\mdf@linewidth@length\relax}%
\begin{tikzpicture}[remember picture]
\node [mybox]{\hspace*{\mdf@innerrightmargin@length}%
   \parbox{\linewidth}{\mdf@frametitlefont#1\relax}};%
\end{tikzpicture}%
}%
\par
\@afterheading
}%
\newenvironment{Frame}[1][]{%
    \begin{mdframed}[%
        frametitle={#1},
        skipabove=\baselineskip plus 2pt minus 1pt,
        skipbelow=\baselineskip plus 2pt minus 1pt,
        linewidth=0.5pt,
    ]%
}{%
    \end{mdframed}
}

\begin{document}
Text
\begin{Frame}[with Title]
    Text
\end{Frame}
Text

\begin{Frame}[with Title with Title with Title with Title with Title with Title with Title with Title with Title with Title with Title with Title with Title with Title with Title with Title]
    Text
\end{Frame}

Text
\begin{Frame}% no Title
    Text
\end{Frame}
Text
\end{document}
share|improve this answer
    
Thanks, it works :-) I don’t need it at the moment i guess but do you have an idea how to make this work with titles longer than one line? –  Tobi Oct 22 '11 at 14:10
    
PS: Is there a common way to prevent a pagebreak right after the frame title? –  Tobi Oct 22 '11 at 14:17
    
Thank you, seems to work now. I tried needspace befor but it doesn’t seem to work with 2\baselineskip only with fix values –  Tobi Oct 22 '11 at 15:37
    
No I didn’t. I’ll send an e-mail to your address given in the manual. –  Tobi Oct 22 '11 at 15:59
    
@Tobi: Please see my edit –  Marco Daniel Jan 8 '12 at 10:02
show 2 more comments

Your Answer

 
discard

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.