2

I'm trying to get a dashed line for frametitlerule under mdframed, but the setting appears to be ineffective. I'm basing this code on the documentation (Tikz example 5), but this question is also related: Box with dashed border

MWE

\documentclass{article}

\usepackage{xparse}
\usepackage[framemethod=tikz]{mdframed}

\NewDocumentEnvironment{fitb}{ m } {
  \mdframed[%
    frametitle={#1},%
    frametitlerule=true,%
    frametitlerulewidth=1pt,%
    frametitlebackgroundcolor=blue!5,%
    apptotikzsetting={\tikzset{mdfframetitlerule/.append style={draw=blue,dashed}}}%
]}{\endmdframed}

\begin{document}
\begin{fitb}{title}
  hello, world
\end{fitb}
\end{document}

Here's what I'm getting:

output

4

Looking at the mdframed code in md-framed-1.mdf the problem seems to be that in the definition of \def\mdf@@frametitlerule the line is drawn as a rectangle, and not a line, using:

\tikz\draw[mdfframetitlerule] (0,0)%
           rectangle (\dimen@,\mdf@frametitlerulewidth@length);

Adding dashed to the style will of course only change the border of the rectangle, which is probably not what you want. I am sure that it should be possible to change this using \patchcmd and friends but I am scared about using it on a multi-line command so in the code below I have just cut-and-pasted the whole command, changing only the part of it so that it draws a line of the appropriate width instead of a rectangle.

This still does not quite fix the problem and I found that I needed to put the \tikzset{...} outside of the \mdframed command. With these changes in place the MWE below produces what I think you want:

enter image description here

If you don't hack the \mdf@@frametitlerule command, as described above, and only move the \tikzset command outside of the \mdframed command then instead you get the expected result

enter image description here

That is, you obtain a filled rectangle with a dashed border.

Here is the full code:

\documentclass{article}
\usepackage{xparse}
\usepackage[framemethod=tikz]{mdframed}

% change the mdframed definition of \mdf@@frametitlerule so that it
% draws a line rather than a rectangle.
\makeatletter
\def\mdf@@frametitlerule{%
  \ifbool{mdf@frametitlerule}{%
   \vbox{\hsize0pt
     \par\unskip\vskip\mdf@frametitlebelowskip@length
     \noindent\rlap{\hspace*{-\mdf@innerleftmargin@length}%
     \begingroup%
     \pgfmathsetlength{\dimen@}{\mdfframetitleboxwidth
                                +\mdf@innerleftmargin@length
                                +\mdf@innerrightmargin@length}%
     % the following line has been changed 
     \tikz\draw[line width=\the\mdf@frametitlerulewidth@length, mdfframetitlerule]
          (0,0) -- (\dimen@,0);
     \endgroup}
   }%
  }{%
   \vbox{\hsize0pt
     \par\unskip\vskip\mdf@frametitlebelowskip@length
     \noindent\rlap{}
   }%
  }%
  \iftoggle{mdf@notfirstframetitle}%
    {%
      \par\unskip\vskip\mdf@splittopskip@length%
    }%
    {%
      \par\unskip\vskip\mdf@innertopmargin@length%
    }%
}%
\makeatother

\NewDocumentEnvironment{fitb}{ m }
  { \tikzset{mdfframetitlerule/.append style={draw=blue,dashed}}
    \mdframed[frametitle={#1},
              frametitlerule=true,%
              frametitlerulewidth=2pt,%
              frametitlebackgroundcolor=blue!5,%
  ]}
  {\endmdframed}

\begin{document}
\begin{fitb}{title}
  hello, world
\end{fitb}
\end{document}

Edit

Now that we know what the "problem" is we can achieve the desired result by styling the rectangle that mdframed uses so that it looks like a dashed line. One way to do this is to use a checkerboard pattern. This produces:

enter image description here

Here is the updated code:

\documentclass{article}
\usepackage{xparse}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{patterns}

\NewDocumentEnvironment{fitb}{ m }
  { \tikzset{mdfframetitlerule/.append style={
      pattern=checkerboard, pattern color=blue}}
    \mdframed[frametitle={#1},
              frametitlerule=true,%
              frametitlerulewidth=2pt,%
              frametitlebackgroundcolor=blue!5,%
  ]}
  {\endmdframed}

\begin{document}
\begin{fitb}{title}
  hello, world
\end{fitb}
\end{document}
  • Do you think there's a way to do this without changing the internal mechanisms of mdframed? If memory serves, you can specify a fill style. I'm looking specifically at 15.5.1 "Graphic Parameters: Fill Pattern" in the pgf manual. – Sean Allred Sep 26 '18 at 15:19
  • @SeanAllred My first reaction was, no, this is a "bug" in mdframed and you have to hack it but then I thought again. See edit. – Andrew Sep 26 '18 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.