3

I want to be able to invoke a mdframed box and specify a title without using the frametitle=Title syntax:

I want to write

\begin{mdframedbox}[Title]
    ...content here...
\end{mdframedbox}

instead of

\begin{mdframedbox}[frametitle=Title]
    ...content here...
\end{mdframedbox}

Here is a tex document that compiles without contains how I am currently using the mdframed package to create a box.

\documentclass{article}

\usepackage[framemethod=TikZ]{mdframed}%boxes
\usepackage{xcolor}%boxes
\usepackage{lipsum}

%% Generic use box with grey background
\newmdenv[%
    backgroundcolor=gray!15,
    linecolor=black,
    outerlinewidth=1pt,
    roundcorner=1mm,
    skipabove=\baselineskip,
    skipbelow=\baselineskip,
    font=\small,
    nobreak=true,
]{graybox}

\begin{document}

\begin{graybox}[frametitle=Gray Box Title]
\lipsum[1]
\end{graybox}

\end{document}

Edit:

I have looked through the documentation for mdframed and didn't see anything that stood out. My first guess is that it's not possible as mdframed is written and I'll need to write a wrapper environment around it to get the desired behavior, however I wanted to make sure that I didn't miss something that already existed before trying to write said wrapper

Tuffwer
  • 200

1 Answers1

4

What I've done is just modify very slightly the way \begin{graybox} works so that it new requires one argument which is the title. I then pass that to the frametitle key. I did this by modify the \graybox command which is implicitly called when you have \begin{graybox}. The little bit of magic that does this is:

\let\orig@graybox=\graybox
\def\graybox#1{
  \orig@graybox[frametitle={#1}]
}

The first line save the original \graybox definition in \orig@graybox. The second and third lines overwrite the \graybox definition so that it calls the original graybox and passes its the first argument (denoted by #1) to the frametitle key.

The \makeatletter and \makeatother below allows the @ symbol to be used in definitions so that \orig@graybox can't be accidentally used in the code.

\documentclass{article}

\usepackage[framemethod=TikZ]{mdframed}%boxes
\usepackage{xcolor}%boxes
\usepackage{lipsum}

%% Generic use box with grey background
\newmdenv[%
    backgroundcolor=gray!15,
    linecolor=black,
    outerlinewidth=1pt,
    roundcorner=1mm,
    skipabove=\baselineskip,
    skipbelow=\baselineskip,
    font=\small,
    nobreak=true,
]{graybox}

\makeatletter
\let\orig@graybox=\graybox
\def\graybox#1{
  \orig@graybox[frametitle={#1}]
}
\makeatother

\begin{document}

\begin{graybox}{Gray Box Title}
\lipsum[1]
\end{graybox}

\end{document}

output

Addendum

If you want to have the possibility of not displaying a title, the easiest option is to leave the argument blank as the mdframed package will take an blank frametitle as indicating no title.

Otherwise, an alternative would be to give it an optional argument which is conventionally surrounded by [].

% ...
\makeatletter
\let\orig@graybox=\graybox
\def\graybox{
  \@ifnextchar[{\graybox@opt}{\orig@graybox}
}
\def\graybox@opt[#1]{
  \orig@graybox[frametitle={#1}]
}
\makeatother

\begin{document}

\begin{graybox}[Gray Box Title]
\lipsum[1]
\end{graybox}

\begin{graybox}
\lipsum[1]
\end{graybox}

\end{document}
JP-Ellis
  • 8,929
  • 2
  • 33
  • 49
  • Oh that's pretty neat, LaTex never ceases to amaze me with it's flexibility. I really should learn how all this works someday. Thanks! – Tuffwer Feb 23 '16 at 00:10
  • Yeah it is pretty neat :) I'll just amend the answer to explain what is happening (so you can adapt it to other scenarios). – JP-Ellis Feb 23 '16 at 00:11
  • Another (hopefully minor) question, how do you get the processed output to display after the tex source? is there some sort of compile here line the tex.se markup understands? – Tuffwer Feb 23 '16 at 00:12
  • Oh, that's some dark voodoo magic to do that... I screenshot it and upload the picture :P – JP-Ellis Feb 23 '16 at 00:12
  • yeah, that was my original guess, but I figured the tex.se interface might be special, I wish it did include a tex interpreter. If I use this and don't want a particular box to have a title can I pass an empty string in, or will I need to keep two versions of the box environment? – Tuffwer Feb 23 '16 at 00:15
  • I just double checked, and if the string is empty then no title is displayed which saves some of the effort of having to look ahead weather an argument is given or not. – JP-Ellis Feb 23 '16 at 00:24
  • @Tuffwer I have just updated the answer addressing the possibility of having an optional frame title. – JP-Ellis Feb 23 '16 at 00:30
  • okay, thanks I think I see why that works, and I prefer the [] syntax to the {} for the title so I'll probably go with your second suggestion. Should there be a closing ] for the @ifnextchar line? – Tuffwer Feb 23 '16 at 00:36
  • 1
    No, the \@ifnextchar doesn't need to have a closing ]. You can read more about it at Understanding @ifnextchar. – JP-Ellis Feb 23 '16 at 00:39