How to write a text along path using TikZ: Speedometer case

At the end of this tutorial, you will be able to write text along any path (e.g. line, circle and arc). To make the article appealing, we will consider the case of drawing a speedometer with labels as shown in the next illustration. 

text along path tikz

1. From where I should start...

If you ask any TikZ user about how to draw the above illustration, you may get different responses. Some of them are optimized (small codes) and some of them are complex to follow. This mainly returns to the user experience and his geometry skills and imagination. 

The speedometer illustration is made of the following ingredients:

  •  Arcs, circles and straight lines. 
  • The filling colour is red with different saturation values.
  • Labels are with capital letters and follow the arc shape. 

We can remark that circles and arcs (part of a big circle) have a center at the speedometer needle rotation center. We will choose this as the origin (0,0) and from there we draw different parts of the illustration. Let’s start by setting up the latex document!

2. What packages and libraries we need?

First of all, we need to upload the  Tikz package together with a specialized library named decorations.texts for writing text along a path.

The Tikz package is uploaded using the command \verb|\usepackage{…}| and the library is uploaded using the command \verb|\usetikzlibrary{…}|check the initial code of the illustration. 



% Here goes the code

3. How to draw an arc using TikZ?

Drawing an arc in \LaTeX can be done as follows:
\verb|\draw | (x,y) \verb| arc | (\alpha:\beta:r);


  • (x,y) is the starting point of the arc in cartesian coordinate. Polar coordinates can be also used (r:\theta);
  • \alpha and \beta are starting and arriving angles in degrees.
  • r is the radius of the arc;


Let’s consider the critical region as an example to highlight how one can draw an arc using TikZ. The next illustration shows different polar coordinates that we will use for the critical region path. We distinguish two arcs both starting from the angle 0^{\circ} and ending at the angle 45^{\circ}. The 45^{\circ} angle is obtained by dividing 180^{\circ} by the number of sectors 4. These two arcs have different radius (2cm and 3.5cm). The following code corresponds to the critical region drawn with a thick white line and filled with red color. 

\draw[draw=white,fill=red,thick] (0:2)--(0:3.5)arc(0:45:3.5)--(45:2)arc(45:0:2);

It starts from the polar coordinate (0:2) (radius 2 and angle 0^\circ) and it goes to the point (0:3.5). Then, from this point we draw an arc where the starting angle is 0^\circ and the arriving angle is 45^\circ. After that, we keep the same angle and we change the radius (from 3.5 to 2.) to draw a straight line. Finally, we close the path by an arc starting from 45^\circ and it goes back to 0^\circ with radius 2. Generally, we add –cycle at the end of the path to close it perfectly.

Read this interesting post for more details about how to draw arcs using TikZ.

4. Add text along a path (arc)

We have mentioned previously that writing text along path requires the library decorations.text. The following syntax highlights how we achieve that:
\draw[decoration={text along path, text={CRITICAL},text align={center},
raise=0.2cm},decorate] (45:3.5) arc (45:0:3.5);
The path corresponds to the outer arc (with radius 3.5cm). We have aligned the text \verb|CRITICAL| using the option \verb|text align={center}| and we raised it a little by 0.2cm to avoid putting it on the path. This is achieved with the option \verb|raise=0.2cm|. The obtained illustration is shown below with the caption “with reverse path option”. It should be noted that starting the path as follows:
\draw[decoration={text along path, text={CRITICAL},text align={center},
raise=0.2cm},decorate] (0:3.5) arc (0:45:3.5);
yields to the illustration with inversed label. For that, we have started the arc from 0^\circ to 45^\circ . Adding the option \verb|reverse path| can do the job without inversing the path as follows:
\draw[decoration={text along path, text={CRITICAL},text align={center},
raise=0.2cm,reverse path},decorate] (0:3.5) arc (0:45:3.5);
with "reverse path" option
without "reverse path" option

5. How to rotate an object using TikZ

In this section, we will draw the speedometer needle. The basic idea is shown in the next animation where we draw a triangle and two circles (big one with black filling and small one with white filling). Drawing order has to be respected to get the final results. The following code:
\fill[black!70] (0,0.2)--(0:2.75) -- (0,-0.2)--cycle; % Triangle
\fill [black!70](0,0) circle (0.4cm); %Big circle
\fill [white](0,0) circle (0.15cm);% Small circle

To rotate the speedometer needle, we can add the option \verb|rotate=45| to the triangle draw code. The speedometer code is given below with all its parts.




% Static part
\draw[draw=white,fill=red,thick] (0:2) -- (0:3.5) arc(0:45:3.5) -- (45:2) arc(45:0:2) -- cycle ;
\draw[draw=white,fill=red!70,thick] (90:2cm)-- (90:3.5cm) arc (90:45:3.5) -- (45:2cm) arc (45:90:2);
\draw[draw=white,fill=red!50,thick] (135:2cm)-- (135:3.5cm) arc (135:90:3.5) -- (90:2cm) arc (90:135:2);
\draw[draw=white,fill=red!30,thick] (135:2cm)-- (135:3.5cm) arc (135:180:3.5) -- (180:2cm) arc (180:135:2);

\draw[decoration={text along path,
      text={LOW},text align={center},raise=0.2cm},decorate] (180:3.5cm) arc (180:135:3.5); 
\draw[decoration={text along path,
      text={MEDIUM},text align={center},raise=0.2cm},decorate] (135:3.5cm) arc (135:90:3.5);
\draw[decoration={text along path,
      text={HIGH},text align={center},raise=0.2cm},decorate] (90:3.5cm) arc (90:45:3.5);
\draw[decoration={text along path,
      text={CRITICAL},text align={center},raise=0.2cm},decorate] (45:3.5cm) arc (45:0:3.5);

% Speedometer needle
\fill[black!70,rotate=145] (0,0.2)--(0:2.75) -- (0,-0.2)--cycle;
\fill [black!70](0,0) circle (0.4cm);
\fill [white](0,0) circle (0.15cm);



You have reached the end of the tutorial 😊. If you have any questions or suggestions, please use the comments’ section below. Thanks!

Leave a Reply

Notify of
Close Menu