44

I'm trying to generate a clock similar to as in the question asked here

But I want the face to be numbered. Is this possible with this package or any other?

  • 4
    As an aside -- if you're planning on producing a Roman Numeral clock, be careful to look at actual RN clocks, as they sometimes have interesting features, e.g., most show 4 as "IIII" instead of "IV", and most also have the "feet" of each number pointing towards the center of the clock (making the six upside down). – Chris Gregg Sep 9 '13 at 6:09
35

A little variation of an example taken from the PGF documentation (Section 83 Repeating Things: The Foreach Statement, page 912 for version 3.0):

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[line cap=rect,line width=3pt]
\filldraw [fill=cyan] (0,0) circle [radius=2cm];
\foreach \angle [count=\xi] in {60,30,...,-270}
{
  \draw[line width=1pt] (\angle:1.8cm) -- (\angle:2cm);
  \node[font=\large] at (\angle:1.36cm) {\textsf{\xi}};
}
\foreach \angle in {0,90,180,270}
  \draw[line width=2pt] (\angle:1.6cm) -- (\angle:2cm);
\draw (0,0) -- (120:0.8cm);
\draw (0,0) -- (90:1cm);
\end{tikzpicture}

\end{document}

enter image description here

An perhaps one could define a command to control some attributes; below an example, allowing to specify the color, the radius and the font size for the numbers:

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}

