Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I center a TikZ node exactly between two others?

Hypothetically,

\node (a) {a}
\node (c) [right of=c] {c}
\node (b) [between={a,c}] {b}
share|improve this question

1 Answer

up vote 11 down vote accepted

My suggestion is to use the calc library (see the pgfmanual 13.5 Coordinate Calculations - version October 25, 2010).

An example:

\documentclass{article}   
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\scalebox{4}{
\begin{tikzpicture}[text height=2ex]
\node (a) {a};
\node (c) [right of=a] {c};
\node (b) at ($(a)!0.5!(c)$) {b};
\end{tikzpicture}
}
\end{document}

The result:

enter image description here

Notice that the syntax you used is wrong: each node should end with ; and the node c can not be positioned at its right.

Notice that without specifying the text height, the nodes are not vertically aligned:

\documentclass{article}   
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\scalebox{4}{
\begin{tikzpicture}
\node (a) {a};
\node (c) [right of=a] {c};
\node (b) at ($(a)!0.5!(c)$) {b};
\end{tikzpicture}
}
\end{document}

enter image description here

See as reference Problem with TikZ and vertical alignment of text inside nodes.

The calc library, however, is not the only way to proceed. In his comment percusse suggested another approach:

\documentclass{article}   
\usepackage{tikz}

\begin{document}
\scalebox{4}{
\begin{tikzpicture}[text height=2ex]
\node (a) {a};
\node (c) [right of=a] {c};
\path (a) -- (c) node[midway] (b) {b};
\end{tikzpicture}
}
\end{document}

and I see even one more (ok, is not so convenient, but I report it for the sake of completness). Suppose you are using the positioning library and nodes are placed on grid; then for sure the node distance is set in some way so one could go as follows:

\documentclass{article}   
\usepackage{tikz}
\usetikzlibrary{positioning}
\pgfmathtruncatemacro\distance{1}

\begin{document}
\scalebox{4}{
\begin{tikzpicture}[text height=2ex, on grid]
\node (a) {a};
\node (c) [right=\distance cm of a] {c};
\node (b) [right=0.5\distance cm of a]{b};
\end{tikzpicture}
}
\end{document}

Both approached lead to the result shown in the first example picture.

share|improve this answer
The code was only hypothetical. Not meant to be working code(just enough to get the point across). Anyways, your method works. Thanks. – AbstractDissonance Sep 14 '12 at 7:51
You can also add \path (a) -- (c) node[midway] (b) {b}; to the list. – percusse Sep 15 '12 at 11:04
1  
@percusse: absolutly right! Actually I see also another way besides your: I'll edit my answer in a minute. – Claudio Fiandrino Sep 16 '12 at 9:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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