16

ADDENDUM: I add, although it is possible to have the shadows on the dots (not active) as you can see from the following picture.

enter image description here

I ask you for your help to create a custom numbered list with a font other than the default, with a gray rectangle and small blue balls that indicate the degree of difficulty of an exercise.

I hope you can create an identical source.

enter image description here

  • Last edition introduces a "new question", so It would be better that you click on "Ask a question" and start a new question that you can link with this one. After that you can edit again this one to delete the new question. – Ignasi Feb 6 '17 at 9:00
  • In any case, what does "to get the shadows on the dots" means? Do you want a fixed amount of gray dots under the number with only some of them colored in red? – Ignasi Feb 6 '17 at 9:03
  • @Ignasi, Dearest, thanks for your answer. I would like to have a small change of the source. Exactly. I want a fixed amount of gray dots under the number with only some of them colored in red. 3 balls with 2 gray, a red dot (for example) indicates difficulties 1. – Sebastiano Feb 6 '17 at 9:47
  • I have provided an ADDENDUM to my answer to address your recent extension request. – Steven B. Segletes Feb 6 '17 at 18:37
  • I've also updated the answer in order to solve your last request. – Ignasi Feb 7 '17 at 9:07
17

See ADDENDUM for OP's follow-on.

EDITED to make a separate environment, benumerate.

Here, inside the benumerate environment, the optional argument to \item indicates the difficulty level.

Here is a brief description of what the new/revised macros do, since they may require some alteration if fonts are changed, as desired by the OP.

\difbox: creates the "difficulty-box" under the item number. The size and color of the gray box are provided by a \rule and \color macro. It calls on \makeballs to set the proper number of circles in the gray box.

\makeballs: the recursive macro that places a set of sequential \bullets as the circles in the box.

benumerate: the environment that accomplishes all this, by redefining a few things and calling on enumerate.

\item: inside benumerate, \item is redefined to take the difficulty level as the optional argument (default 1), which it saves before calling on the usual \item.

\labelenumi: the label redefinition inside benumerate, which stacks the \difbox 1pt below the enumeration label (which has been set in bold, sans font of an altered color).

EDITED to \smash the \labelenumi, since the low-hanging box was affecting line spacing of the item content.

