4

By default, enumerate's \item labels align with the top of the text:

enter image description here

However, if the text happens to be a mdframed box, the labels align with the bottom of the box which does not look so good:

enter image description here

Question:

How do I align the labels at the top for both the framed and non-framed case?


Failed Solution:

I attempted to use the adjustbox package as per Aligning enumerate labels to top of image?:

    \begin{enumerate}[label={(\alph*)}]
        \item \adjustbox{valign=t}{\TextBFramed}
        \item \adjustbox{valign=t}{\TextCFramed}
    \end{enumerate}

but this yields the following error:

LaTeX Error: Something's wrong--perhaps a missing \item


Almost Working (Manual) Solution:

Adapting this solution from Aligning an enumeration item to the top of a tikzpicture seems to work, but how do I automate this without redefining \item. Also, the label is not quite aligned with the baseline of the first line in the frame (which can be tweaked by changing the 2, but how do I determine this value in different situations).

    \begin{enumerate}[label={(\alph*)}]
        \item ~\\[-2\baselineskip] \TextBFramed
        \item ~\\[-2\baselineskip] \TextCFramed
    \end{enumerate}

enter image description here

I'd be ok with replacing \item with \MyItem, so something like:

\newcommand{\MyItem}{\item ~\\[-2\baselineskip]}%

but I would need this \MyItem to only apply the ~\\[-2\baselineskip], when the content is an mdframed box, but not otherwise. Also, how do I determine the value of the multiplier? See the Mixed text (failed automated solution) section below.


Notes:


Code:

\documentclass{article}
\usepackage[paperheight=30cm]{geometry}% fit on "one" page
\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{mdframed}
\usepackage{adjustbox}

\newcommand{\TextB}{%
        Duis blandit tempus placerat. 
        Nulla vitae erat ante. Nulla facilisi. 
        Aliquam tristique interdum suscipit. 
        Duis posuere orci vel velit suscipit in porttitor purus eleifend. 
}%
\newcommand{\TextBFramed}{%
    \begin{mdframed}%
        \TextB%
    \end{mdframed}%
}%

\newcommand{\TextC}{%
    \begin{align*}
        e &= mc^2\\
        F &= ma
    \end{align*}
}%
\newcommand{\TextCFramed}{%
    \begin{mdframed}%
        \TextC%
    \end{mdframed}%
}%


\begin{document}\noindent
\textbf{Plain text (works fine):}
\begin{enumerate}[label={(\alph*)}]
    \item \TextB
    \item \TextC
\end{enumerate}
%
\medskip\noindent
\textbf{Framed text (not so good):}
\begin{enumerate}[label={(\alph*)}]
    \item \TextBFramed
    \item \TextCFramed
\end{enumerate}
%
\medskip\noindent
\textbf{Using adjustbox yields error:}
%\begin{enumerate}[label={(\alph*)}]
%   \item \adjustbox{valign=t}{\TextBFramed}
%   \item \adjustbox{valign=t}{\TextCFramed}
%\end{enumerate}

\medskip\noindent
\textbf{Framed text (manual solution):}
\begin{enumerate}[label={(\alph*)}]
    \item ~\\[-2\baselineskip] \TextBFramed
    \item ~\\[-2\baselineskip] \TextCFramed
\end{enumerate}

\bigskip\noindent
\newcommand{\MyItem}{\item ~\\[-2\baselineskip]}%
\textbf{Mixed text (failed automated solution):}\medskip
\begin{enumerate}[label={(\alph*)}]
    \MyItem \TextB
    \MyItem \TextC
    \MyItem \TextBFramed
    \MyItem \TextCFramed
\end{enumerate}
\end{document}

1 Answer 1

4

The output of mdframed is done by a simple makebox. At the moment I have no better idea ;-).

Whatever. To align the complete environment at the top you can modify the output with a vtop:

\makeatletter
\renewrobustcmd*\mdf@makebox@out[2][\linewidth]{%
 \noindent\hb@xt@\z@{%
    \noindent\makebox[\dimexpr #1\relax][l]{\vtop{\vskip-\ht\strutbox\vskip-\dp\strutbox\relax#2}}%
 \hss}%
}%
\makeatother

The complete code results to:

enter image description here

\documentclass{article}
\usepackage[paperheight=30cm]{geometry}% fit on "one" page
\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{mdframed}
%\usepackage{adjustbox}

\makeatletter
\renewrobustcmd*\mdf@makebox@out[2][\linewidth]{%
 \noindent\hb@xt@\z@{%
    \noindent\makebox[\dimexpr #1\relax][l]{\vtop{\vskip-\ht\strutbox\vskip-\dp\strutbox\relax#2}}%
 \hss}%
}%
\makeatother


\newcommand{\TextB}{%
        Duis blandit tempus placerat. 
        Nulla vitae erat ante. Nulla facilisi. 
        Aliquam tristique interdum suscipit. 
        Duis posuere orci vel velit suscipit in porttitor purus eleifend. 
}%
\newcommand{\TextBFramed}{%
    \begin{mdframed}%
        \TextB%
    \end{mdframed}%
}%

\newcommand{\TextC}{%
    \begin{align*}
        e &= mc^2\\
        F &= ma
    \end{align*}
}%
\newcommand{\TextCFramed}{%
    \begin{mdframed}[skipabove=0pt]%
        \TextC%
    \end{mdframed}%
}%


\begin{document}
\textbf{Framed text (not so good):}
\begin{enumerate}[label={(\alph*)}]
    \item \TextBFramed
    \item \TextCFramed
\end{enumerate}
\end{document}

You must log in to answer this question.

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