今天看了tikz的一些知识,顺便做一个实时时钟,主要用到的宏包是tikz和time两个宏包,编译出可以显示现在的时间和日期,刚学tikz,请各位大神指点一下代码部分有什么改进的地方

效果图如下

10.png

代码如下

\documentclass[border=0mm]{standalone}
\usepackage{ctex}
\usepackage{tikz}
\usepackage{time}
\begin{document}
\newcommand{\mytime}{\the\time}

\begin{tikzpicture}[domain=-2:2]
\draw (0,0) circle (2cm);
\fill (0,0) circle (.7mm);  
\draw[->] (0,0) -- (90-\mytime*6:1.5);  %minute
\draw[->>] (0,0) -- (90-\mytime/2:1);  %hour
\node[red,below = 8mm] at (0,0) {\small\now};
\node[above = 8mm] at (0,0) {\tiny \TeX 白兔};
\node[red,below = 2cm] at (0,0) {\small \today};

%%%%%%%%%1-12%%%%%%%%%%%%%
\node at (90:1.8) {12};
\node at (60:1.8) {1};
\node at (30:1.8) {2};
\node at (0:1.8) {3};
\node at (-30:1.8) {4};
\node at (-60:1.8) {5};
\node at (-90:1.8) {6};
\node at (-120:1.8) {7};
\node at (-150:1.8) {8};
\node at (-180:1.8) {9};
\node at (-210:1.8) {10};
\node at (-240:1.8) {11};

%%%%%%%%%%%大节点%%%%%%%%%%%%%%%%%%%
\node at (90:2cm-1pt) {\rotatebox{0}{\rule{.8pt}{2pt}}};
\node at (60:2cm-1pt) {\rotatebox{-30}{\rule{.8pt}{2pt}}};
\node at (30:2cm-1pt) {\rotatebox{-60}{\rule{.8pt}{2pt}}};
\node at (0:2cm-1pt) {\rotatebox{-90}{\rule{.8pt}{2pt}}};
\node at (-30:2cm-1pt) {\rotatebox{-120}{\rule{.8pt}{2pt}}};
\node at (-60:2cm-1pt) {\rotatebox{-150}{\rule{.8pt}{2pt}}};
\node at (-90:2cm-1pt) {\rotatebox{-180}{\rule{.8pt}{2pt}}};
\node at (-120:2cm-1pt) {\rotatebox{-210}{\rule{.8pt}{2pt}}};
\node at (-150:2cm-1pt) {\rotatebox{-240}{\rule{.8pt}{2pt}}};
\node at (-180:2cm-1pt) {\rotatebox{-270}{\rule{.8pt}{2pt}}};
\node at (-210:2cm-1pt) {\rotatebox{-300}{\rule{.8pt}{2pt}}};
\node at (-240:2cm-1pt) {\rotatebox{-330}{\rule{.8pt}{2pt}}};

