tikz 中的垂直错位节点

tikz 中的垂直错位节点

你能解释为什么标有 s 的节点在下面的 tikzpicture 上未对齐吗?

\documentclass[tikz]{standalone}

\begin{document}
    \begin{tikzpicture}
\node at (0,0) {d};
\node at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

默认情况下tikz将节点对齐中心围绕着它们的盒子。

锚点默认

\documentclass[tikz,border=5mm]{standalone}

\begin{document}
    \begin{tikzpicture}
\node[draw,anchor=center] at (0,0) {d};
\node[draw,anchor=center] at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

将节点对齐根据,只需选择节点库作为anchor

锚基

\documentclass[tikz,border=5mm]{standalone}

\begin{document}
    \begin{tikzpicture}
\node[draw,anchor=base] at (0,0) {d};
\node[draw,anchor=base] at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

锚点不绘制

\documentclass[tikz,border=5mm]{standalone}

\begin{document}
    \begin{tikzpicture}
\node[anchor=base] at (0,0) {d};
\node[anchor=base] at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

请参阅手册第 227 页。

补充回答@bmv 的问题

使用文本高度参数调整框中文本的高度。

文字高度

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
\node[draw,anchor=base,text height=0pt] at (0,0) {d};
\node[draw,anchor=base,text height=0pt] at (0.5,0) {s};
    \end{tikzpicture}

     \begin{tikzpicture}
\node[draw,anchor=base,text height=5pt] at (0,0) {d};
\node[draw,anchor=base,text height=5pt] at (0.5,0) {s};
    \end{tikzpicture}   

        \begin{tikzpicture}
\node[draw,anchor=base,text height=10pt] at (0,0) {d};
\node[draw,anchor=base,text height=10pt] at (0.5,0) {s};
    \end{tikzpicture}
\end{document}

答案2

替代解决方案:

\documentclass[tikz, margin=3.141592mm]{standalone}

\begin{document}
    \begin{tikzpicture}[box/.style={draw, text height=1.5ex, text depth=0.5ex}]
\node[box] at (0,0)     {d};
\node[box] at (0.5,0)   {s};
\node[box] at (1.0,0)   {p};
\node[box] at (1.5,0)   {g};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容