使用 LaTeX 制作一个实时时钟

2019-09-01 19:36发布

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

今天看了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条回答
lingdanda
2019-09-05 18:16

之前收藏的ctex论坛的kuing

\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         %半径r
\clockkd=0.1cm      %刻度长kd
\clockszbj=0.32cm   %数字边距szbj
\clockszc=1.3cm     %时针长szc
\clockszk=0.07cm    %时针宽szk
\clockfzc=1.68cm    %分针长fzc
\clockfzk=0.04cm    %分针宽fzk
\clockzxr=0.1cm     %中心半径zxr

\newcommand\showclock{%
\begin{tikzpicture}
\draw[thick] (0,0) circle (\clockr);
\foreach \i in {0,30,...,330} {
\draw[thick] (\i:\clockr)--(\i:\clockr-\clockkd);}
\foreach \i in {6,12,...,360} {
\draw[very thin] (\i:\clockr)--(\i:\clockr-\clockkd);}
\foreach \i in {1,2,...,12} {
\node at (90-\i*30:\clockr-\clockszbj) {\i};}
\draw[line width=\clockszk] (0,0)--(90-0.5*\theclockminute:\clockszc);
\pgfmathparse{90-mod(\theclockminute,60)*6}
\let\minuteangle\pgfmathresult
\draw[line width=\clockfzk] (0,0)--(\minuteangle:\clockfzc);
\fill (0,0) circle (\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       %半径r
\scclockhk=0.5cm    %环宽hk
\scclockkd=0.07cm   %刻度长kd
\scclockszk=0.07cm  %时针宽szk
\scclockzxr=0.13cm  %中心半径zxr

\newcommand\showscclock{%
\begin{tikzpicture}
\draw[thick] (0,0) circle (\scclockr);
\draw (0,0) circle (\scclockr-\scclockhk);
\foreach \i in {15,45,...,345} {
\draw (\i:\scclockr)--+(\i:-\scclockhk);}
\foreach \i in {7.5,15,...,360} {
\draw (\i:\scclockr-\scclockhk)--+(\i:-\scclockkd);}
%子丑寅卯辰巳午未申酉戌亥
\foreach \i/\j in
{0/子,1/丑,2/寅,3/卯,4/辰,5/巳,6/午,7/未,8/申,9/酉,10/戌,11/亥} {
\node[rotate=-\i*30] at (90-\i*30:\scclockr-0.5*\scclockhk) {{\large\lisu\j}};}
\draw[->,>=latex,line width=\scclockszk] (0,0)--(90-0.25*\theclockminute:\scclockr-\scclockhk);
\fill (0,0) circle (\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 年 \number\month 月 \number\day 日}
\maketitle


这显然又是一个极其无聊之作,画法见源文件,我就不作解释了。

编译本文档的时刻是
\pgfmathparse{int(floor(\time/60))}\pgfmathresult
:\pgfmathparse{int(mod(\time,60))}\pgfmathresult
,用 \verb"\clocknow" 输出编译的时刻:

\clocknow

用 \verb"\clock{小时}{分钟}" 输出你想显示的时间,
例如 \verb"\clock{10}{24}" 得到:

\clock{10}{24}

还有一个时辰版的,命令是 \verb"\scclocknow" 和 \verb"\scclock{小时}{分钟}",效果:

\scclocknow \ \scclock{10}{24}

如果想改变某些长度宽度神马的,可以修改代码里的参数,随你自己弄了。

下面批量测试一下:

\parindent=0em

\foreach \i in {0,1,...,11} {
\foreach \j in {0,20,40} {
\clock{\i}{\j}\ }}

\foreach \i in {0,1,...,23} {
\foreach \j in {0,20,40} {
\scclock{\i}{\j}\ }}

\parindent=2em

就素酱紫,够无聊了吧……

\end{document}

QQ截图20190905181607.png

照猫画虎,某日画了一个

\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         %半径r
\clockkd=0.1cm      %刻度长kd
\clockszbj=0.35cm   %数字边距szbj
\clockszc=0.6cm     %时针长szc
\clockszk=0.07cm    %时针宽szk
\clockfzc=0.9cm    %分针长fzc
\clockfzk=0.04cm    %分针宽fzk
\clockzxr=0.08cm     %中心半径zxr

\newcommand\clock[2]{\setcounter{clockminute}{#1*60+#2}
	\begin{tikzpicture}%[scale=1.5]
	\draw[thick,yellow,line width=2pt] (0,0) circle (\clockr);
	%\foreach \i in {0,30,...,330} {
		%\draw[thick] (\i:\clockr)--(\i:\clockr-\clockkd);}
	%\foreach \i in {6,12,...,360} {
		%\draw[very thin] (\i:\clockr)--(\i:\clockr-\clockkd);}
	\foreach \i/\j in
	{0/\raisebox{3ex}{$\lfloor\pi\rfloor\!\cdot\!\lceil \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 \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$}} {
		\node[scale=0.5] at (90-\i*30:\clockr-\clockszbj) {\j};}
	\draw[line width=\clockszk,yellow,line cap=round] (0,0)--(90-0.5*\theclockminute:\clockszc);
	\pgfmathparse{90-mod(\theclockminute,60)*6}
	\let\minuteangle\pgfmathresult
	\draw[line width=\clockfzk,yellow,line cap=round] (0,0)--(\minuteangle:\clockfzc);
	\filldraw[fill=yellow,yellow] (0,0) circle (\clockzxr);
	\end{tikzpicture}}
\begin{document}
\clock{10}{10}
\end{document}

QQ截图20190906175547.png    

一周热门 更多>