3

I have a simple problem with tikzpicture. I wanted to have the line exiting the box labelled 10 from its right edge on the box outline.

enter image description here

Here there is my code:

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
        \draw[->]
        (0,0) node[rectangle,draw] {$10$} -- (1,0) node[anchor=west,rectangle,draw] {$12$}
        ;
    \end{tikzpicture}
    \caption{}
    \label{}
\end{figure}

I am aware I can name them for instance a and b and then do

\draw[->] (a.east) -- (b.west)

But I was looking for a way that does not involve me labeling each node and linking them since I'll be making a lot of these nodes.

1
  • Welcome to TeX.SE! You draw line between coordinates (0,0) and (1,0) and put first node on coordinate (0,0) and second right from (1,0). Try \begin{tikzpicture} \draw[->] (0,0) node[draw,left] {$10$} -- (1,0) node[draw,right] {$12$}; \end{tikzpicture}.
    – Zarko
    Commented Apr 23, 2020 at 1:22

3 Answers 3

4

Let me convert my comment to answer:

  • you draw line between coordinates (0,0) and (1,0)
  • left nodes is at coordinate (0,0), right node is right from coordinate (1,0), consequently it seem that arrows is draw from center of left node to left border of right border.
  • possible solution: draw left node left of given coordinate (0,0):
\documentclass[tikz, border=1cm]{standalone}

\begin{document}
 \begin{tikzpicture}
    \draw[->] 
        (0,0) node[draw, anchor=east] {10} -- 
        (1,0) node[draw, anchor=west] {12};
    \end{tikzpicture}
\end{document}

or

\documentclass[tikz, border=1cm]{standalone}

\begin{document}
 \begin{tikzpicture}
    \draw[->] 
        (0,0) node[draw, left] {10} -- 
        (1,0) node[draw, right] {12};
    \end{tikzpicture}
\end{document}

in both example you will get the same result:

enter image description here

5

Use the shorten <= _____ option. The downside of this method is that you will need to come-up with the correct value using trial and error (it is expected since you don't have a reference to the starting/ending coordinates).

Output

\documentclass[border=1cm]{standalone}
\usepackage{tikz}

\begin{document}
 \begin{tikzpicture}
        \draw[->,shorten <=8.5pt] 
        (0,0) node[rectangle,draw] {$10$} -- 
        (1,0) node[anchor=west,rectangle,draw] {$12$};
    \end{tikzpicture}
\end{document}
5

Just give the node a name and use it.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[->]
        (0,0) node[rectangle,draw] (a){$10$}
        (a) -- (1,0) node[anchor=west,rectangle,draw] {$12$}
        ;
\end{tikzpicture}
\end{document}

enter image description here

Note that this does not distort the node positions and also works for sloped arrows.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[->]
        (0,0) node[rectangle,draw] (a){$10$}
        (a) -- (1,0.5) node[anchor=west,rectangle,draw] {$12$}
        ;
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .