By default, enumerate
's \item
labels align with the top of the text:
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:
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}
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:
- Since redefining
\item
can be dangerous in strange interaction between `mdframed` and `\item`, I am trying to avoid that by using\MyItem
.
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}