我正在尝试让 TikZ 制作一种经验栏命令,我只需调用它并输入一个数字,比如 6,它就会返回那么多“填充”点,以及 6 减去那么多“未填充”点。问题是,“foreach”命令计算大小的方式让它变得一团糟,要么是添加太多点,要么是点不够,要么是位置不对,或者其他原因(取决于我对索引边界的具体处理方式)
例如:
\newcommand{\dotexp}[1]{
\tikz{\foreach \x in {1,...,#1}
\draw[cyan,fill=cyan] (\x/2.5,0) circle (.5ex);
\ifnum#1<6 \foreach \y in {#1,...,6} \draw[lightgray,fill=lightgray] (\y/2.5,0) circle (.5 ex);\fi}
}
给我
其中每一行都应该0, 1, 2,....6
按顺序调用点。
有什么修复建议吗?也许可以预先分配点的位置,但只改变颜色?
答案1
\documentclass{article}
\usepackage{tikz}
\newcommand{\dotexp}[1]{%
\edef\DEnum{\the#1}%
\tikz{%
\foreach \x in {1,...,6}{%
\ifnum\x>\DEnum
\edef\DEfill{lightgray}%
\else
\edef\DEfill{cyan}%
\fi
\draw[\DEfill,fill=\DEfill] (\x/2.5,0) circle (.5ex);%
}%
}%
}
\begin{document}
\newcount\x
\x=0
\loop
\the\x\dotexp{\x}
\advance \x by 1
\ifnum\x<7
\repeat
\end{document}
答案2
方法略有不同,\foreach
同样生成 7 个变体。输出与 Phelype 的答案相同。
\documentclass{article}
\usepackage{tikz}
\newcommand{\dotexp}[1]{%
\begin{tikzpicture}
\foreach \tmpx in {0,...,5} {
\ifnum \tmpx < #1
\filldraw[cyan] (\tmpx/2.5,0) circle[radius=.5ex];
\else
\filldraw[lightgray] (\tmpx/2.5,0) circle[radius=.5ex];
\fi
}
\end{tikzpicture}%
}
\begin{document}
\foreach \x in {0,...,6}
{ \x\ \dotexp{\x} \par }
\end{document}
答案3
使用 MetaPost 和 LuaLaTeX 进行一次粗略的尝试:
\documentclass{article}
\usepackage{luamplib}
\everymplib{%
input mpcolornames;
verbatimtex \leavevmode etex;
def dotexp(expr n, loc) =
u := .4cm;
label(textext(decimal n), loc);
pickup pencircle scaled \mpdim{1ex};
if n <= 6:
for i = 1 upto n: drawdot loc + (i*u, 0) withcolor Cyan; endfor;
for i = n+1 upto 6: drawdot loc + (i*u, 0) withcolor LightGray; endfor;
fi
enddef;
beginfig(1);}
\everyendmplib{endfig;}
\begin{document}
\begin{mplibcode}
for i= 0 upto 6:
dotexp(i, (0, -i*\mpdim{\bigskipamount}));
endfor;
\end{mplibcode}
\end{document}
答案4
只是为了好玩,这里有一个 tikz“仅风格”解决方案。
\documentclass[varwidth,border=7pt]{standalone}
\usepackage{tikz}
\tikzset{
% one node insert
insert circle/.style={insert path={++(1,0) node[#1,fill,circle]{}}},
cyan circle/.style={insert circle=cyan},
gray circle/.style={insert circle=lightgray},
% the loops (the values 0 and 6 are particular cases with one empty loop)
c0/.style={gray circle/.list={0,...,5}},
c6/.style={cyan circle/.list={0,...,5}},
c/.style={cyan circle/.list={1,...,#1},gray circle/.list={#1,...,5}},
}
\newcommand{\dotexp}[1]{\tikz\path[c#1/.try,c/.retry=#1];}
\begin{document}
\dotexp{0}\\
\dotexp{1}\\
\dotexp{2}\\
\dotexp{3}\\
\dotexp{4}\\
\dotexp{5}\\
\dotexp{6}
\end{document}
第二种解决方案:.is choice
这是使用(不带.try
和.retry
)和的第二种解决方案这个答案。
\documentclass[varwidth,border=7pt]{standalone}
\usepackage{tikz}
\def\test{3}
\tikzset{
% one node insert
insert circle/.style={insert path={++(1,0) node[#1,fill,circle]{}}},
cyan circle/.style={insert circle=cyan},
gray circle/.style={insert circle=lightgray},
% the loops (the values 0 and 6 are particular cases with one empty loop)
c/.is choice,
c/0/.style={gray circle/.list={0,...,5}},
c/6/.style={cyan circle/.list={0,...,5}},
c/.unknown/.style={cyan circle/.list={1,...,\pgfkeyscurrentchoice},gray circle/.list={\pgfkeyscurrentchoice,...,5}},
c/.unknown/.prefix code={\edef\pgfkeyscurrentchoice{\pgfkeyscurrentname}}
}
\newcommand{\dotexp}[1]{\tikz\path[c=#1];}
\begin{document}
\dotexp{0}\\
\dotexp{1}\\
\dotexp{2}\\
\dotexp{3}\\
\dotexp{4}\\
\dotexp{5}\\
\dotexp{6}
\end{document}