%%%%%%%%%%%%%%%%%%%%%%%%小节点%%%%%%%%%%%%%%%%%%%%%%%%%%%
\node at (90-6*1:2cm-0.5pt) {\rotatebox{-6}{\rule{.4pt}{1pt}}};
\node at (90-6*2:2cm-0.5pt) {\rotatebox{-12}{\rule{.4pt}{1pt}}};
\node at (90-6*3:2cm-0.5pt) {\rotatebox{-18}{\rule{.4pt}{1pt}}};
\node at (90-6*4:2cm-0.5pt) {\rotatebox{-24}{\rule{.4pt}{1pt}}};
\node at (90-6*5:2cm-0.5pt) {\rotatebox{-30}{\rule{.4pt}{1pt}}};
\node at (90-6*6:2cm-0.5pt) {\rotatebox{-36}{\rule{.4pt}{1pt}}};
\node at (90-6*7:2cm-0.5pt) {\rotatebox{-42}{\rule{.4pt}{1pt}}};
\node at (90-6*8:2cm-0.5pt) {\rotatebox{-48}{\rule{.4pt}{1pt}}};
\node at (90-6*9:2cm-0.5pt) {\rotatebox{-54}{\rule{.4pt}{1pt}}};
\node at (90-6*10:2cm-0.5pt) {\rotatebox{-60}{\rule{.4pt}{1pt}}};
\node at (90-6*11:2cm-0.5pt) {\rotatebox{-66}{\rule{.4pt}{1pt}}};
\node at (90-6*12:2cm-0.5pt) {\rotatebox{-72}{\rule{.4pt}{1pt}}};
\node at (90-6*13:2cm-0.5pt) {\rotatebox{-78}{\rule{.4pt}{1pt}}};
\node at (90-6*14:2cm-0.5pt) {\rotatebox{-84}{\rule{.4pt}{1pt}}};
\node at (90-6*15:2cm-0.5pt) {\rotatebox{-90}{\rule{.4pt}{1pt}}};
\node at (90-6*16:2cm-0.5pt) {\rotatebox{-96}{\rule{.4pt}{1pt}}};
\node at (90-6*17:2cm-0.5pt) {\rotatebox{-102}{\rule{.4pt}{1pt}}};
\node at (90-6*18:2cm-0.5pt) {\rotatebox{-108}{\rule{.4pt}{1pt}}};
\node at (90-6*19:2cm-0.5pt) {\rotatebox{-114}{\rule{.4pt}{1pt}}};
\node at (90-6*20:2cm-0.5pt) {\rotatebox{-120}{\rule{.4pt}{1pt}}};
\node at (90-6*21:2cm-0.5pt) {\rotatebox{-126}{\rule{.4pt}{1pt}}};
\node at (90-6*22:2cm-0.5pt) {\rotatebox{-132}{\rule{.4pt}{1pt}}};
\node at (90-6*23:2cm-0.5pt) {\rotatebox{-138}{\rule{.4pt}{1pt}}};
\node at (90-6*24:2cm-0.5pt) {\rotatebox{-144}{\rule{.4pt}{1pt}}};
\node at (90-6*25:2cm-0.5pt) {\rotatebox{-150}{\rule{.4pt}{1pt}}};
\node at (90-6*26:2cm-0.5pt) {\rotatebox{-156}{\rule{.4pt}{1pt}}};
\node at (90-6*27:2cm-0.5pt) {\rotatebox{-162}{\rule{.4pt}{1pt}}};
\node at (90-6*28:2cm-0.5pt) {\rotatebox{-168}{\rule{.4pt}{1pt}}};
\node at (90-6*29:2cm-0.5pt) {\rotatebox{-174}{\rule{.4pt}{1pt}}};
\node at (90-6*30:2cm-0.5pt) {\rotatebox{-180}{\rule{.4pt}{1pt}}};
\node at (90-6*31:2cm-0.5pt) {\rotatebox{-186}{\rule{.4pt}{1pt}}};
\node at (90-6*32:2cm-0.5pt) {\rotatebox{-192}{\rule{.4pt}{1pt}}};
\node at (90-6*33:2cm-0.5pt) {\rotatebox{-198}{\rule{.4pt}{1pt}}};
\node at (90-6*34:2cm-0.5pt) {\rotatebox{-204}{\rule{.4pt}{1pt}}};
\node at (90-6*35:2cm-0.5pt) {\rotatebox{-210}{\rule{.4pt}{1pt}}};
\node at (90-6*36:2cm-0.5pt) {\rotatebox{-216}{\rule{.4pt}{1pt}}};
\node at (90-6*37:2cm-0.5pt) {\rotatebox{-222}{\rule{.4pt}{1pt}}};
\node at (90-6*38:2cm-0.5pt) {\rotatebox{-228}{\rule{.4pt}{1pt}}};
\node at (90-6*39:2cm-0.5pt) {\rotatebox{-234}{\rule{.4pt}{1pt}}};
\node at (90-6*40:2cm-0.5pt) {\rotatebox{-240}{\rule{.4pt}{1pt}}};
\node at (90-6*41:2cm-0.5pt) {\rotatebox{-246}{\rule{.4pt}{1pt}}};
\node at (90-6*42:2cm-0.5pt) {\rotatebox{-252}{\rule{.4pt}{1pt}}};
\node at (90-6*43:2cm-0.5pt) {\rotatebox{-258}{\rule{.4pt}{1pt}}};
\node at (90-6*44:2cm-0.5pt) {\rotatebox{-264}{\rule{.4pt}{1pt}}};
\node at (90-6*45:2cm-0.5pt) {\rotatebox{-270}{\rule{.4pt}{1pt}}};
\node at (90-6*46:2cm-0.5pt) {\rotatebox{-276}{\rule{.4pt}{1pt}}};
\node at (90-6*47:2cm-0.5pt) {\rotatebox{-282}{\rule{.4pt}{1pt}}};
\node at (90-6*48:2cm-0.5pt) {\rotatebox{-288}{\rule{.4pt}{1pt}}};
\node at (90-6*49:2cm-0.5pt) {\rotatebox{-294}{\rule{.4pt}{1pt}}};
\node at (90-6*50:2cm-0.5pt) {\rotatebox{-300}{\rule{.4pt}{1pt}}};
\node at (90-6*51:2cm-0.5pt) {\rotatebox{-306}{\rule{.4pt}{1pt}}};
\node at (90-6*52:2cm-0.5pt) {\rotatebox{-312}{\rule{.4pt}{1pt}}};
\node at (90-6*53:2cm-0.5pt) {\rotatebox{-318}{\rule{.4pt}{1pt}}};
\node at (90-6*54:2cm-0.5pt) {\rotatebox{-324}{\rule{.4pt}{1pt}}};
\node at (90-6*55:2cm-0.5pt) {\rotatebox{-330}{\rule{.4pt}{1pt}}};
\node at (90-6*56:2cm-0.5pt) {\rotatebox{-336}{\rule{.4pt}{1pt}}};
\node at (90-6*57:2cm-0.5pt) {\rotatebox{-342}{\rule{.4pt}{1pt}}};
\node at (90-6*58:2cm-0.5pt) {\rotatebox{-348}{\rule{.4pt}{1pt}}};
\node at (90-6*59:2cm-0.5pt) {\rotatebox{-354}{\rule{.4pt}{1pt}}};
\node at (90-6*60:2cm-0.5pt) {\rotatebox{-360}{\rule{.4pt}{1pt}}};

