3

The following code gives one way to use circled numbers in one list. For the moment this solution uses one parameter given by the user which is the "width" of the total number of items of the list.

% Sources :
%   1) http://tex.stackexchange.com/questions/7032/good-way-to-make-pgftextcircled-numbers
%   2) http://tex.stackexchange.com/questions/33738/customize-diameter-of-a-circle-around-numbers/33744#33744

\documentclass{article}
    \usepackage[utf8x]{inputenc}
    \usepackage{enumitem}
    \usepackage{tikz}

    \newbox\nodebox
    \newcommand\pgftextcircled[2][0]{%
        \ifnum#1=0%
            \setbox\nodebox\hbox{#2}%
        \else%
            \setbox\nodebox\hbox{0}\wd\nodebox\dimexpr\wd\nodebox*#1\relax
        \fi
        \begin{tikzpicture}[baseline=(a.base)]%
            \node[
                draw,circle,
                outer sep=0pt,
                inner sep=0.5pt
            ](a){\hbox to \wd\nodebox{\hss#2\hss}};
        \end{tikzpicture}%
    }


\begin{document}

\begin{enumerate}[label=\protect{\pgftextcircled[1]{\arabic*}}]
    \item Item n°1
    \item Item n°2
    \item Item n°3
    \item Item n°4
    \item Item n°5
    \item Item n°6
    \item Item n°7
    \item Item n°8
\end{enumerate}

\begin{enumerate}[label=\protect{\pgftextcircled[2]{\arabic*}}]
    \item Item n°1
    \item Item n°2
    \item Item n°3
    \item Item n°4
    \item Item n°5
    \item Item n°6
    \item Item n°7
    \item Item n°8
    \item Item n°9
    \item Item n°10
    \item Item n°11
    \item Item n°12
\end{enumerate}

\end{document}

Could it be possible to define one environment enumerateCircle which knows automatically the length of the list so as to use the following syntax ?

\begin{enumerateCircle}
    \item Item n°1
    \item Item n°2
    \item Item n°3
    \item Item n°4
    \item Item n°5
    \item Item n°6
    \item Item n°7
    \item Item n°8
\end{enumerateCircle}

\begin{enumerateCircle}
    \item Item n°1
    \item Item n°2
    \item Item n°3
    \item Item n°4
    \item Item n°5
    \item Item n°6
    \item Item n°7
    \item Item n°8
    \item Item n°9
    \item Item n°10
    \item Item n°11
    \item Item n°12
\end{enumerateCircle}
projetmbc
  • 13,315

2 Answers2

2

You can compile the environment just to get the total number of items; this is a job for environ, with a redefinition of \pgftextcircled that takes as optional argument what size you want to encompass:

\documentclass{article}
    \usepackage[utf8]{inputenc}
    \usepackage{enumitem,environ}
    \usepackage{tikz}

\newbox\nodebox
\newcommand\pgftextcircled[2][0]{%
  \sbox\nodebox{#1}%
  \begin{tikzpicture}[baseline=(a.base)]
    \node[
          draw,circle,
          outer sep=0pt,
          inner sep=0.5pt
         ](a){\hbox to \wd\nodebox{\hss#2\hss}};
        \end{tikzpicture}%
    }

\makeatletter
\NewEnviron{enumerateCircle}
  {\setbox0=\vbox{
              \enumerate
                \BODY
                \global\chardef\enCir@final\value{\@enumctr}%
              \endenumerate}%
   \enumerate[label={\pgftextcircled[\number\enCir@final]{\arabic*}}]
     \BODY
   \endenumerate}
\makeatother

\begin{document}

\begin{enumerateCircle}
    \item Item n°1
    \item Item n°2
    \item Item n°3
    \item Item n°4
    \item Item n°5
    \item Item n°6
    \item Item n°7
    \item Item n°8
\end{enumerateCircle}

\begin{enumerateCircle}
    \item Item n°1
    \item Item n°2
    \item Item n°3
    \item Item n°4
    \item Item n°5
    \item Item n°6
    \item Item n°7
    \item Item n°8
    \item Item n°9
    \item Item n°10
    \item Item n°11
    \item Item n°12
\end{enumerateCircle}

\end{document}
egreg
  • 1,121,712
  • Thanks for this shorter code even if it seems a little slower than the preceding one. – projetmbc Nov 05 '11 at 17:38
  • @projetmbc Yes, it is slower because it has to typeset the environment twice. But it's this way or adding a self-generated \label command in order to use the information at the next run. – egreg Nov 05 '11 at 17:44
2

This shold be what you are looking for:

\documentclass{article}
\usepackage{enumitem}
\usepackage{tikz}
\makeatletter
\newcount\item@cnt
\newcommand*\circle@arg[1]{%
    \tikzpicture[baseline=(a.base)]%
        \node[draw,circle,outer sep=0pt,inner sep=0.5pt](a){\hbox to\max@wd{\hss#1\hss}};
    \endtikzpicture}

\long\def\get@body#1\end{%
    \expandafter\def\expandafter\collected@body\expandafter{\collected@body#1}\find@end}

\newcommand\find@end[1]{%
    \def\temp@name{#1}%
    \ifx\temp@name\name@env
        \count@item
        \setbox\z@\hbox{\item@number}\edef\max@wd{\the\wd\z@}%
        \begin{enumerate}[label=\protect\circle@arg{\arabic*}]
            \collected@body
        \end{enumerate}
    \else
        \expandafter\def\expandafter\collected@body\expandafter{\collected@body\end{#1}}%
        \expandafter\get@body
    \fi}

\newenvironment{enumcircle}
    {\expandafter\endgroup\expandafter\def\expandafter\name@env\expandafter{\@currenvir}%
    \let\collected@body\@empty\get@body}
    \relax

\long\def\count@item@i#1\item{\expandafter\ifx\@car#1\@nil\count@item\else\advance\item@cnt\@ne\expandafter\item\fi}

\long\def\count@item{%
    \begingroup
        \let\item\count@item@i
        \collected@body\item\count@item\item
    \expandafter\endgroup
    \expandafter\def\expandafter\item@number\expandafter{\number\item@cnt}}

\makeatother
\begin{document}
\begin{enumcircle}
    \item Item n°1
    \item Item n°2
    \item Item n°3
    \item Item n°4
    \item Item n°5
    \item Item n°6
    \item Item n°7
    \item Item n°8
\end{enumcircle}

\begin{enumcircle}
    \item Item n°1
    \item Item n°2
    \item Item n°3
    \item Item n°4
    \item Item n°5
    \item Item n°6
    \item Item n°7
    \item Item n°8
    \item Item n°9
    \item Item n°10
    \item Item n°11
    \item Item n°12
\end{enumcircle}
\end{document}
unbonpetit
  • 6,190
  • 1
  • 20
  • 26
  • Thanks. Your solution seems to be a litlle quicker than the following one but the code just after seems to be simpler to understand (I can't tkae time for analyse it for the moment). – projetmbc Nov 05 '11 at 17:37