# How to make mdframed ignore descenders in last line

How can we make mdframed to ignore the descenders of the last line in the calculation of the innerbottomsep. In my eyes there are two reasons to desire this:

Inner margin
The optical bottom sep is too large in a line that contains descenders compared with on the has none.

Line grid
It is impossible to keep in the line grid, which otherwise could be achieved by adjusting the skip above and below.

As the image shows the frame without descenders keeps the following lines in the grid with the well adjusted skips but if we got descenders the adjustment is broken.

Code
This is the code I used to produce the images (comment out on line to get the first image).

\documentclass{scrartcl}

\usepackage{tikz}
\usepackage{mdframed}

\newmdenv[%
skipabove=12.6pt,
skipbelow=12.6pt,
]{myframe}

\usepackage{lipsum}
\begin{document}
% draw a line grid
\noindent
\begin{tikzpicture}[overlay]
\foreach \y in {0,...,-42}
\draw [magenta] (0,\y*\baselineskip) -- ++(\textwidth,0);
\end{tikzpicture}%
\lipsum[1]
\begin{myframe}
Without descender it works.
\end{myframe}
\lipsum[2]
\begin{myframe}
But with descenders the skip is a little too much.
\end{myframe}
\lipsum
\end{document}

-

You can get a consistent space below the line by forcing a baselineskip to be inserted like this:

\par\strut\par
\unskip\unskip\setbox0=\lastbox


This way, a 'pseudo-line' is inserted and then removed, leaving exactly the amount of space which would fill up the baseline distance. And this of course depends on whether there are descenders or not.

Unfortunately, it seems mdframed is removing trailing vertical space, so normally, the additional space would vanish again. In addition, you'd always get the spacing of the latter example, which doesn't seem to be what you want. You can counteract both by adding

\vspace*{\dimexpr\ht\strutbox-\baselineskip\relax}


which adds negative vertical space corresponding to the distance between the baseline and the beginning of the next line.

Of course, this can be put together as a new environment, leading to the solution

\newmdenv[%
skipabove=12.6pt,
skipbelow=12.6pt,
]{@myframe}

\newenvironment{myframe}
{%
\begin{@myframe}
}
{
\par\strut\par
\unskip\unskip\setbox0=\lastbox
\vspace*{\dimexpr\ht\strutbox-\baselineskip\relax}
\end{@myframe}
}

-
Works fine. Thanks :-) – Tobi Apr 11 '12 at 12:59
@Tobi: I added the option ignorelastdescenders to the new version at gihub. at:Stefan thanks for the great solution. – Marco Daniel May 18 '12 at 11:46
@MarcoDaniel: Thanks! – Tobi May 21 '12 at 7:37

You can enclose your text in a vbox and adjust according to your taste.

\begin{myframe}
\vbox to 0pt {But with descenders the skip is a little too much.}
\end{myframe}


You can change the vbox to 0pt to another length to suit (5.5pt looks right for me). For more control you can even enclose a strut in the \vbox. Use a:

vrule height..pt width0pt depth..pt


for more control.

-
Thanks but I desire a more automatic solution since it would be much work to create a \vbox for about 100 frames manually … – Tobi Mar 11 '12 at 10:12
@Tobi Then I suggest you capture the contents, place them in a box and check if the box has depth (this will signal that there are descenders), then adjust accordingly. – Yiannis Lazarides Mar 11 '12 at 10:52
If I'm understanding this correctly, stuff in the myframe environment would break over pages. This property is lost by enclosing the content in a \vbox. – Stephan Lehmke Apr 10 '12 at 14:39
@StephanLehmke: Yes thats right … – Tobi Apr 10 '12 at 21:27