Forgot password?
 Create new account
View 7673|Reply 9

tikz画带圈数字

[Copy link]

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

kuing Posted at 2014-10-27 21:13:15 |Read mode
  1. \newlength\szg
  2. \newcommand\quan[1]{%
  3. \settoheight\szg{#1}%
  4. \tikz[baseline]{\pgfmathparse{
  5. ifthenelse(#1 < 10, 1, ifthenelse(#1 < 100, 0.75, 0.5))
  6. }
  7. \let\hfs\pgfmathresult
  8. \node at (0,\szg/2) {\makebox[0em][c]{\scalebox{\hfs}[1]{#1}}};
  9. \draw (0,\szg/2) circle (\szg/2+0.35ex);
  10. }}
Copy the Code
设计的思路很简单,就懒得作解释了。

用法:\quan{数字}
其中,数字的取值范围为不超过999的自然数,如果数字不在这范围内,可能会出现溢出现象或者报错。

代码 \foreach \i in {0,1,...,100} { \quan\i} 的效果:
QQ截图20141027220447.gif

测试文档: $type tikzquannum.tex (719 Bytes, Downloads: 879)

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

 Author| kuing Posted at 2014-10-27 21:17:08
有一件奇怪的事,我本来是直接写 \scalebox{\pgfmathresult}[1]{#1},但是居然出不来正确的效果,而先 \let\hfs\pgfmathresult 再 \scalebox{\hfs}[1]{#1} 就正常了,不知为什么。
PS、我这里的测试环境是 texlive2013 ,用 xelatex 编译,不知新版会不会这样。

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

 Author| kuing Posted at 2014-10-27 22:07:58
还有,我在1楼的测试文档里忘了加 \usepackage{graphicx} ,居然也能编译? 为毛印象中 \scalebox 应该是 graphicx 宏包里的命令?

3

Threads

42

Posts

249

Credits

Credits
249

Show all posts

羊羊羊羊 Posted at 2014-10-27 23:06:48
这字不一般,不是一般的丑啊。

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

 Author| kuing Posted at 2014-11-19 14:38:42
居然忘记弄黑圈,类似地设计为:
  1. \newcommand\hquan[1]{%
  2. \settoheight\szg{#1}%
  3. \tikz[baseline]{\pgfmathparse{
  4. ifthenelse(#1 < 10, 1, ifthenelse(#1 < 100, 0.75, 0.5))
  5. }
  6. \let\hfs\pgfmathresult
  7. \filldraw (0,\szg/2) circle (\szg/2+0.35ex);
  8. \node[white] at (0,\szg/2) {\makebox[0em][c]{\scalebox{\hfs}[1]{\textbf{#1}}}};
  9. }}
Copy the Code
设计时有两点值得记录:
一是我用 \filldraw 而不用 \fill ,是为了和白圈的大小完全一致;
二是这里我加了 \textbf 是为了好看些,如果不加,会显得数字的线条很细,这大概是黑底白字的视觉原因。
QQ截图20141119143846.png

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

 Author| kuing Posted at 2014-11-20 00:53:04
其实还可以加颜色之类的可选参数,不过一般来说也没什么必要

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

 Author| kuing Posted at 2015-12-12 02:02:41
若要用于脚注,则只需加一句:

\renewcommand\thefootnote{\protect\quan{\number\value{footnote}}}

测试文件: $type tikzquannum2.tex (904 Bytes, Downloads: 821)

顺便改进了一处,画圈的时候用 \draw[line width=0.1ex] ,这样即使字号变大,线粗也跟着变大,原先1#的是不变的。

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

 Author| kuing Posted at 2017-8-24 18:26:05
前段时间在 bbs.ctex.org/forum.php?mod=viewthread&tid=153386 继续讨论了相关问题,忘了存档这里了。

隔了一段时间,当时的理解都有点模糊了,只能大概整理下了。
该帖中遇到的问题大概是跟命令的“脆弱”有关,要加强保护,如:
将 \newcommand\quan[1]{... 改为 \protected\def\quan#1{...
后面相关的东西也有可能要改了,像楼上的就没必要再用 \protect 了,还有就是控制好展开顺序,即
\renewcommand\thefootnote{\expandafter\quan\expandafter{\number\value{footnote}}}
酱紫。

晚点再弄个测试文档。

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

hbghlyj Posted at yesterday 10:12
测试\textcircled

测试enclose
$$\enclose{circle}{123}\;
\enclose{circle}[mathcolor=red]{123}\;
\enclose{circle}[mathcolor=red,mathbackground=blue]{123}\;
\enclose{bottom,circle,top}{123}$$

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

hbghlyj Posted at yesterday 10:27

手机版Mobile version|Leisure Math Forum

2025-4-20 12:09 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list