I'm trying to (automatically) center an itemize in a Beamer presentation.
The problem is, that all solutions I've found either
- use
varwidth
withenumitem
(which works a charm but not in beamer), - use a table (which would cost me the exact overlay syntax from itemize/enumerate/description, which I'd prefer to keep, as the environment will be used by others) or
- manually size a
minipage
(or something similar).
So I started writing an environment that tries to calculate the width of an itemize to set a minipage
to the correct width.
The document below works as expected, what doesn’t work is nesting the lists.
So two questions:
- I don’t really know where to go from here. Can the problem be fixed (e.g. by changing the order of how stuff is calculated, as a
minipage
in the place of the nested list works)? Or do I need another approach? If the same syntax (i.e.\begin{ul}[<+-]
and\item<...>
with nesting) is possible with other methods that would also be fine, I'd just like the user-facing syntax to be familiar. - I currently use
\leftmargini
in the width calculation, I don't really understand how I would detect in which “layer” I am and whether to apply\leftmarginii
/\leftmarginiii
.
\documentclass{beamer}
% adapted from https://tex.stackexchange.com/a/127014
\ExplSyntaxOn
\NewDocumentEnvironment{measureItemize}{+b}
{%
% split the contents at \item
\seq_set_split:Nnn \l_werner_itemize_input_seq {\item} {#1}
% remove the first (empty) item
\seq_pop_left:NN \l_werner_itemize_input_seq \l_tmpa_tl
% measure each item
\seq_map_inline:Nn \l_werner_itemize_input_seq {\measureitem{##1}}
}
{}
\seq_new:N \l_werner_itemize_input_seq
% adapted from https://tex.stackexchange.com/a/185707
\newlength{\xitemlenx}
\newlength{\xitemleny}
\newcommand{\measureitem}[1]{%
\settowidth{\dimen0}{#1}%
\ifdim\dimen0>\xitemlenx%
\xitemlenx=\dimen0
\fi
\global\xitemleny\xitemlenx%
}
\NewDocumentEnvironment{ul}{o+b}
{%
\begin{measureItemize}
#2
\end{measureItemize}
\bigskip
\begin{minipage}{\dim_min:nn {\linewidth} {\xitemleny+\labelsep+\itemindent+\leftmargini}}
\begin{itemize}[#1]
#2
\end{itemize}
\end{minipage}
\bigskip
}{}
\ExplSyntaxOff
\begin{document}
\begin{frame}
\centering
\fbox{
\begin{ul}[<+->]
\item item 1
\item item 2 is a bit longer
\item item 3
% \begin{ul}
% \item item 1
% \item item 2
% \item item 3
% \end{ul}
\end{ul}
}
\end{frame}
\end{document}