我正在制作一个时间轴(跨越小时,而不是天/年),并用数字时间标记(例如 1:15、5:45、3:00)。一切都以分钟为单位计算。我遇到的问题是整点时间,其中分钟显示为零,而不是双零:
有没有简单的方法可以做到这一点,或者最好对 if-then 进行硬编码?
这是 MWE(基于此站点某处的数字线代码,不记得在哪里了):
\documentclass[10pt,oneside]{article}
\usepackage[margin=0.5in,a4paper,landscape]{geometry}
\usepackage{tikz}
\newcommand{\drawtimeline}[5]
{
\pgfmathsetmacro\starttime{#1*60+#2}
\pgfmathsetmacro\endtime{#3*60+#4}
\pgfmathsetmacro\timediff{\endtime-\starttime}
\pgfmathsetmacro\numticks{\timediff}
\pgfmathsetmacro\numlabels{\timediff/15}
\pgfmathsetmacro\intervallabel{#5/\numlabels}
\pgfmathsetmacro\intervaltick{#5/\numticks}
\pgfmathsetmacro\extraticks{\numticks+2}
\begin{tikzpicture}
%draw line
\draw [thick](-2*\intervaltick pt,0) -- (#5+2*\intervaltick pt,0);
%draw small ticks
\foreach \y in {-2,-1,..., \extraticks } {
\draw[thin, yshift=0 cm, xshift= \intervaltick * \y ] (0pt,+4pt) -- (0pt,-3pt);}
%draw big ticks and labels
\foreach \y
[evaluate=\y as \hrs using ({div(\y * 15 + \starttime,60)}]
[evaluate=\y as \mins using ({mod(\y * 15 +\starttime,60)}]
in {0,1,...,\numlabels}
{
\draw[yshift=-0.5 cm, xshift = \intervallabel * \y ] node[ below]{$\pgfmathprintnumber{\hrs}$:$\pgfmathprintnumber{\mins}$};
\draw[thick, yshift=0 cm, xshift= \intervallabel * \y ] (0pt,+8pt) -- (0pt,-8pt);}
\end{tikzpicture}
}
\begin{document}
\drawtimeline{5}{45}{6}{30}{25cm}
\end{document}
答案1
在我确定\mins
输出字符串像15.0
和之后0.0
,我将\pgfmathprintnumber{\mins}
输出替换为\twodigit{\mins}
,其中\twodigit
定义为
\newcommand\twodigit[1]{\expandafter\twodigitaux#1\relax}
\def\twodigitaux#1.#2\relax{\ifnum10>#1\relax0\fi\pgfmathprintnumber{#1.#2}}
这样,如果预点数\mins
小于10
,则在输出中添加一个额外的点0
。例如,\twodigit{7.23}
产生07.23
。
\documentclass[10pt,oneside]{article}
\usepackage[margin=0.5in,a4paper,landscape]{geometry}
\usepackage{tikz}
\newcommand{\drawtimeline}[5]
{
\pgfmathsetmacro\starttime{#1*60+#2}
\pgfmathsetmacro\endtime{#3*60+#4}
\pgfmathsetmacro\timediff{\endtime-\starttime}
\pgfmathsetmacro\numticks{\timediff}
\pgfmathsetmacro\numlabels{\timediff/15}
\pgfmathsetmacro\intervallabel{#5/\numlabels}
\pgfmathsetmacro\intervaltick{#5/\numticks}
\pgfmathsetmacro\extraticks{\numticks+2}
\begin{tikzpicture}
%draw line
\draw [thick](-2*\intervaltick pt,0) -- (#5+2*\intervaltick pt,0);
%draw small ticks
\foreach \y in {-2,-1,..., \extraticks } {
\draw[thin, yshift=0 cm, xshift= \intervaltick * \y ] (0pt,+4pt) -- (0pt,-3pt);}
%draw big ticks and labels
\foreach \y
[evaluate=\y as \hrs using ({div(\y * 15 + \starttime,60)}]
[evaluate=\y as \mins using ({mod(\y * 15 +\starttime,60)}]
in {0,1,...,\numlabels}
{
\draw[yshift=-0.5 cm, xshift = \intervallabel * \y ] node[ below]{%
$\pgfmathprintnumber{\hrs}$:%
$\twodigit{\mins}$};
\draw[thick, yshift=0 cm, xshift= \intervallabel * \y ] (0pt,+8pt) -- (0pt,-8pt);}
\end{tikzpicture}
}
\newcommand\twodigit[1]{\expandafter\twodigitaux#1\relax}
\def\twodigitaux#1.#2\relax{\ifnum10>#1\relax0\fi\pgfmathprintnumber{#1.#2}}
\begin{document}
\drawtimeline{5}{45}{6}{30}{25cm}
\end{document}