使用 Tikz 绘制星星/类似图案

使用 Tikz 绘制星星/类似图案

我目前正在为一本书写评论,我想通过添加一个 x-out-of-y 星形图形来展示 LaTeX 的功能。Tikz 可以做很多事情,所以我原本以为这样的技巧相当简单,但是我似乎无法找出一个宏来以正确的大小绘制形状,更不用说半满星形或类似的东西之类的花哨技巧了。我认为到目前为止我拥有的代码可能朝着正确的方向发展:

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{calc,shadows}

\newcommand*\starfill{%
  \tikz[baseline=(key.base),scale=-3]
  \node[star, star points=5, star point ratio=2.25, fill=black, draw](key) {S};  
}

我尝试先绘制一个星形,但理想情况下,我希望像这样定义,这样就可以输出星形的\starsranking{number}{total}适当阴影。这可行吗?听起来不是特别困难。numbertotal

答案1

这是完全填充的星星的代码,现在由于 Andrew Stacey 的改进而略有改进棋盘问题的答案

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\newcommand\score[2]{%
  \pgfmathsetmacro\pgfxa{#1 + 1}%
  \tikzstyle{scorestars}=[star, star points=5, star point ratio=2.25, draw, inner sep=1.3pt, anchor=outer point 3]%
  \begin{tikzpicture}[baseline]
    \foreach \i in {1, ..., #2} {
      \pgfmathparse{\i<=#1 ? "yellow" : "gray"}
      \edef\starcolor{\pgfmathresult}
      \draw (\i*1.75ex, 0) node[name=star\i, scorestars, fill=\starcolor]  {};
   }
  \end{tikzpicture}%
}

\begin{document}
\score{0}{5} A meagre result.

\score{4}{5} Much better

\score{5}{5} Perfect score!

\end{document}

使用 tikz 对星星进行排名


下面是更加复杂、更加无意义的浮点计分星宏(我也会保留简单的宏,因为它更加实用):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, calc}

\newcommand\score[2]{%
  \pgfmathsetmacro\pgfxa{#1 + 1}%
  \tikzstyle{scorestars}=[star, star points=5, star point ratio=2.25, draw, inner sep=0.15em, anchor=outer point 3]%
  \begin{tikzpicture}[baseline]
    \foreach \i in {1, ..., #2} {
      \pgfmathparse{\i<=#1 ? "yellow" : "gray"}
      \edef\starcolor{\pgfmathresult}
      \draw (\i*1em, 0) node[name=star\i, scorestars, fill=\starcolor]  {};
    }
    \pgfmathparse{#1>int(#1) ? int(#1+1) : 0}
    \let\partstar=\pgfmathresult
    \ifnum\partstar>0
      \pgfmathsetmacro\starpart{#1-(int(#1)}
      \path [clip] ($(star\partstar.outer point 3)!(star\partstar.outer point 2)!(star\partstar.outer point 4)$) rectangle 
      ($(star\partstar.outer point 2 |- star\partstar.outer point 1)!\starpart!(star\partstar.outer point 1 -| star\partstar.outer point 5)$);
      \fill (\partstar*1em, 0) node[scorestars, fill=yellow]  {};
    \fi
  \end{tikzpicture}%
}

\begin{document}
\score{0}{5} That's appalling!

\small\score{2}{5} A meagre result.

\Huge{\score{4.4}{5} Wooo!}

\end{document}

在此处输入图片描述

答案2

在此处输入图片描述

Asymptote版本stars.asy

size(200);
real sc=20;

picture score(real scoreMark=0,guide star=scale(sc)*unitcircle, int maxscore=5, 
  pen linePen=nullpen, pen bgPen=darkblue, pen scorePen=orange){
  picture pic;
  guide[] g;  
  for(int i=0;i<maxscore;++i){
    g.push(shift((2sc*i,0))*star);
  }
  assert(maxscore>0 && scoreMark>=0 && scoreMark<=maxscore,"***** Wrong score.");
  fill(pic,box((-sc,-sc),(maxscore*2sc-sc,sc)),bgPen);
  fill(pic,box((-sc,-sc),(scoreMark*2sc-sc,sc)),scorePen);
  clip(pic,g);
  draw(pic,g,linePen);
  return pic;
}

guide star;
pair p;
for(int i=0;i<5;++i){
  p=rotate(72*i)*N;
  star=star--p;
  star=star--(scale(0.382)*rotate(72*i+36)*N);
}
star=scale(sc)*(star--cycle);

add(score(scoreMark=1,star,maxscore=7),(0,0));
add(score(scoreMark=2,star,maxscore=7,linePen=lightred,bgPen=lightblue),(0,-3sc));
add(score(scoreMark=3.5,star,maxscore=5,linePen=lightred,bgPen=lightblue),(0,-6sc));
add(score(scoreMark=3.75,star,maxscore=4,linePen=olive,bgPen=white,scorePen=lightgreen),(0,-9sc));

add(score(4.2,bgPen=green+opacity(0.3),scorePen=red+opacity(0.5)),(0,3sc));

为了独立stars.pdf运行asy -f pdf stars.asy

答案3

使用 PSTricks。

在此处输入图片描述

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{pstricks}
\usepackage{multido}
\SpecialCoor
\makeatletter
\def\LoadPSVars{\pstVerb{/ptcm {\pst@number\psunit div} bind def}}
\makeatother

\def\points{}
\def\Star{%
    \xdef\points{}% cleaning
    \multido{\iR=0+72,\ir=36+72}{5}{\xdef\points{\points (10pt;\iR)(5pt;\ir)}}
    \expandafter\pspolygon\points}

\def\Rating#1{% #1: percentage
    \psscalebox{0.35}{%
    \begin{pspicture}(11pt,-11pt)(111pt,11pt)
    \LoadPSVars
    \psclip{\pscustom{\psLoop{5}{\translate(20pt,0)\Star}}}
        \psframe*[linecolor=yellow](11pt,-11pt)(!#1 11 add ptcm 11 ptcm)
    \endpsclip
    \pscustom{\psLoop{5}{\translate(20pt,0)\Star}}
    \end{pspicture}}}

\begin{document}
\begin{enumerate}
    \item \Rating{100} PSTricks
    \item \Rating{50} Asymptote
    \item \Rating{20} Metapost
    \item \Rating{5} TikZ
\end{enumerate}
\end{document}

警告:

  • \rput在 内不起作用\pscustom。请使用\translate
  • standalone丢弃了我ptcm在序言中定义的操作符,所以我必须在里面手动加载它pspicture。真可悲!

答案4

Jake 的回答非常好。以下是额外的半颗星:

\node[scorestars,fill=gray] {};
\path node[scorestars,fill=yellow] (s) {} [clip] (s.south west) rectangle (s.north);

相关内容