Tx · LaTeX & TikZ

How to Move Text Higher in TikZ

Published By the 9to5Science editorial team

At a glance

  1. 1. Find the node

    Locate the node option that places the text on a path or at a coordinate.

  2. 2. Add a vertical shift

    Use yshift with a length such as 2mm to move the node upward.

  3. 3. Set its alignment

    Use above to place the label above the path instead of centering it on the path.

To move a TikZ label a little higher, add yshift to the label’s node options. A positive shift moves the node upward; a negative shift moves it downward. If the label belongs above a line, use above as well so TikZ places the node above the line before applying the extra shift.

Move a label on a path

Here is a complete minimal example. It places a label halfway along a horizontal line, above the line, then shifts it up by another 2 mm.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw (0,0) -- (4,0)
    node[midway, above, yshift=2mm] {Label};
\end{tikzpicture}
\end{document}

The key parts are:

  • midway places the node at the midpoint of the path.
  • above aligns the label above the path. It is also possible to write above=2mm to specify the initial gap from the path.
  • yshift=2mm adds a further upward offset.

You can change 2mm to another length, such as 1pt, 0.1cm, or 3mm. TikZ uses TeX length units, so include a unit.

Shift a node at a coordinate

The same node option works when the label is placed at a coordinate rather than attached to a path:

\node[yshift=2mm] at (2,1) {Higher text};

This shifts the entire node upward from (2,1). If you also want the node to sit above that coordinate, use an anchor or a placement option, for example:

\node[anchor=south, yshift=2mm] at (2,1) {Higher text};

anchor=south makes the bottom of the node align with the shifted coordinate. Without an explicit anchor, TikZ centers the node on the coordinate by default.

Choose between above and yshift

Use above when you want the label positioned relative to a path or another node. Use yshift when you want a small, explicit adjustment to the node’s position. They can be combined when the standard placement is almost right but needs fine-tuning.

For example, node[above=1mm, yshift=1mm] {Label} gives the label a 1 mm gap above the path and then shifts it up another 1 mm. If the text is already in the correct position and only needs to move, yshift=2mm is often the simplest change.

Common mistakes

  • Using a negative shift to move text up: Positive yshift values move upward; negative values move downward.
  • Forgetting the node options: Put the shift inside the node’s brackets, such as node[yshift=2mm] {Label}.
  • Expecting yshift to change only the letters: It moves the whole node, including its text area and any node shape or border.
  • Using above when you need an exact offset: above sets the relative placement; use a length such as above=2mm or add yshift to fine-tune it.

If you only need to move the label slightly, try yshift=1mm or yshift=2mm first, then adjust the length until the spacing looks right.

References

  1. The TikZ and PGF Packages: Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.