2

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

2

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}
  • 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 0: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 0: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 0: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 0:12
  • 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 0:39

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.