[Tex/LaTex] Advanced macro with character recognition

luatexmacros

I would like to code an advanced macro that would look like

\begin{exercise}[....]
    bla bla bla
\end{exercise}

In the brackets [....], I would put strings like :

  • FFFHHC
  • F
  • HHF
  • CCH
  • ?
  • etc.

The idea is to count to the number of F, H and C and take different actions depending on the result. In my mind, FFF will print three "lightning" symbols next to the exercise, H will print one star, HH two stars, CC two computer symbols, ? will print one question mark, etc.

(F stand for "flash", H for "hard", C for "computer", ? for "unknown", etc.)

What direction should I take? Do you have any hint (like somewhere where I could copy some code)? Any package that would help me?

I would prefer a LaTeX only solution but I'm also interested to know if LuaLaTeX can make this kind of things much easier.

Best Answer

enter image description here

\documentclass{article}

\newenvironment{zzz}[1][]{%
\quote\strut\marginpar{\zzzz#1!}\ignorespaces}
{\endquote}

\def\zzzz#1{\csname zzz#1\endcsname\zzzz}

\def\zzzH{$\ast$}
\def\zzzF{$|$}
\def\zzzC{$\,\rule{4pt}{5pt}\,$}
\expandafter\def\csname zzz?\endcsname{?}
\expandafter\def\csname zzz!\endcsname#1{}

\begin{document}

\begin{zzz}
  one two three
\end{zzz}

\begin{zzz}[CCCH]
  one two three
\end{zzz}

\begin{zzz}[FF]
  one two three
\end{zzz}

\begin{zzz}[CF?]
  one two three
\end{zzz}


\end{document}

This directly prints the symbols but you could of course make the macros increment some counter instead and defer all printing until after the whole collection is known.

\documentclass{article}

\newenvironment{zzz}[1][]{%
\quote\strut\marginpar{%
\Hc=0
\Fc=0
\Cc=0
\Qc=0
\zzzz#1!%
There are \the\Cc\ C and \the\Fc\ F}\ignorespaces}
{\endquote}

\def\zzzz#1{\csname zzz#1\endcsname\zzzz}

\newcount\Hc
\newcount\Fc
\newcount\Cc
\newcount\Qc

\def\zzzH{\advance\Hc1 }
\def\zzzF{\advance\Fc1 }
\def\zzzC{\advance\Cc1 }
\expandafter\def\csname zzz?\endcsname{\advance\Qc1 }
\expandafter\def\csname zzz!\endcsname#1{}

\begin{document}

\begin{zzz}
  one two three
\end{zzz}

\begin{zzz}[CCCH]
  one two three
\end{zzz}

\begin{zzz}[FF]
  one two three
\end{zzz}

\begin{zzz}[CF?]
  one two three
\end{zzz}


\end{document}

enter image description here