2

I need some help with tikz. I have multiple nodes with labels and my problem is that the labels that are below a node are placed in such a way that their upper boundaries align. How can i change it such that the labels align at their baseline? Here a minimal example, the labels above the nodes look fine but the ones below are horrible.

enter image description here

\begin{tikzpicture}
\node[circle,fill,label=above:$n$] at (0,1) {};
\node[circle,fill,label=below:$n$] at (0,0) {};
\node[circle,fill,label=above:$n-1$] at (1,1) {};
\node[circle,fill,label=below:$n-1$] at (1,0) {};
\node[circle,fill,label=above:$c$] at (2,1) {};
\node[circle,fill,label=below:$c$] at (2,0) {};
\node[circle,fill,label=above:$d$] at (3,1) {};
\node[circle,fill,label=below:$d$] at (3,0) {};
\end{tikzpicture}
Florian
  • 199
  • 2
    Add \vphantom{d1-} (or \strut or \mathstrut) too all or set a fixed text height for all, text height={height("$d1-$"}) to mimic the \vphantom solution. See also Q107227 and related question. TikZ packs all text relatively tightly and uses the border of the text to place it. You will need to have all nodes the same vertical dimension to have them align properly. – Qrrbrbirlbel Feb 02 '23 at 10:43

3 Answers3

5

Quick hack to make all the labels the same height:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture} \node[circle,fill,label=above:\strut$n$] at (0,1) {}; \node[circle,fill,label=below:\strut$n$] at (0,0) {}; \node[circle,fill,label=above:\strut$n-1$] at (1,1) {}; \node[circle,fill,label=below:\strut$n-1$] at (1,0) {}; \node[circle,fill,label=above:\strut$c$] at (2,1) {}; \node[circle,fill,label=below:\strut$c$] at (2,0) {}; \node[circle,fill,label=above:\strut$d$] at (3,1) {}; \node[circle,fill,label=below:\strut$d$] at (3,0) {}; \end{tikzpicture}

\end{document}

enter image description here

3

Essentially the same as the solution by @samcarter_is_at_topanswers.xyz (+1), but for convenience you can make a style that adds a \strut to each label.

enter image description here

\documentclass{article}

\usepackage{tikz} \tikzset{mynode/.style={circle, fill, label=#1\strut}}

\begin{document} \begin{tikzpicture} \node[mynode=above:$n$] at (0,1) {}; \node[mynode=below:$n$] at (0,0) {}; \node[mynode=above:$n-1$] at (1,1) {}; \node[mynode=below:$n-1$] at (1,0) {}; \node[mynode=above:$c$] at (2,1) {}; \node[mynode=below:$c$] at (2,0) {}; \node[mynode=above:$d$] at (3,1) {}; \node[mynode=below:$d$] at (3,0) {}; \end{tikzpicture} \end{document}

Sandy G
  • 42,558
2

This is @samcarter_is_at_topanswers.xyz's answer without using label of nodes. From this link

\strut - Used to create an invisible box with no width, height 8.6pt and depth 3pt.

enter image description here

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[c/.style={circle,fill=brown,inner sep=1.5pt}]
\path
(0,1) node[c]{} node[above]{\strut$n$} 
(1,1) node[c]{} node[above]{\strut$n-1$}
(2,1) node[c]{} node[above]{\strut$c$}
(3,1) node[c]{} node[above]{\strut$g$}
;
\path
(0,0) node[c]{} node[below]{\strut$n$} 
(1,0) node[c]{} node[below]{\strut$n-1$}
(2,0) node[c]{} node[below]{\strut$h$}
(3,0) node[c]{} node[below]{\strut$y$}
;
\end{tikzpicture}
\end{document}
Black Mild
  • 17,569