\documentclass{article}
\usepackage{stackengine,xcolor,lipsum}
\let\svitem\item
\newcommand\difbox[1]{\stackengine{0pt}{\color{gray!30}\rule{5ex}{1.15ex}}{%
    \color{cyan!60!black}$\mkern1mu\makeballs{#1}$}{O}{c}{F}{F}{L}}
\def\makeballs#1{\ifnum#1>0\relax{\bullet}%
  \expandafter\makeballs\the\numexpr#1-1\relax\fi}
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
  \def\labelenumi{\smash{\stackunder[1pt]{\color{cyan!60!black}%
  \bfseries\sffamily\theenumi}{\difbox{\difficulty}}}}%
  \enumerate}{\endenumerate}
\begin{document}
\begin{benumerate}
\setcounter{enumi}{32}
\item[2] \lipsum[4]
\item[3] \lipsum[4]
\item[4] \lipsum[3]
\end{benumerate}
\end{document}

enter image description here

ADDENDUM

To handle the OP's follow on extension request, the solution is trivial. After the invocation of \makeballs{#1} in the difbox macro, add \textcolor{red}{\makeballs{\the\numexpr4-#1\relax}}.

\documentclass{article}
\usepackage{stackengine,xcolor,lipsum}
\let\svitem\item
\newcommand\difbox[1]{\stackengine{0pt}{\color{gray!30}\rule{5ex}{1.15ex}}{%
    \color{cyan!60!black}$\mkern1mu\makeballs{#1}%
    \textcolor{red}{\makeballs{\the\numexpr4-#1\relax}}$}{O}{c}{F}{F}{L}}
\def\makeballs#1{\ifnum#1>0\relax{\bullet}%
  \expandafter\makeballs\the\numexpr#1-1\relax\fi}
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
  \def\labelenumi{\smash{\stackunder[1pt]{\color{cyan!60!black}%
  \bfseries\sffamily\theenumi}{\difbox{\difficulty}}}}%
  \enumerate}{\endenumerate}
\begin{document}
\begin{benumerate}
\setcounter{enumi}{32}
\item[2] \lipsum[4]
\item[3] \lipsum[4]
\item[4] \lipsum[3]
\end{benumerate}
\end{document}

enter image description here

  • Nice, but wouldn't it be safer to create a new list environment rather than redefine \item globally? – Alan Munn Nov 3 '16 at 20:57
  • Very good! Thank you so much for your help Steven and Alan. But can I have some more details for the source? Why there is a few space between the first and the second straight line? I use Optima font with symbols computer modern. If I use \sffamily, the font of the numbers is Optima font. It is possible to have another bold font that is not default and that I can find into MikTeX packages? Agenda bold not exists in LaTeX. Best regards. – Sebastiano Nov 4 '16 at 8:20
  • @Sebastiano I am sorry, but I do not have access to the Optima font, so I can't tailor the particular solution. However, I will edit my answer with more explanation, so that you can alter certain settings to suit your need, as required. I am sorry I don't understand your comment "Why there is a few space between the first and the second straight line". – Steven B. Segletes Nov 4 '16 at 9:44
  • Hi Steven, thanks for kind and detailed answer. For example, you can look the nr. 33. Between the first row and the second row there is a little space (\vspace) adjacent to the number 33, to the rectangle with the little balls. For the others rows there are not problems. Is there a solution? For the source I have understand all. Steven, sorry for my bad English. – Sebastiano Nov 4 '16 at 17:24
  • 1
    @Sebastiano I verified again that my code compiles properly. Generally the error you describe arises when you try to output text in the preamble. This can occur inadvertantly, for example, with a missing backslash, a misplaced percent sign, etc. – Steven B. Segletes Feb 14 '17 at 10:58
11

Another implementation with tcolorbox. Instead of an \item inside an enumerate environment, a new environment problem will be used. This environment uses a mandatory parameter (difficulty 1 to 4) and an optional one to change particular formatting options in one or more problems.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox[auto counter]{problem}[2][]{%
   breakable,
    blankest,
    enhanced,
    attach boxed title to top left={yshift=-\tcboxedtitleheight},
    left=\tcboxedtitlewidth+1mm,
    top=0pt,
    colback=white,
    fonttitle=\bfseries\sffamily,
    coltitle={cyan!60!black},
    colbacktitle=white,
    colframe=white,
    title=\thetcbcounter, 
    overlay unbroken and first={%
        \node[fill=gray!30, inner sep=1pt, minimum width=\tcboxedtitlewidth, 
                outer sep=0pt, text=cyan!60!black, anchor=north] at (title.south) 
                {\foreach \i in {1,...,#2}{\if#2gt1{\,}\fi$\bullet$}};}, 
    #1}

\begin{document}

\lipsum[1]

\begin{problem}{1}
\lipsum[2]
\end{problem}

\begin{problem}{4}
\lipsum[3]
\end{problem}

\begin{problem}{2}
\lipsum[4]
\end{problem}

\end{document}

enter image description here

ADDENDUM

In this case, four balls are placed below the title and their color is decided with a conditional.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox[auto counter]{problem}[2][]{%
   breakable,
    blankest,
    enhanced,
    attach boxed title to top left={yshift=-\tcboxedtitleheight},
    left=\tcboxedtitlewidth+2mm,
    top=0pt,
    colback=white,
    fonttitle=\bfseries\sffamily,
    coltitle={cyan!60!black},
    colbacktitle=white,
    colframe=white,
    title=\thetcbcounter, 
    overlay unbroken and first={%
        \node[fill=gray!30, inner sep=1pt, 
            minimum width=\tcboxedtitlewidth, 
            outer sep=0pt, 
            anchor=north] at (title.south) {%
            \foreach \i in {1,...,4}{%
            \ifnum\i>#2%
                \,\textcolor{cyan!60!black}{$\bullet$}%
            \else
                \,\textcolor{red}{$\bullet$}%           
            \fi}};}, 
    #1}

\begin{document}

\lipsum[2]

\begin{problem}{1}
\lipsum[2]
\end{problem}

\begin{problem}{2}
\lipsum[2]
\end{problem}

\begin{problem}{3}
\lipsum[2]
\end{problem}

\begin{problem}{4}
\lipsum[2]
\end{problem}

\end{document}

enter image description here

  • Kind Ignasi thanks for another help for my question. I like you work too. I use LaTeX from 2002 but I not know all packages and I not understand the way to build the little balls to indicate the difficulty for exercises or problems. Now I use a long source with fonts optima with modern computer symbols, and I have not difficulty to paste to the my source. Thanks lot lot for your precius help. Yesterday, I have effectuate the registration into the excellent web site. Best regards. – Sebastiano Nov 5 '16 at 21:42
  • I apologize for the delay of my most sincere thanks for your answer due to the many commitments at school. – Sebastiano Feb 7 '17 at 13:38
  • I've tried to compile the code....What does this error mean? "! Package pgfkeys Error: I do not know the key '/tcb/blankest' and I am going t o ignore it. Perhaps you misspelled it." – ryuk Jan 3 at 0:52
  • 1
    @ryuk It means that you are using an outdated tcolorbox version which doesn't have blankest defined. Update tcolorbox and the problem will disappear. – Ignasi Jan 3 at 9:09
10

Here is a third solution using a standard enumitem environment together with tikz.

The idea is to create a new enumerate environment, which I have called questions, and then use before=\fakeitem to replace the standard \item command in this environment with a new command that typesets the "difficulty dots". The "difficulty dots" are then drawn using a pics construct inside a \tikz call.

Here is the code:

\documentclass{article}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{lipsum}

\tikzset{pics/question/.style args={#1,#2}{
             code={
                \node[teal] at (0.5,0.35) {\textsf{#1}};
                \fill[gray!30] (0.1,0) rectangle (0.9,0.2);
                \foreach \ball [evaluate=\ball as \x using {0.5+(\ball-0.5-#2/2)*0.2)}] in {1,...,#2} {
                    \fill[teal] (\x,0.1) circle (2.5pt);
                }
            }
        }
}

\let\realitem\item% save a copy of the \item command
\newcommand\fakeitem[1][\relax]{%
   \refstepcounter{questionsi}% increment the counter
   \realitem[\smash{\tikz[baseline]{\draw(0,-0.2) pic{question={\thequestionsi,#1}};}}]%
}

\newlist{questions}{enumerate}{1}% define a new enumerate environment
\setlist[questions]{  % set properties of the questions environment
   label=\arabic*.,   % label format
   leftmargin=*,      % indent labels
   before=\let\item\fakeitem, % change the \item command
   resume,            % continuous numbering
}

\begin{document}

      \begin{questions}[start=32]
        \item[2] \lipsum[4]
        \item[3] \lipsum[4]
        \item[4] \lipsum[3]
      \end{questions}

\end{document}

and here is the output:

enter image description here

Addendum

I small tweak of the \fakeitem macro answers the followup question in the post:

\documentclass{article}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{lipsum}

\tikzset{
  pics/question/max/.initial = 5, % maximum number of balls
  pics/question/.style args={#1,#2}{
             code={
                \node[teal] at (0.5,0.35) {\textsf{#1}};
                \fill[gray!30] (0.1,0) rectangle (0.9,0.2);
                \def\qmax{\pgfkeysvalueof{/tikz/pics/question/max}}
                 \foreach \ball [evaluate=\ball as \x using {0.1+(\ball-0.5)*0.8/\qmax}] in {1,...,\qmax} {
                    \fill[\ifnum\ball>#2 teal\else red\fi] (\x,0.1) circle (2.5pt);
                }
            }
        }
}

\let\realitem\item% save a copy of the \item command
\newcommand\fakeitem[1][\relax]{%
   \refstepcounter{questionsi}% increment the counter
   \realitem[\smash{\tikz[baseline]{\draw(0,-0.2) pic{question={\thequestionsi,#1}};}}]%
}

\newlist{questions}{enumerate}{1}% define a new enumerate environment
\setlist[questions]{  % set properties of the questions environment
   label=\arabic*.,   % label format
   leftmargin=*,      % indent labels
   before=\let\item\fakeitem, % change the \item command
   resume,            % continuous numbering
}

\begin{document}

      \begin{questions}[start=32]
        \item[2] \lipsum[4]
        \item[3] \lipsum[4]
        \item[4] \lipsum[3]
      \end{questions}

\end{document}

By default, 5 dots are printed for each question. This can be changed using, for example, \tikzset{pics/question/max=4} to set it to 4 dots. In theory this could be arbitrary but in practice it should be in the range 2-5. Here is a sample of the new output:

enter image description here

  • Hi, Andrew. Also your solution is very good. Can you see this link, please? tex.stackexchange.com/questions/343117/…. So kindly would you be able to modify my source of this link, giving me the opportunity to change the size of the numbers with a font that is very similar to that of the image (Agenda.ttf), put a paragraph with the same font, with the number 5 in gray and the rest in the red? – Sebastiano Dec 9 '16 at 12:45
  • Now I have visited this link tex.stackexchange.com/questions/167532/fontfamily-with-xelatex. As is it possible change my source with the considerations at the link tex.stackexchange.com/questions/343117/…? – Sebastiano Dec 9 '16 at 20:36
  • @Sebastiano I don't know anything about fonts:) Assuming that you have loaded the fonts you should be able to use them? Can you say more clearly in the question what you actually want? That is, explicitly say what fonts, and their sizes, you want to use, and where, in this environment. It would help if you could also cut your question down to a minimal working example, so that it was clear what is actually needed to anwer your question. – Andrew Dec 10 '16 at 15:02
  • Andrew I put above my MME, now. – Sebastiano Dec 10 '16 at 20:20
  • @Sebastiano Hi! I was suggesting that you edit your follow-up question to make it clearer what you want. Please don't post code as an answer when it is meant to give more information to your question and please reduce the code (in your other question) so that it is as small as possible to demonstrate what your problem is. Posting a large chunk of code does not help people understand what you need. If in doubt consult the definition of a minimal working example. – Andrew Dec 11 '16 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.