\NewDocumentCommand{\Clock}{O{2cm}O{\large}O{cyan}}{%
\def\radius{#1}%
\begin{tikzpicture}[line cap=rect,line width=0.055*\radius]
\filldraw [fill=#3] (0,0) circle [radius=\radius];
\foreach \angle [count=\xi] in {60,30,...,-270}
{
  \draw[line width=1pt] (\angle:0.9*\radius) -- (\angle:\radius);
  \node[font=#2] at (\angle:0.68*\radius) {\textsf{\xi}};
}
\foreach \angle in {0,90,180,270}
  \draw[line width=0.04*\radius] (\angle:0.82*\radius) -- (\angle:\radius);
\draw (0,0) -- (120:0.4*\radius);
\draw (0,0) -- (90:0.5*\radius);
\end{tikzpicture}%
}

\begin{document}

\Clock\quad\Clock[1cm][\footnotesize][orange]

\end{document}

enter image description here

  • Do you know if this example has been removed from the documentation? I can't see anything obviously relevant on page 507... – cfr Jul 27 '14 at 0:12
  • @cfr It was page 507 on the manual for version 2.10. For version 3.0 it was moved to page 912. I've edited my answer updating the page and adding the name of the section in the hope of a more "stable" reference. – Gonzalo Medina Jul 27 '14 at 0:28
  • Thanks! I assumed that was the case and hoped you might do something like that. – cfr Jul 27 '14 at 1:12
36

We can convert Gonzalo's excellent answer in to a macro so that we can use it like:

\clock{<hour>}{<minute>}{<seconds>}

Further beautification is possible and left as a home work ;-).

\documentclass[dvipsnames]{article}
\usepackage{tikz}

\begin{document}
\newcommand\clock[3]{%
\begin{tikzpicture}[line cap=round,line width=3pt]
\filldraw [fill=Goldenrod!30] (0,0) circle (2cm);
\foreach \angle / \label in
{0/3, 30/2, 60/1, 90/12, 120/11, 150/10, 180/9,
210/8, 240/7, 270/6, 300/5, 330/4}
{
\draw[line width=1pt] (\angle:1.8cm) -- (\angle:2cm);
\draw (\angle:1.4cm) node{\textsf{\label}};
}
\foreach \angle in {0,90,180,270}
\draw[line width=2pt] (\angle:1.6cm) -- (\angle:2cm);
\node[draw=none,font=\tiny,text=red] at (0,.9cm) {TICK-TOCK};
\draw[rotate=90,line width=2pt] (0,0) -- (-#1*30-#2*30/60:0.7cm); % hours
\draw[rotate=90,line width=1.5pt] (0,0) -- (-#2*6:1cm); % minutes
\draw[rotate=90,thin,red] (0,0) -- (-#3*6:1.2cm); % seconds
\path [fill=red] (0,0) circle (2pt);
%
\end{tikzpicture}%
}
%%\syntax
%% \clock{<hour>}{<minute>}{<seconds>}
\noindent\clock{1}{15}{30} \clock{2}{25}{15}
\clock{12}{58}{10} \clock{6}{10}{45}


\end{document}

enter image description here

Scalable version:

\documentclass[dvipsnames]{article}
\usepackage{tikz}

\begin{document}
\newcommand\clock[4][2]{%
\begin{tikzpicture}[scale=#1,line cap=round,line width=#1*3pt]
\filldraw [fill=Goldenrod!20] (0,0) circle (2cm);
\foreach \angle / \label in
{0/3, 30/2, 60/1, 90/12, 120/11, 150/10, 180/9,
210/8, 240/7, 270/6, 300/5, 330/4}
{
\draw[line width=#1*1pt] (\angle:1.8cm) -- (\angle:2cm);
\draw (\angle:1.4cm) node[scale=#1]{\textsf{\label}};
}
\foreach \angle in {0,90,180,270}
\draw[line width=#1*2pt] (\angle:1.6cm) -- (\angle:2cm);
\node[draw=none,font=\tiny,text=red,scale=#1] at (0,.9cm) {TICK-TOCK};
\draw[rotate=90,line width=#1*2pt] (0,0) -- (-#2*30-#3*30/60:0.7cm); % hours
\draw[rotate=90,line width=#1*1.5pt] (0,0) -- (-#3*6:1cm); % minutes
\draw[rotate=90,line width=#1*.6pt,red] (0,0) -- (-#4*6:1.2cm); % seconds
\path [fill=black] (0,0) circle (3pt);
\path [fill=red] (0,0) circle (1.5pt);
%
\end{tikzpicture}%
}
%%\syntax
%% \clock[<optional scaling dim>]{<hour>}{<minute>}{<seconds>}
\noindent\clock{9}{35}{55}\clock[1.2]{1}{15}{30} \clock[.9]{2}{25}{15}
\clock[.7]{12}{58}{10} \clock[.5]{6}{10}{55}
\clock[.3]{12}{20}{22} \clock[.2]{8}{0}{5}


\end{document}

enter image description here

And here is a minimal animation. I don't dare to go for the inclusion of seconds hand / make the intervals small.

\documentclass[preview,border={10pt 0pt 10pt 10pt}]{standalone}

\usepackage{filecontents}
\begin{filecontents*}{clock.tex}
\documentclass[tikz,border=0pt,dvipsnames]{standalone}
\usepackage{tikz}

\begin{document}
\foreach \hdeg / \mdeg in {0/0,2/24,4/48,6/72,8/96,10/120,12/144,14/168,
16/192,18/216,20/240,22/264,24/288,26/312,28/336,30/360}{%
\begin{tikzpicture}[line cap=round,line width=3pt]
\filldraw [fill=Goldenrod!30] (0,0) circle (2cm);
\foreach \angle / \label in
{0/3, 30/2, 60/1, 90/12, 120/11, 150/10, 180/9,
210/8, 240/7, 270/6, 300/5, 330/4}
{
\draw[line width=1pt] (\angle:1.8cm) -- (\angle:2cm);
\draw (\angle:1.4cm) node{\textsf{\label}};
}
\foreach \angle in {0,90,180,270}
\draw[line width=2pt] (\angle:1.6cm) -- (\angle:2cm);
\node[draw=none,font=\tiny,text=red] at (0,.9cm) {TICK-TOCK};
\draw[rotate=90,line width=2pt] (0,0) -- (-\hdeg:0.7cm); % hours
\draw[rotate=90,line width=1.5pt] (0,0) -- (-\mdeg:1cm); % minutes
\path [fill=black] (0,0) circle (3pt);
\path [fill=red] (0,0) circle (2pt);
%
\end{tikzpicture}%
%}
}

\end{document}
\end{filecontents*}
%
\immediate\write18{pdflatex clock}

% convert to GIF animation
\immediate\write18{convert -delay 200 -loop 0 -density 400 -alpha remove clock.pdf clock.gif}

\usepackage{animate}
\begin{document}
See the gif file in the same folder as this file.
\end{document}

enter image description here

  • Maybe I'm wrong, but shouldn't it be \newcommand\clock[4][1]{% (or the starred varsion \newcommand*\clock[4][1]{%) instead of \newcommand\clock[4][2]{%? – Svend Tveskæg Sep 9 '13 at 9:06
  • @SvendTveskæg It was \newcommand\clock[4][1]{% originally. Now edited by MMA :) – user11232 Sep 9 '13 at 9:49
  • 3
    I just broke my watch, I think I will buy one of these... :-) – karlkoeller Sep 9 '13 at 13:21
  • 1
    @PSTikZ Sorry. It is a wall clock ;-). Geometrically, I was careless and file size, trust me, it could have been even larger ;-) let alone the time of compilation. I do have a almost full version, but, I hate it as it ran too long to compile. – user11232 Sep 10 '13 at 0:14
  • 1
    @PSTikZ: True. PSTricks is powerful than TikZ in some aspects. But you know, I am on the other side ;-) – user11232 Sep 10 '13 at 0:17
24

Bit off topic-answer, once I made a "mathematican clock" as a birthday present. The result was printed out, and I replaced the number plate of a real clock with it:

\documentclass[landscape,a3paper,extrafontsizes,12pt]{memoir}
\usepackage[a3paper,margin=0.2cm]{geometry} 
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{center}
\vspace*{\fill}

\begin{tikzpicture}
    \foreach \x in {0,30,...,360} {
        \draw [line width = 0.1cm] (\x:9cm) -- (\x:10.5cm);
    };
    \foreach \x in {0,6,...,360} {
        \draw [line width = 0.05cm] (\x:10cm) -- (\x:10.5cm);
    };
    \draw (0:0cm) circle [radius = 0.2cm];

    \node (1) at (60:7cm) {\Large $\boldsymbol{9^{9-9}}$};
    \node (2) at (30:7cm) {\Large $\boldsymbol{\dfrac{9+9}{9}}$};
    \node (3) at (0:7cm) {\Huge $\boldsymbol{\sqrt{\log_{9}{9^9}}}$};
    \node (4) at (-30:7cm) {\Large $\boldsymbol{\sqrt{9}+\log_{9}9}$};
    \node (5) at (-60:7cm) {\Large $\boldsymbol{\sqrt{9}!-\log_{9}9}$};
    \node (6) at (-90:7cm) {\Huge $\boldsymbol{9 - \dfrac{9}{\sqrt{9}}}$};
    \node (7) at (-120:7cm) {\Large $\boldsymbol{\sqrt{9}!+\dfrac{9}{9}}$};
    \node (8) at (-150:7cm) {\Large $\boldsymbol{9 - \dfrac{9}{9}}$};
    \node (9) at (-180:7cm) {\Huge $\boldsymbol{\sqrt[9]{9^9}}$};
    \node (10) at (-210:7cm) {\Large $\boldsymbol{9 + \log_{9}{9}}$};
    \node (11) at (-240:7cm) {\Large $\boldsymbol{\dfrac{99}{9}}$};
    \node (12) at (-270:7cm) {\Huge $\boldsymbol{9 + \dfrac{9}{\sqrt{9}}}$};
\end{tikzpicture}
\vspace*{\fill}
\end{center}
\end{document}

(this is an excerpt, the original had also numbers 0-59 on each of the minutes line)

result:

LaTeX results

There are no hands, as those were provided by the real clock

  • 1
    You should typeset the birthday present correctly; log --> \log. – Svend Tveskæg Sep 9 '13 at 9:51
  • didn't knew there was a \log command. Thanks – SztupY Sep 9 '13 at 10:25
9

With PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{multido}
\SpecialCoor
\begin{document}
\begin{pspicture}(-3,-3)(3,3)
    \pscircle{3}
    \multido{\ia=60+-30,\in=1+1}{12}{\psline(2.9;\ia)(3;\ia)\uput{2.4}[\ia](0,0){\in}}
    \multido{\ia=90+90}{4}{\psline(2.8;\ia)(2.9;\ia)}
    \psline[linewidth=3\pslinewidth]{<->}(2.1;90)(0,0)(1.5;0)
\end{pspicture}
\end{document}

enter image description here

A clock with exhausted batteries

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{multido}
\SpecialCoor
\begin{document}
\multido{\is=90+-6,\ic=0+1}{60}{%
\begin{pspicture}(-3,-3)(3,3)
    \pscircle{3}
    \multido{\ia=60+-30,\in=1+1}{12}{\psline(2.9;\ia)(3;\ia)\uput{2.4}[\ia](0,0){\in}}
    \multido{\ia=90+90}{4}{\psline(2.8;\ia)(2.9;\ia)}
    \pstVerb{/angle {\ic\space 2 mod 0 gt {90}{105} ifelse} bind def}%
    \psline[linewidth=3\pslinewidth]{<->}(!2.1 angle PtoC)(0,0)(!1.5 angle 90 sub PtoC)
    \psset{linecolor=red}
    \psline{->}(0,0)(2.2;\is)
    \pscircle*{2pt}
\end{pspicture}}
\end{document}

enter image description here

  • Just curious: Where is second's hand and it's Animation (your unique answer feature)? – texenthusiast Sep 9 '13 at 7:20
  • Looks like \psline[linewidth=3\pslinewidth]{<->}(2.1;90)(0,0)(1.5;0) represends both hands. – Per Alexandersson Sep 9 '13 at 7:47
  • Your second clock is broken. The hour hand points at 2:30, but the minute hand points at x:57. – Jake Sep 9 '13 at 8:15
  • @PSTikZ: Ah, I thought you meant that the second hand is decoupled from the minutes/hours. – Jake Sep 9 '13 at 8:23
4

With Metapost. This is more complicated than it has to be, because it takes care to mark off each minute, and to use different widths and lengths for five-minute ticks as opposed to one-minute ticks, but it's still pretty simple:

beginfig(1);

diameter=160;
radius=diameter/2;

pickup pencircle scaled 0.5;

z0 = (radius,radius);
z50 = (radius,diameter);

pair r[];
r[12]  = z50 shifted (0,-radius/4.5);
for i=12 downto 1:
    r[i] = r[i+1] rotatedaround (z0,30);
    z[i] = r[i];
endfor
defaultfont := "cmr17";
defaultscale := 1.5;
label("12",z12);
label("6",z6);
label("9",z9);
label("3",z3);
label("1",z1);
label("5",z5);
label("7",z7);
label("11",z11 shifted (0,-1pt));
label("2",z2);
label("4",z4);
label("8",z8);
label("10",z10 shifted (1pt,0));

pair p[];
pair q[];
numeric circangle;
circangle := 0;
p[0] = z50;
for i=0 step 1 until 60:
    p[i] = p[i-1] rotatedaround (z0,6);
    if (i mod 5 = 0):
        q[i] = 1/12[p[i],z0];
        pickup pencircle scaled 2;
        draw p[i]--q[i];
        pickup pencircle scaled 0.5;
    else:
        q[i] = 1/30[p[i],z0];
        pickup pencircle scaled 0.5;
        draw p[i]--q[i];
    fi
    draw p[i-1]..p[i];
endfor

endfig;
end;

I've always been partial to Metapost, as I find it much easier to program than tikZ. tikZ does have the benefit of being able to be thrown into macros, though, as Harish Kumar's excellent answer above demonstrates.

I know that, strictly speaking, this isn't a TeX answer; but Metapost has such a pedigree among TeXnicians (even Knuth himself uses it for his figures, I'm told) that it seemed a fair answer to me.

3

I'll leave my contribution here, using pics to recreate Gonzalo Medina's clock with enough options to make it fancy but also simple to use:

\pic[opt] (name) at (x,y) {clock=HH:MM}

Some examples:

enter image description here

Code

\documentclass[tikz, margin=2mm]{standalone}
\makeatletter
\newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #1@}
\newcommand*{\oldrom}[1]{\ifnum #1=4 IIII\else\expandafter\@slowromancap\romannumeral #1@\fi}
\makeatother
\tikzset{
    hour pointer/.style={line cap=round, thick},
    minute pointer/.style={thin, line cap=round},
    ticks sep/.style={outer sep=-#1}, ticks sep={0pt},
    ticks size/.store in=\clocktickssize,
    ticks size=.25cm,
    ticks/.style={thin},
    main ticks/.style={very thick},
    clock/.style={draw, minimum size=4cm, circle},
    clock text/.code={\let\clocktext\textrm},
    hour pointer size/.store in=\hourpointersize,
    hour pointer size=0.5cm,
    minute pointer size/.store in=\minutepointersize,
    minute pointer size=0.75cm,
    clock text sep/.store in=\clocktextsep,
    clock text sep=1em,
    pics/clock/.style args={#1:#2}{
        code={%
        \pgfmathsetmacro{\hourangle}{90-#1*30-#2*0.5}\pgfmathsetmacro{\minuteangle}{90-#2*6}
        \node[clock] (-clock) {};
        \foreach \angle [count=\xi] in {60,30,...,-270}
        {
            \draw[clock text, ticks] (-clock.\angle) -- ++(\angle:-\clocktickssize) node[anchor=center, shift={(\angle:-\clocktextsep)}] {\clocktext{\xi}};
        };
        \foreach \angle in {0,90,180,270} \draw[ticks, main ticks] (-clock.\angle) -- ++(\angle:-\clocktickssize);
        \draw[minute pointer] (-clock.center) -- ++(\minuteangle:\minutepointersize);
        \draw[hour pointer] (-clock.center) -- ++(\hourangle:\hourpointersize);
        }
    }
}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\pic (Default) at (0,5) {clock=00:00};
\pic[hour pointer/.style={-{Kite[width=2pt]},
                         insert path={node[circle,
                                           minimum size=2pt,
                                           inner sep=0pt,
                                           fill=yellow!10,
                                           draw, thick]{}
                                     } },
     minute pointer/.style={{Circle[length=2pt]}-{Kite[width=2pt]}, shorten <=-.25cm},
     clock/.style={double=gray, double distance=2pt, draw, fill=yellow!10, circle, minimum size=3cm},
     clock text sep=0.6em,
     ticks sep=1pt,
     ticks/.style={shorten >=2pt}, main ticks/.style={shorten >=0pt, very thick},
     clock text/.code={\let\clocktext\textit}
     ] (Fancy) {clock=22:40};
\pic[right=1cm,
     clock text/.code={\let\clocktext\rom},
     clock/.style={minimum size=5cm, draw}] (Roman) at (Fancy-clock.east) {clock=16:33};
\pic[left=1cm,
     clock text/.code={\let\clocktext\oldrom},
     clock/.style={minimum size=5cm, draw}] (Old roman) at (Fancy-clock.west) {clock=12:30};
\foreach \clock in {Default, Fancy, Roman, Old roman} \node[above=.25cm] at (\clock -clock.north) {\clock\ Clock};
\end{tikzpicture}
\end{document}
2

Here is another version with MetaPost. This one give the current hour and minute at the time of the compilation, thanks to the internal variables hour and minute of MetaPost. Alas there is no internal variable in MetaPost which gives the current second, otherwise I would have also drawn a needle indicating it (but anyway, in this case the program would have been shown in this subject).

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
  \mplibsetformat{metafun}
  \mplibtextextlabel{enable}
\begin{document}
  \begin{mplibcode}
    input mpcolornames
    r := 4cm; len := 10bp; min_thickness := 1.75bp; hour_thickness := 3bp;
    beginfig(1);
      path cadran; cadran = fullcircle scaled (2r); 
      fill cadran scaled 1.025; fill cadran withcolor .75[rgb_orange, white]; 
      % Graduation marks and labels
      for i = 1 upto 60:
        if i mod 5 = 0: 
          j := i div 5; angl := 90-30j; 
          freelabel("\large\sffamily\bfseries" & decimal j, (r-len)*dir angl, r*dir angl);
          draw ((r, 0) -- (r - len, 0)) rotated angl withpen pencircle scaled min_thickness;
        else:
          angl := 90 - 6i;
          draw ((r, 0) -- (r - .5len, 0)) rotated angl;
          freelabel("\tiny\sffamily" & decimal i, (r-.5len)*dir angl, r*dir angl);
        fi
      endfor
      % Needles
      label("\Large\scshape tic-tac", .65r*up) withcolor red;
      pickup pencircle scaled min_thickness;
      drawarrow origin -- r*dir(90-minute*6) cutends (0, 3.5len);
      pickup pencircle scaled hour_thickness;
      drawarrow origin -- r*dir (90-(hour+minute/60)*30) cutends(0, 6len);
      fill fullcircle scaled .75len;
    endfig;
  \end{mplibcode}
\end{document}

To be typeset with LuaLaTeX. Current hour in Belgium:

enter image description here

  • 1
    To get current second, we can use lua code: second := \directlua{ tex.sprint( os.date("*t").sec ) }; – Dohyun Kim Apr 8 '15 at 9:56
  • @Dohyun Kim Great, thank you! Another incentive to learn Lua as soon as possible :-) I've included your code in the other subject: tex.stackexchange.com/questions/236923/… – Franck Pastor Apr 8 '15 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.