5

I have a problem with mdframed in combination with \parskip and \label. Consider the following code, which is a MWE stripped down from a large document.

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\parskip=32pt

\begin{document}

  \begin{mdframed}[suppressfirstparskip=false,frametitle={Theorem 1.}]
  Text
  \label{Yeah}
  \end{mdframed}

  \begin{mdframed}[suppressfirstparskip=false,frametitle={Theorem 2.}]
  \label{Yeah}
  Text
  \end{mdframed}

\end{document}

(I am aware of the fact that the \label command does not make sense here; it does in my original document and I see the same behaviour in that document). This code is rendered as follows:

enter image description here

Clearly, the second theorem has too much vertical space due to the \label command. Is there a way to fix this?

EDIT: After @Gonzalo Medina's answer, I added \leavevmode to the environment that I am actually using. This almost gets me to where I want to be, except there is still an unexpected space showing up in my document. The MWE is:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\parskip=32pt

\newenvironment{theorem}[1]%
  {\begin{mdframed}[suppressfirstparskip=false,frametitle={#1}]\leavevmode\ignorespaces}%
  {\end{mdframed}}     

\begin{document}

  \begin{theorem}{Theorem 1.}
  Text
  \label{Yeah}
  \end{theorem}

  \begin{theorem}{Theorem 2.}
  \label{Yeah}
  Text
  \end{theorem}

\end{document}

This is rendered as follows:

enter image description here

There is an extra space in the second environment.

EDIT 2: I ended up replacing \label inside the mdframed scope:

\makeatletter
\newenvironment{theorem}[1]%
  {\begin{mdframed}[suppressfirstparskip=false,frametitle={#1}]%
   \let\@oldlabel=\label%
   \renewcommand\label[1]{\@oldlabel{##1}\ignorespaces}%
   \leavevmode\ignorespaces}%
  {\let\label=\@oldlabel%
   \end{mdframed}}
\makeatother

1 Answer 1

4

Another instance of problems with material in vertical mode; use \leavevmode:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\parskip=32pt

\begin{document}

  \begin{mdframed}[suppressfirstparskip=false,frametitle={Theorem 1.}]
  Text
  \label{Yeah}
  \end{mdframed}

  \begin{mdframed}[suppressfirstparskip=false,frametitle={Theorem 2.}]
  \leavevmode\label{Yeah}%
  Text
  \end{mdframed}

\end{document}

enter image description here

After the edit to the question, I added a % character after the argument for \label in the second environment to prevent an undesired space.

If you are interested in theorem-like structures, perhaps you could use \newmdtheoremenv.

7
  • Hmm, I actually did add \leavevmode\ignorespaces to the beginning part of my environment; unfortunately now the space after \label is turned into an actual space... Is there a good way to deal with that as well?
    – yori
    Commented Apr 14, 2014 at 6:51
  • @Yori you need to comment out the spacing: \label{Yeah}% Commented Apr 14, 2014 at 7:03
  • @Gonzalo_Medina: I was afraid so. Unfortunately I have a book full of theorems formatted like that. Is there no way around this? Interestingly, your code does not seem to suffer from the same problem. Why is that?
    – yori
    Commented Apr 14, 2014 at 7:04
  • @Yori I don't see an easy way, other than commenting out the space or changing the placement of \label. Commented Apr 14, 2014 at 7:07
  • @Yori sure my code includes that space; I uploaded the wrong image that I produced with the commented-out sign; I accidentally deleted it in an edit. Commented Apr 14, 2014 at 7:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .