3

I'm trying to make a Figure in tikz, I would the labels "u[i,J]" and "u[I,J]" to keep certain distance from his corresponding arrows, based on labelDist parameter.

As an example, is it possible to make the text "u[i,J]" to end at his corresponding node??, I thought right alignment could do the trick, but it seems to have another effect. In this case it seems simple for me to define a node where the text ends and the text label adjust automatically to be written to the left of the node location, is there another simple way to accomplish my objective?

I would also like to keep the distance from the text and the arrow defined based on labelDist parameter for aesthetic reasons.

\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
  • There are other options like` [above, sloped, pos=0.8]` for adding labels to arrows, but that seems to not be the case here. Nov 5 '18 at 23:11
2

Not sure I understand the question but are you looking for this?

\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}

enter image description here

2
  • yeah what you provided is what I was looking for. Can I ask why \node[left] is between the points that define the arrow and \node[right] at the end? Nov 5 '18 at 23:18
  • @FranciscoAngel This is how TikZ path construction works. You say \draw <coordinate> and then what TikZ should do with the coordinate. In this example I say "draw coordinate at its left" and then "draw a line to the next coordinate". When you arrive at the coordinate, you can put another node (or whatever).
    – user121799
    Nov 5 '18 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.