节点和文本定位

节点和文本定位

我想tikz在数学模式下使用文本绘制圆圈节点,该文本具有以下结构。一个变量加上可能的下标和/或上标。无论我是否添加下标或上标,我希望变量在圆圈内的位置完全相同。

我已完成以下操作:

\documentclass[12pt]{article}
\usepackage{amsmath,mathtools,calc}
\usepackage{tikz}\usetikzlibrary{positioning}
\newcommand{\supe}[1]{\smash{\mathrlap[\scriptstyle]{^{\ensuremath{#1}}}}}
\newcommand{\indi}[1]{\smash{\mathrlap[\scriptstyle]{_{\ensuremath{#1}}}}}
\newcommand{\suin}[2]{\smash{\mathrlap[\scriptstyle]{^{\ensuremath{#1}}_{\ensuremath{#2}}}}}
\begin{document}
\begin{tikzpicture}
\begin{scope}[every circle node/.style={circle,draw,minimum height=1cm}]
\node [circle] (p) at (1,1) {$p$} ;
\node [circle,right=  of p]  (r1) {$p\indi{1}$} ;
\node [circle,right= of r1]  (r2) {$p\supe{2}$} ;
\node [circle,right= of r2]  (r3) {$r\suin{1}{3}$} ;
\end{scope}
\end{tikzpicture}
\end{document}

这似乎效果很好,但我想知道是否有更佳tikz的方法来实现相同的结果。

答案1

虽然不是更 TikZy 的方式,但无论如何都是更好的 LaTeX:

\documentclass[12pt]{article}
\usepackage{amsmath,mathtools,calc}
\usepackage{tikz}\usetikzlibrary{positioning}

\newcommand{\supe}[1]{^{\smash{\mathrlap{#1}}}}
\newcommand{\indi}[1]{_{\smash{\mathrlap{#1}}}}
\newcommand{\suin}[2]{\supe{#1}\indi{#2}}

\begin{document}
\begin{tikzpicture}
\begin{scope}[every circle node/.style={circle,draw,minimum height=1cm}]
\node [circle] (p) at (1,1) {$p$} ;
\node [circle,right=  of p]  (r1) {$p\indi{1}$} ;
\node [circle,right= of r1]  (r2) {$p\supe{2}$} ;
\node [circle,right= of r2]  (r3) {$r\suin{1}{3}$} ;
\end{scope}
\end{tikzpicture}
\end{document}

请注意,通过这种方式,您要将上标和下标设置在它们所属的位置,而不是像代码中那样设置在空公式中。

在此处输入图片描述

更简单的版本,相同的结果:

\documentclass[12pt]{article}
\usepackage{amsmath,mathtools,calc}
\usepackage{tikz}\usetikzlibrary{positioning}

\newcommand{\fixedwidth}[2]{%
  \makebox[\widthof{$#1$}][l]{\vphantom{$#1$}\smash{$#1#2$}}%
}

\begin{document}
\begin{tikzpicture}
\begin{scope}[every circle node/.style={circle,draw,minimum height=1cm}]
\node [circle] (p) at (1,1) {$p$} ;
\node [circle,right=  of p]  (r1) {\fixedwidth{p}{_1}} ;
\node [circle,right= of r1]  (r2) {\fixedwidth{p}{^2}} ;
\node [circle,right= of r2]  (r3) {\fixedwidth{r}{^1_3}} ;
\end{scope}
\end{tikzpicture}
\end{document}

答案2

我认为没有内置任何特定功能TikZ来简化此问题。也许您可以使用TikZ进行放置来获得此效果,但这肯定不会比在 之外实现相同效果更容易TikZ

但是,我认为你只用一个宏就可以做到:

\newcommand{\Smash}[1]{\smash{\mathrlap[\scriptstyle]{#1}}}

并将您的输出与仅使用的输出进行比较\Smash

在此处输入图片描述

代码:

\documentclass[12pt]{article}
\usepackage{amsmath,mathtools,calc}
\usepackage{tikz}\usetikzlibrary{positioning}
\newcommand{\supe}[1]{\smash{\mathrlap[\scriptstyle]{^{\ensuremath{#1}}}}}
\newcommand{\indi}[1]{\smash{\mathrlap[\scriptstyle]{_{\ensuremath{#1}}}}}
\newcommand{\suin}[2]{\smash{\mathrlap[\scriptstyle]{^{\ensuremath{#1}}_{\ensuremath{#2}}}}}

\newcommand{\Smash}[1]{\smash{\mathrlap[\scriptstyle]{#1}}}

\begin{document}
Original version:

\begin{tikzpicture}
\begin{scope}[every circle node/.style={circle,draw,minimum height=1cm}]
\node [circle] (p) at (1,1) {$p$} ;
\node [circle,right=  of p]  (r1) {$p\indi{1}$} ;
\node [circle,right= of r1]  (r2) {$p\supe{2}$} ;
\node [circle,right= of r2]  (r3) {$r\suin{1}{3}$} ;
\end{scope}
\end{tikzpicture}

Simplified version:

\begin{tikzpicture}
\begin{scope}[every circle node/.style={circle,draw,minimum height=1cm}]
\node [circle] (p) at (1,1) {$p$} ;
\node [circle,right=  of p]  (r1) {$p\Smash{_1}$} ;
\node [circle,right= of r1]  (r2) {$p\Smash{^2}$} ;
\node [circle,right= of r2]  (r3) {$r\Smash{^{1}_{3}}$} ;
\end{scope}
\end{tikzpicture}

\end{document}

答案3

也许我忽略了一些东西,我不知道为什么这没有被采纳,但text depth似乎已经足够了。

\documentclass[border=5mm]{standalone}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
\begin{scope}[
    every circle node/.style={
        draw,
        inner sep=2pt,
        text depth=0.45ex,
        text height=1.35ex,
        minimum size=1cm
    }
]
\node [circle] (p) at (1,1) {$p$};
\node [circle,right=  of p]  (r1) {$p_1$};
\node [circle,right= of r1]  (r2) {$p^2$};
\node [circle,right= of r2]  (r3) {$r_1^3$};
\node [circle,below= 3mm of p]  (r4) {$A_1^3$};
\node [circle,right= of r4]  (r5) {$M_M^M$};
\node [circle,right= of r5]  (r6) {$M_p^h$};
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容