# Numberwithin with enumitem

From this topic, I've implemented Mico's answer. Now, I want to reference figures this enumeration too. For instance,

\documentclass{article}
\usepackage{enumitem,cleveref}

\newlist{myenum}{enumerate}{3}
\setlist[myenum,1]{label=\textbf{\arabic*.},
ref  =\textbf{\arabic*.}}
\setlist[myenum,2]{label=\textbf{(\alph*)},
ref  =\themyenumi\textbf{(\alph*)}}
\setlist[myenum,3]{label=\bfseries(\roman*),
ref  =\themyenumii\textbf{.(\roman*)}}

\crefname{myenumi}{item}{items}
\crefname{myenumii}{item}{items}
\crefname{myenumiii}{item}{items}

\usepackage{graphicx,amsmath}
\numberwithin{figure}{myenumi}

\begin{document}

\begin{myenum}
\item
\begin{figure}
\caption{The Universe.\label{fig:universe}}
\includegraphics{universe}
\end{figure}
\end{myenum}

As shown in \cref{fig:universe}.

\end{document}


However, these lines got me this error: "Missing \endcsname inserted. \caption". Any ideas about how to solve this? Thanks.

• Please don't post code fragments. Instead put your fragments into a complete compilable document that people can play with. – Alan Munn Jan 20 '18 at 22:41
• Fixed. Sorry about that. – mflopezabu Jan 20 '18 at 22:51
• a figure is a float and can't be nested within anything else. next, a label on a figure should be associated with a caption. the first problem can be solved by just inserting the graphic with \incudegraphics (omitting the figure). some other type of anchor will be needed for the \label. unfortunately, i'm not in a position to be able to test anything, but this might give a pointer to another helper. – barbara beeton Jan 20 '18 at 23:14
• That's not quite @barbarabeeton's point: you can't use the figure environment inside your enumeration. – Alan Munn Jan 20 '18 at 23:44
• On the assumption that you don't want all figures to be within your myenum environment, it doesn't really make sense to do what you're asking. – Alan Munn Jan 21 '18 at 0:02

As @barbarabeeton has already pointed out in a comment, one can't use a LaTeX "float" object (such as a figure environment) inside an enumeration. To work around this, I suggest you load the caption package and use \captionof{figure}{...} directives to generate the captions. I also suggest you use minipage environments to encase the graphs and their associated \captionof directives. That way, you can assure that the captions will be centered above the respective graphs.

Separately, I would also like to suggest that cross-references not be rendered in bold-face.

A final comment, to echo the comment by @AlanMunn: Unless all graphs are associated with level-1 enumerated items, your proposed setup doesn't make a lot of (typographic) sense. In fact, it should probably be avoided.

\documentclass[demo]{article} % remove 'demo' option in real doc.
\usepackage{graphicx,amsmath,enumitem}

\usepackage[skip=0.333\baselineskip]{caption} % for "\captionof" macro
\usepackage{cleveref}

\newlist{myenum}{enumerate}{3}
\setlist[myenum,1]{label=\textbf{\arabic*.},
ref  =\arabic*}
\setlist[myenum,2]{label=\textbf{(\alph*)},
ref  =\themyenumi.(\alph*)}
\setlist[myenum,3]{label=\bfseries(\roman*),
ref  =\themyenumii.(\roman*)}
\crefname{myenumi}{item}{items}
\crefname{myenumii}{item}{items}
\crefname{myenumiii}{item}{items}

\numberwithin{figure}{myenumi}
\begin{document}

\begin{myenum}
\item
\begin{minipage}[t]{0.5\linewidth} % choose width suitably
\captionof{figure}{The Universe.}
\label{fig:universe}
\includegraphics[width=\textwidth]{universe}
\end{minipage}
\end{myenum}

As shown in \cref{fig:universe}, \dots
\end{document}

• Indeed, all my graphs are associated with level-1 enumerated items, so this worked great. Thanks. – mflopezabu Jan 21 '18 at 1:20