Align title box and text with horizontal lines in mdframed

mdframed

I found a neat solution using mdframed to make boxes for theorems, lemmas, and proofs. As brilliant as this solution is, I wonder if it is possible to align the title with the horizontal lines of the boxes (through the center). This is probably made look like this by design but I cannot help to wonder what it would look like aligned.

MWE is as follows:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}
\usepackage[framemethod=TikZ]{mdframed}
%%%
\newcounter{theo}[section]\setcounter{theo}{0}
\renewcommand{\thetheo}{\arabic{section}.\arabic{theo}}
\newenvironment{theo}[2][]{%
\refstepcounter{theo}%
\ifstrempty{#1}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=blue!20]
{\strut Theorem~\thetheo};}}
}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=blue!20]
{\strut Theorem~\thetheo:~#1};}}%
}%
\mdfsetup{innertopmargin=10pt,linecolor=blue!20,%
linewidth=2pt,topline=true,%
frametitleaboveskip=\dimexpr-\ht\strutbox\relax
}
\begin{mdframed}[]\relax%
\label{#2}}{\end{mdframed}}
%%%

\title{FANCY MDFRAMED ALIGNMENT TEST}
\author{John Doe}
\date{January 2022}

\begin{document}

\maketitle

\section{Introduction}

\begin{theo}

TEST.

\end{theo}

\end{document}
  • Is it possible to align the title text and box with the horizontal lines? In other words, is it possible to shift it up a bit?

I tried to ask a question on the solution site but it never appeared.

Best Answer

The example from texblog is mentioned in the mdframed manual (page 21). The manual also contains another example where the title box is centered on the top line, on page 27. You can adjust this other example to get the desired result.

The example draws the title node using the singleextra option. This 'family' of extra keys (extra, singleextra, firstextra, secondextra, middleextra) defines points (P) and (O) on the frame, where the intersection of (P) and (O), i.e., (P-|O), is the top corner, as used in the example in the manual. The idea is to draw the title node with this coordinate as position, i.e., \node at (P-|O).

Then you need to set some options for the title node. Important is anchor=west which causes the left side of the frame to be placed at the specified position (P-|O), as the default center placement would cause the node to expand to the left if the title is longer. Furthermore you can use xshift as in the manual to put the title not exactly on the corner but a bit inside. Finally the box can be made a bit more tight with inner sep=0.5mm. This looks nice vertically, but horizontally it is a bit too tight, therefore you can add some \hspace before and after the theorem title. Finally set some innertopmargin to shift the contents down (because the title overlaps the content box) and that's it.

MWE:

\documentclass{article}
\usepackage{amsthm}
\usepackage[framemethod=TikZ]{mdframed}
%%%
\newcounter{theo}[section]\setcounter{theo}{0}
\renewcommand{\thetheo}{\arabic{section}.\arabic{theo}}
\newenvironment{theo}[2][]{%
\refstepcounter{theo}%
\ifstrempty{#1}%
{\mdfsetup{%
singleextra={\node[anchor=west,xshift=5mm,rectangle,fill=blue!20,inner sep=0.5mm] at (P-|O) %
{\hspace*{5pt}\strut Theorem~\thetheo\hspace*{5pt}};}}%
}%
{\mdfsetup{%
innertopmargin=2cm,
singleextra={\node[anchor=west,xshift=5mm,rectangle,fill=blue!20,inner sep=0.5mm] at (P-|O) %
{\hspace*{5pt}\strut Theorem~\thetheo:~#1\hspace*{5pt}};}}%
}%
\mdfsetup{linecolor=blue!20,%
linewidth=2pt,topline=true,%
innertopmargin=1.5Em,
}
\begin{mdframed}[]\relax%
\label{#2}}{\end{mdframed}}
%%%

\title{FANCY MDFRAMED ALIGNMENT TEST}
\author{John Doe}
\date{January 2022}

\begin{document}

\maketitle

\section{Introduction}

\begin{theo}[Pythagoras]{thm:t1}
TEST.
\end{theo}

\end{document}

Result:

enter image description here


Below a version (for the definition with an explicit title) that is centered on the middle of the bar in the lower case e, using text height and text depth with values found by trial and error. I'm not sure it is much better, I think the original is preferred.

\mdfsetup{%
extra={\node[anchor=west,xshift=5mm,rectangle,fill=blue!20,inner sep=0.5mm,text height=7pt,text depth=2.5pt] at (P-|O) %
{\hspace*{5pt}\strut Theorem~\thetheo:~#1\hspace*{5pt}};}}%

enter image description here

Related Question