我需要一些有关 tikz 的帮助。我有多个带标签的节点,我的问题是节点下方的标签放置方式使其上边界对齐。我该如何更改它以使标签在其基线对齐?这是一个最小示例,节点上方的标签看起来不错,但下面的标签很糟糕。
\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}
答案1
快速破解使所有标签具有相同的高度:
\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}
答案2
本质上与@samcarter_is_at_topanswers.xyz (+1) 的解决方案相同,但为了方便起见,您可以创建一种\strut
在每个标签中添加的样式。
\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}
答案3
这是@samcarter_is_at_topanswers.xyz 的答案,没有使用节点标签。来自此链接
\strut——用于创建一个没有宽度、高度 8.6pt、深度 3pt 的隐形框。
\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}