[Tex/LaTex] tikz – placing two nodes side by side

tikz-pgf

my preamble:

\documentclass{beamer}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{positioning}

Using tikz, I would like to write the word "event" and the word "cumulative" side by side. My problem is that the word "event" appears to be slightly above "cumulative"… Can you help me?

\begin{document}
\begin{frame}

\begin{center}
\begin{tikzpicture}
\node (1st) {event};
\node[right=of 1st] (2nd) {cumulative};
\end{tikzpicture}
\end{center}

\end{frame}
\end{document}

enter image description here

Best Answer

When you use right=of TikZ actually places the .west anchor of the second node a specific distance right of the first node’s .east anchor. For nodes with different heights and depths those anchors (along with the .center anchor) do not lie the same distance from the baseline (anchors .text, .base, .base west, .base east).

When you use base right=of the anchors .base west (new node) and .base east (referenced node) are used. The same applies for the mid anchors and mid right as the mid anchors simply lie .5ex above the baseline.

You can also alter the height and depth of the nodes’ text boxes with the keys text height and text depth which has a different effect but may be more appropriate if you want to draw the nodes or use them for a vertical reference. You can still use base right=of here, of course. Instead of guessing or setting a specific text height/depth you can also use \vphantom{<heighest and deepest characters>} (for the font key) or text height=\heightof{<heighest character>} and so on.

In the example below I have used an exaggerated example.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\tikzset{nodes={draw=gray, text=gray}}
\begin{document}
\begin{tikzpicture}
\node               (1st) {evena};
\node[right=of 1st] (2nd) {cumulative\rule{.4pt}{2em}};

\draw (2nd.base) -- ++ (left:3);
\draw (2nd.center) -- (1st.center);
\draw[thick] (2nd.west) -- (1st.east);
\end{tikzpicture}

\begin{tikzpicture}
\node                    (1st) {evena};
\node[base right=of 1st] (2nd) {cumulative\rule{.4pt}{2em}};

\draw (2nd.base) -- ++ (left:3);
\draw (2nd.center) -- (1st.center);
\draw[thick] (2nd.west) -- (1st.east);
\end{tikzpicture}

\begin{tikzpicture}
\node[text height=2em]  (1st) {evena};
\node[right=of 1st]     (2nd) {cumulative\rule{.4pt}{2em}};

\draw (2nd.base) -- ++ (left:3);
\draw (2nd.center) -- (1st.center);
\draw[thick] (2nd.west) -- (1st.east);
\end{tikzpicture}
\end{document}

Output

enter image description here enter image description here enter image description here