我想创建一个宏,以最有效的方式自动填充百分比网格

我想创建一个宏,以最有效的方式自动填充百分比网格

我想创建一个宏来填充百分比网格,如果我调用\mypercent{43},它将填充 43 个方格。我正在手动完成此操作,但希望有人能帮助实现该过程的自动化。这是我的代码。

\documentclass[11pt,fleqn]{examdesign}
\usepackage{savesym}
\usepackage{amsmath}
\usepackage{pifont}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{xspace}
\usepackage{xcolor}
\SectionFont{\large\sffamily}
\usepackage[shortlabels,inline]{enumitem}  %%shortlabels here for matchign
\usepackage{cancel}
\usepackage{tkz-euclide} %%%%%%%%%%%for marking angles
\usetkzobj{all}  %%%%%%%%%%%because of Overleaf
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc,matrix}
\usepackage[font=scriptsize,hypcap=false]{caption}
\Fullpages
\ContinuousNumbering
\ShortKey
%%\NoKey
\DefineAnswerWrapper{}{}
\NumberOfVersions{1}
%%%%%Title of the Assignment
\class{needed for examdesign}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\begin{shortanswer}[title={Review of Percent},rearrange=no,resetcounter=yes]
\begin{question}
Shade in the percent grids to show the indicated percent.
\begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
%\newcommand\mypercent{43}%This is the call. 
\item $43\%$\\
\begin{tikzpicture}[scale=0.3]
%%%%%want the following to be in a macro
\foreach\x in {1,...,4}{
 \foreach \y in {0,-1,...,-10}
 \draw[draw=black,fill=black!20!white] (0,0) grid(\x,\y) rectangle (0,0);}
  \foreach \x in {4}{
 \foreach \y in {-3}
 \draw[draw=black,fill=black!20!white,line width=\pgflinewidth/2] (\x,0) grid(\x+1,\y) rectangle (\x,0);}
 %%%so the grid will draw over the top
 \draw [] (0,0) grid(10,-10);
  \end{tikzpicture}
    \end{minipage}%
    \hfill
\begin{answer}
answer here
    \end{answer}

\end{question}
\end{shortanswer}

\end{document}

这是图片。 百分比示例 我想知道这个过程是否可以使用 if 条件然后 for 循环。例如:如果数字小于 10,则填写一列中的方块数,如果大于 10,则填写一列,移至下一列,将数字倒数 10,然后重复循环。在小于 10 的条件之后退出循环。在我的代码中,我必须在顶部绘制最后的网格以确保线条越过顶部。

我把网格向下移动,使倒计时更加直观。提前感谢大家。

答案1

这是实现此目的的一种可能方法。我们首先定义一张图片。

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=0.3,pics/percent grid/.style={code={
    \tikzset{percent grid/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/percent grid/##1}}%
    \pgfmathtruncatemacro{\nx}{\pv{p}/10}
    \pgfmathtruncatemacro{\ny}{Mod(\pv{p},10)}
    \ifnum\nx=0
     \ifnum\ny=0
     \else
      \path[percent grid/pfill] (-5,5) rectangle (-4,5-\ny);
     \fi
    \else
     \ifnum\ny=0
      \path[percent grid/pfill] (-5,5) rectangle (-5+\nx,-5);
     \else
      \path[percent grid/pfill] (-5,5) |- (-5+\nx,-5) |- (-5+\nx+1,5-\ny) |-cycle;
     \fi
    \fi
    \draw[line cap=rect] (-5,-5) grid (5,5);
 }},percent grid/.cd,p/.initial=0,pfill/.style={fill=black!20!white}]

 \path[transform shape] (0,0) pic{percent grid={p=43}}
  (12,0) pic{percent grid={p=3}}
  (0,-12) pic{percent grid={p=50}}
  (12,-12) pic{percent grid={p=100}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

这是其宏版本。

\documentclass{article}
\usepackage{tikz}
\tikzset{pics/percent grid/.style={code={
    \tikzset{percent grid/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/percent grid/##1}}%
    \pgfmathtruncatemacro{\nx}{\pv{p}/10}
    \pgfmathtruncatemacro{\ny}{Mod(\pv{p},10)}
    \ifnum\nx=0
     \ifnum\ny=0
     \else
      \path[percent grid/pfill] (-5,5) rectangle (-4,5-\ny);
     \fi
    \else
     \ifnum\ny=0
      \path[percent grid/pfill] (-5,5) rectangle (-5+\nx,-5);
     \else
      \path[percent grid/pfill] (-5,5) |- (-5+\nx,-5) |- (-5+\nx+1,5-\ny) |-cycle;
     \fi
    \fi
    \draw[line cap=rect] (-5,-5) grid (5,5);
 }},percent grid/.cd,p/.initial=0,pfill/.style={fill=black!20!white}}
\newcommand{\mypercent}[2][]{\begin{tikzpicture}[#1]
\pic[scale=0.3]{percent grid={p=#2}};
\end{tikzpicture}} 
\begin{document}
\mypercent{43}
\end{document}

在此处输入图片描述

答案2

谁需要TikZ

\documentclass{article}
\usepackage{pgffor,xcolor,graphicx}
\newcounter{z}
\newcommand\shazam[1]{%
\setcounter{z}{-#1}%
\rotatebox[origin=c]{90}{%
\scalebox{-1}[1]{%
\fboxsep=0pt\relax%
\fbox{\parbox{100pt}{\fontsize{10pt}{10pt}\selectfont%
\fboxsep=-\fboxrule\relax%
\foreach\z in{1,...,10}{%
\foreach\zz in{1,...,10}{%
\ifnum\thez<0\relax\fbox{\color{gray}\rule{10pt}{10pt}}\else
  \fbox{\color{white}\rule{10pt}{10pt}}\fi
\stepcounter{z}%
}%
\ifnum\z<10\\\fi
}}}}}}
\begin{document}
\shazam{43}
\shazam{17}\medskip

\shazam{89}
\shazam{67}
\end{document}

在此处输入图片描述

飞涨:

在此处输入图片描述

相关内容