我正在尝试在 tikz 中制作一个图形,我会根据 labelDist 参数使标签“u[i,J]”和“u[I,J]”与其对应箭头保持一定距离。
例如,是否可以使文本“u[i,J]”在其对应的节点处结束???我以为右对齐可以解决问题,但似乎有其他效果。在这种情况下,我似乎可以简单地定义一个节点,文本在该节点处结束,并且文本标签会自动调整为写在节点位置的左侧,还有其他简单的方法可以实现我的目标吗?
出于美观原因,我还想保持基于 labelDist 参数定义的文本和箭头之间的距离。
\documentclass[12pt]{article}
\usepackage[spanish,es-tabla,es-nodecimaldot,es-noquoting]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\newcommand{\cellSize}{2}
\newcommand{\arrowSize}{0.4}
\newcommand{\nodeRadius}{0.125}
\newcommand{\labelDist}{0.25}
\begin{document}
\begin{tikzpicture}[scale=1.5]
% P grid
\draw (0,0)--(\cellSize,0)--(\cellSize,\cellSize)--(0,\cellSize)--cycle;
\draw (0.5*\cellSize,0.5*\cellSize) circle [radius = \nodeRadius];
% Labels
\draw[->] (-0.5*\arrowSize,0.5*\cellSize)--(0.5*\arrowSize,0.5*\cellSize);
\node[align=right] at (-0.5*\arrowSize-\labelDist,0.5*\cellSize) {u[i,J]};
\draw[->] (\cellSize-0.5*\arrowSize,0.5*\cellSize)--(\cellSize+0.5*\arrowSize,0.5*\cellSize);
\node[align=left] at (\cellSize+0.5*\arrowSize+\labelDist,0.5*\cellSize) {u[i+1,J]};
\draw[->] (0.5*\cellSize,-0.5*\arrowSize)--(0.5*\cellSize,0.5*\arrowSize);
\node at (0.5*\cellSize,-0.5*\arrowSize-\labelDist) {v[I,j]};
\draw[->] (0.5*\cellSize,\cellSize-0.5*\arrowSize)--(0.5*\cellSize,\cellSize+0.5*\arrowSize);
\node at (0.5*\cellSize,\cellSize+0.5*\arrowSize+\labelDist) {v[I,j+1]};
\end{tikzpicture}
\end{document}
答案1
我不确定我是否理解了这个问题但您是在寻找这个吗?
\documentclass[12pt]{article}
\usepackage[spanish,es-tabla,es-nodecimaldot,es-noquoting]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\newcommand{\cellSize}{2}
\newcommand{\arrowSize}{0.4}
\newcommand{\nodeRadius}{0.125}
\newcommand{\labelDist}{0.25}
\begin{document}
\begin{tikzpicture}[scale=1.5]
% P grid
\draw (0,0)--(\cellSize,0)--(\cellSize,\cellSize)--(0,\cellSize)--cycle;
\draw (0.5*\cellSize,0.5*\cellSize) circle [radius = \nodeRadius];
% Labels
\draw[->] (-0.5*\arrowSize,0.5*\cellSize)node[left] {u[i,J]}
--(0.5*\arrowSize,0.5*\cellSize);
\draw[->] (\cellSize-0.5*\arrowSize,0.5*\cellSize)
--(\cellSize+0.5*\arrowSize,0.5*\cellSize) node[right]{u[i+1,J]};
\draw[->] (0.5*\cellSize,-0.5*\arrowSize)node[below]{v[I,j]}
--(0.5*\cellSize,0.5*\arrowSize);
\draw[->] (0.5*\cellSize,\cellSize-0.5*\arrowSize)--(0.5*\cellSize,\cellSize+0.5*\arrowSize)
node[above]{v[I,j+1]};
\end{tikzpicture}
\end{document}