\end{tikzpicture}
\end{document}

5 回答5

0
<p>优化了一下代码,简单装饰美化了一下,装饰可以再进一步优化。</p><pre class="brush:plain;toolbar:false">\documentclass{standalone} \usepackage{ctex} \usepackage{tikz} \usepackage{time} \newcommand{\mytime}{\the\time} \begin{document} \begin{tikzpicture}[>=stealth] \draw[red!50!black,ultra thick] (0,0) circle (2.2cm); \draw[red!50!black,thin](0,0)circle(2.25); \draw[red!50!black,very thin](0,0)circle(2.3); \draw[->] (0,0) -- (90-\mytime*6:1.5);  %minute \draw[->>] (0,0) -- (90-\mytime/2:1);  %hour \fill[ball color =red] (0,0) circle (.7mm);  %\node[red,below = 8mm] at (0,0) {\small\now}; %我觉得放在这里影响表针。 \node[above = 8mm] at (0,0) {\tiny \TeX 白兔}; \node[red] at (0,-2.5) {\small \today \now}; %% 钟面 \foreach \x in {1,2,...,12}   {       \node at (90-30*\x:1.8){\x}; % 数字      \draw [very thick](30*\x:2.2)--++(30*\x:-0.18); % 时针刻度      \foreach \y in{1,2,3,4}       {       \draw (30*\x+\y*6:2.2)--++(30*\x+\y*6:-0.13);  % 分针刻度      }   } \end{tikzpicture} \end{document}</pre><p><img src="/data/ueditor/php/upload/image/20190902/1567421014585245.png" title="1567421014585245.png" alt="clock.png"></p>
1
<p></p><p>之前收藏的ctex论坛的kuing</p><pre class="brush:as3;toolbar:false">\documentclass{article} \usepackage{calc} \usepackage{tikz} \usepackage{xeCJK} \setCJKmainfont[BoldFont=SimHei,ItalicFont=KaiTi]{SimSun} \setCJKmonofont{FangSong} \newCJKfontfamily\lisu{LiSu} \usepackage[top=0.5in,bottom=0.5in,left=0.1in,right=0.1in]{geometry} \newcounter{clockminute} %常规版 \newlength\clockr \newlength\clockkd \newlength\clockszbj \newlength\clockszc \newlength\clockszk \newlength\clockfzc \newlength\clockfzk \newlength\clockzxr \clockr=2cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%半径r \clockkd=0.1cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%刻度长kd \clockszbj=0.32cm&nbsp;&nbsp;&nbsp;%数字边距szbj \clockszc=1.3cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%时针长szc \clockszk=0.07cm&nbsp;&nbsp;&nbsp;&nbsp;%时针宽szk \clockfzc=1.68cm&nbsp;&nbsp;&nbsp;&nbsp;%分针长fzc \clockfzk=0.04cm&nbsp;&nbsp;&nbsp;&nbsp;%分针宽fzk \clockzxr=0.1cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%中心半径zxr \newcommand\showclock{% \begin{tikzpicture} \draw[thick]&nbsp;(0,0)&nbsp;circle&nbsp;(\clockr); \foreach&nbsp;\i&nbsp;in&nbsp;{0,30,...,330}&nbsp;{ \draw[thick]&nbsp;(\i:\clockr)--(\i:\clockr-\clockkd);} \foreach&nbsp;\i&nbsp;in&nbsp;{6,12,...,360}&nbsp;{ \draw[very&nbsp;thin]&nbsp;(\i:\clockr)--(\i:\clockr-\clockkd);} \foreach&nbsp;\i&nbsp;in&nbsp;{1,2,...,12}&nbsp;{ \node&nbsp;at&nbsp;(90-\i*30:\clockr-\clockszbj)&nbsp;{\i};} \draw[line&nbsp;width=\clockszk]&nbsp;(0,0)--(90-0.5*\theclockminute:\clockszc); \pgfmathparse{90-mod(\theclockminute,60)*6} \let\minuteangle\pgfmathresult \draw[line&nbsp;width=\clockfzk]&nbsp;(0,0)--(\minuteangle:\clockfzc); \fill&nbsp;(0,0)&nbsp;circle&nbsp;(\clockzxr); \end{tikzpicture}} \newcommand\clock[2]{% \setcounter{clockminute}{#1*60+#2}\showclock} \newcommand\clocknow{% \setcounter{clockminute}{\time}\showclock} %时辰版 \newlength\scclockr \newlength\scclockhk \newlength\scclockkd \newlength\scclockszk \newlength\scclockzxr \scclockr=2cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%半径r \scclockhk=0.5cm&nbsp;&nbsp;&nbsp;&nbsp;%环宽hk \scclockkd=0.07cm&nbsp;&nbsp;&nbsp;%刻度长kd \scclockszk=0.07cm&nbsp;&nbsp;%时针宽szk \scclockzxr=0.13cm&nbsp;&nbsp;%中心半径zxr \newcommand\showscclock{% \begin{tikzpicture} \draw[thick]&nbsp;(0,0)&nbsp;circle&nbsp;(\scclockr); \draw&nbsp;(0,0)&nbsp;circle&nbsp;(\scclockr-\scclockhk); \foreach&nbsp;\i&nbsp;in&nbsp;{15,45,...,345}&nbsp;{ \draw&nbsp;(\i:\scclockr)--+(\i:-\scclockhk);} \foreach&nbsp;\i&nbsp;in&nbsp;{7.5,15,...,360}&nbsp;{ \draw&nbsp;(\i:\scclockr-\scclockhk)--+(\i:-\scclockkd);} %子丑寅卯辰巳午未申酉戌亥 \foreach&nbsp;\i/\j&nbsp;in {0/子,1/丑,2/寅,3/卯,4/辰,5/巳,6/午,7/未,8/申,9/酉,10/戌,11/亥}&nbsp;{ \node[rotate=-\i*30]&nbsp;at&nbsp;(90-\i*30:\scclockr-0.5*\scclockhk)&nbsp;{{\large\lisu\j}};} \draw[->,>=latex,line&nbsp;width=\scclockszk]&nbsp;(0,0)--(90-0.25*\theclockminute:\scclockr-\scclockhk); \fill&nbsp;(0,0)&nbsp;circle&nbsp;(\scclockzxr); \end{tikzpicture}} \newcommand\scclock[2]{% \setcounter{clockminute}{#1*60+#2}\showscclock} \newcommand\scclocknow{% \setcounter{clockminute}{\time}\showscclock} \parindent=2em \lineskiplimit=5pt \lineskip=5pt \linespread{1.3} \begin{document} \title{用tikz画简易时钟} \author{kuing} \date{\number\year&nbsp;年&nbsp;\number\month&nbsp;月&nbsp;\number\day&nbsp;日} \maketitle 这显然又是一个极其无聊之作,画法见源文件,我就不作解释了。 编译本文档的时刻是 \pgfmathparse{int(floor(\time/60))}\pgfmathresult :\pgfmathparse{int(mod(\time,60))}\pgfmathresult ,用&nbsp;\verb"\clocknow"&nbsp;输出编译的时刻: \clocknow 用&nbsp;\verb"\clock{小时}{分钟}"&nbsp;输出你想显示的时间, 例如&nbsp;\verb"\clock{10}{24}"&nbsp;得到: \clock{10}{24} 还有一个时辰版的,命令是&nbsp;\verb"\scclocknow"&nbsp;和&nbsp;\verb"\scclock{小时}{分钟}",效果: \scclocknow&nbsp;\&nbsp;\scclock{10}{24} 如果想改变某些长度宽度神马的,可以修改代码里的参数,随你自己弄了。 下面批量测试一下: \parindent=0em \foreach&nbsp;\i&nbsp;in&nbsp;{0,1,...,11}&nbsp;{ \foreach&nbsp;\j&nbsp;in&nbsp;{0,20,40}&nbsp;{ \clock{\i}{\j}\&nbsp;}} \foreach&nbsp;\i&nbsp;in&nbsp;{0,1,...,23}&nbsp;{ \foreach&nbsp;\j&nbsp;in&nbsp;{0,20,40}&nbsp;{ \scclock{\i}{\j}\&nbsp;}} \parindent=2em 就素酱紫,够无聊了吧…… \end{document}</pre><p><img src="/data/ueditor/php/upload/image/20190905/1567678596308656.png" title="1567678596308656.png" alt="QQ截图20190905181607.png"/></p><p>照猫画虎,某日画了一个</p><pre class="brush:as3;toolbar:false">\documentclass[tikz,border=2pt]{standalone} \usepackage{calc} \usepackage{ctex} \usepackage{amsmath} \newcounter{clockminute} %常规版 \newlength\clockr \newlength\clockkd \newlength\clockszbj \newlength\clockszc \newlength\clockszk \newlength\clockfzc \newlength\clockfzk \newlength\clockzxr \clockr=1.8cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%半径r \clockkd=0.1cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%刻度长kd \clockszbj=0.35cm&nbsp;&nbsp;&nbsp;%数字边距szbj \clockszc=0.6cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%时针长szc \clockszk=0.07cm&nbsp;&nbsp;&nbsp;&nbsp;%时针宽szk \clockfzc=0.9cm&nbsp;&nbsp;&nbsp;&nbsp;%分针长fzc \clockfzk=0.04cm&nbsp;&nbsp;&nbsp;&nbsp;%分针宽fzk \clockzxr=0.08cm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%中心半径zxr \newcommand\clock[2]{\setcounter{clockminute}{#1*60+#2} \begin{tikzpicture}%[scale=1.5] \draw[thick,yellow,line&nbsp;width=2pt]&nbsp;(0,0)&nbsp;circle&nbsp;(\clockr); %\foreach&nbsp;\i&nbsp;in&nbsp;{0,30,...,330}&nbsp;{ %\draw[thick]&nbsp;(\i:\clockr)--(\i:\clockr-\clockkd);} %\foreach&nbsp;\i&nbsp;in&nbsp;{6,12,...,360}&nbsp;{ %\draw[very&nbsp;thin]&nbsp;(\i:\clockr)--(\i:\clockr-\clockkd);} \foreach&nbsp;\i/\j&nbsp;in {0/\raisebox{3ex}{$\lfloor\pi\rfloor\!\cdot\!\lceil&nbsp;\pi\rceil$},1/\raisebox{2ex}{$\frac{\pi}{\pi}\hspace{-1em}$},2/$\lfloor\pi-\frac{\pi}{\pi}\rfloor\quad$,3/$\lfloor\pi\rfloor\hspace{-1em}$,4/$\lceil\pi\rceil\hspace{-1em}$,5/$\lceil&nbsp;\pi+\frac{\pi}{\pi}\rceil$,6/\raisebox{-4ex}{$\lfloor\pi+\pi\rfloor$},7/$\lceil\pi+\pi\rceil$,8/\qquad$\lfloor\pi\cdot\pi-\frac{\pi}{\pi}\rfloor$,9/$\lfloor\pi\cdot\pi\rfloor$,10/$\lceil\pi\cdot\pi\rceil$,11/\raisebox{-3ex}{\quad$\lceil\pi\cdot\pi+\frac{\pi}{\pi}\rceil$}}&nbsp;{ \node[scale=0.5]&nbsp;at&nbsp;(90-\i*30:\clockr-\clockszbj)&nbsp;{\j};} \draw[line&nbsp;width=\clockszk,yellow,line&nbsp;cap=round]&nbsp;(0,0)--(90-0.5*\theclockminute:\clockszc); \pgfmathparse{90-mod(\theclockminute,60)*6} \let\minuteangle\pgfmathresult \draw[line&nbsp;width=\clockfzk,yellow,line&nbsp;cap=round]&nbsp;(0,0)--(\minuteangle:\clockfzc); \filldraw[fill=yellow,yellow]&nbsp;(0,0)&nbsp;circle&nbsp;(\clockzxr); \end{tikzpicture}} \begin{document} \clock{10}{10} \end{document}</pre><p><img src="/data/ueditor/php/upload/image/20190906/1567763755473783.png" title="1567763755473783.png" alt="QQ截图20190906175547.png"/> &nbsp; &nbsp;</p>
0
<p>其实你可以发专栏,不必写成问题……</p>
  • 得给群主提一下 – 逆天小白兔 2019-09-01 20:22 回复
  • 普通用户可能不能写专栏,我找了半天没找到 – 逆天小白兔 2019-09-01 20:21 回复
0
<p>重复的命令直接写一个循环就行了</p>
0
<p></p><p>用python写一个(术业有专攻,我可能是来搞笑的,答非所问</p><p></p>

你的回答

请登录后回答

你的回答将会帮助更多人,请务必认真回答问题。