How to implement the five star notation in Latex

Welcome! To first approximation you could do

\documentclass{article}
\usepackage{xcolor}
\usepackage{amssymb}
\usepackage{scalerel}
\usepackage{stackengine}
\newcounter{iloop}
\newcommand\openbigstar[1][0.7]{%
  \scalerel*{%
    \stackinset{c}{-.125pt}{c}{}{\scalebox{#1}{\color{white}{$\bigstar$}}}{%
      $\bigstar$}%
  }{\bigstar}
}
\newcommand{\Stars}[1]{\ensuremath{\setcounter{iloop}{0}%
\loop\stepcounter{iloop}\ifnum\value{iloop}<#1
\bigstar\repeat
\openbigstar[0.5]
\setcounter{iloop}{0}%
\loop\stepcounter{iloop}\ifnum\value{iloop}<\the\numexpr6-#1\relax
\openbigstar[.9]\repeat}}
\begin{document}
\Stars{5}

\Stars{4}

\Stars{3}

\Stars{2}

\Stars{1}
\end{document}

enter image description here

It is not clear to me whether you also want to award fractional stars.

ADDENDUM: If you want to allow for fractional ratings, you could do

\documentclass{article}
\usepackage{xcolor}
\usepackage{amssymb}
\usepackage{scalerel}
\usepackage{stackengine}
\usepackage{pgf}
\newcounter{iloop}
\newcommand\openbigstar[1][0.7]{%
  \scalerel*{%
    \stackinset{c}{-.125pt}{c}{}{\scalebox{#1}{\color{white}{$\bigstar$}}}{%
      $\bigstar$}%
  }{\bigstar}
}
\newcommand{\Stars}[1]{\ensuremath{%
\pgfmathtruncatemacro{\imax}{ifthenelse(int(#1)==#1,#1-1,#1)}%
\pgfmathsetmacro{\xrest}{0.9*(1-#1+\imax)}%
\setcounter{iloop}{0}%
\loop\stepcounter{iloop}\ifnum\value{iloop}<\the\numexpr\imax+1
\bigstar\repeat
\openbigstar[\xrest]%
\setcounter{iloop}{0}%
\loop\stepcounter{iloop}\ifnum\value{iloop}<\the\numexpr5-\imax\relax
\openbigstar[.9]\repeat}}
\begin{document}
\Stars{5}

\Stars{4.2}

\Stars{3.6}

\Stars{2}

\Stars{1.3}

\Stars{0.3}

\end{document}

enter image description here

Or a TikZ version, which is obviously much more customizable.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\newcommand{\Stars}[2][fill=yellow,draw=orange]{\begin{tikzpicture}[baseline=-0.35em,#1]
\foreach \X in {1,...,5}
{\pgfmathsetmacro{\xfill}{min(1,max(1+#2-\X,0))}
\path (\X*1.1em,0) 
node[star,draw,star point height=0.25em,minimum size=1em,inner sep=0pt,
path picture={\fill (path picture bounding box.south west) 
rectangle  ([xshift=\xfill*1em]path picture bounding box.north west);}]{};
}
\end{tikzpicture}}
\begin{document}
\Stars{4.5}

\Stars{4.2}

\Stars{3.6}

\Stars{2}

\Stars{1.3}

\Stars{0.3}
\end{document}

enter image description here


TeX is technically Turing-complete, but some programming concepts are a bit more difficult than in other languages. Loops are one. But they are certainly doable. I don't know where you're getting \bigstar from, so I can't do that specifically; but here's the general, TeX way to do a loop:

\documentclass{article}
\begin{document}
\newcount\loopi\loopi=0
\loop\ifnum\loopi<5
    Hey
    \advance\loopi by1
\repeat
\end{document}

You use \loop, then an \ifnum to give your condition. It will then repeat whatever is between that \ifnum and your \repeat command. Make sure that you increment your loop-control variable (here, \loopi) within your loop, or you'll get an infinite loop.

The above will print "Hey" five times. I understand that there are more LaTeXy ways to do this, and that pgf may have some very easy loop commands; but I can't speak to them. This is the basic TeX way, anyway.

Your specific question seems to require manipulating fractional values, which is also a bit tougher in TeX; maybe look into the fp package? You could also perhaps use an integer and just append it to 0.:

\newcount\loopi\loopi=1
\loop\ifnum\loopi<6
    0.\the\loopi,
    \advance\loopi by1%
\repeat

That will print 0.1,0.2,0.3,0.4,0.5; maybe you could use this in your \openbigstar commands.

Tags:

Loops

Symbols