5

I have a transportation-problem that I would like to draw. I would like to place nodes next to the arrows so they automatically fit in between them. See example in picture below. I am looking for a smart method to place the numbers "5, 3, 4". Example picture:

Figure of how I want it to look

My code:

\begin{figure}
\centering
\begin{tikzpicture}[scale=1]
    \draw [fill=white, thick] (0,0) circle [radius=0.5];
    \node at (0,0) {$B_{4}$};
    \node [left] at (-0.5,0) {4};
    \draw [black, thick] (0,3) circle [radius=0.5];
    \node at (0,3) {$B_{3}$};
    \node [left] at (-0.5,3) {6};
    \draw [black, thick] (0,6) circle [radius=0.5];
    \node at (0,6) {$B_{2}$};
    \node [left] at (-0.5,6) {8};
    \draw [black, thick] (0,9) circle [radius=0.5];
    \node at (0,9) {$B_{1}$};
    \node [left] at (-0.5,9) {5};
    \draw [black, thick] (7,1.5) circle [radius=0.5];
    \node at (7,1.5) {$E_{3}$};
    \node [right] at (7.5,1.5) {9};
    \draw [black, thick] (7,4.5) circle [radius=0.5];
    \node at (7,4.5) {$E_{2}$};
    \node [right] at (7.5,4.5) {6};
    \draw [black, thick] (7,7.5) circle [radius=0.5];
    \node at (7,7.5) {$E_{1}$};
    \node [right] at (7.5,7.5) {8};
    %Arrows from B4
    \draw [->, ultra thick, shorten <= 0.5cm, shorten >= 0.5cm] (0,0) -- (7,1.5);
    \draw [->, shorten <= 0.5cm, shorten >= 0.5cm] (0,0) -- (7,4.5);
    \draw [->, ultra thick, shorten <= 0.5cm, shorten >= 0.5cm] (0,0) -- (7,7.5);
    %Arrows from B3
    \draw [->, shorten <= 0.5cm, shorten >= 0.5cm] (0,3) -- (7,1.5);
    \draw [->, shorten <= 0.5cm, shorten >= 0.5cm] (0,3) -- (7,4.5);
    \draw [->, ultra thick, shorten <= 0.5cm, shorten >= 0.5cm] (0,3) -- (7,7.5);
    %Arrows from B2
    \draw [->, ultra thick, shorten <= 0.5cm, shorten >= 0.5cm] (0,6) -- (7,1.5);
    \draw [->, ultra thick, shorten <= 0.5cm, shorten >= 0.5cm] (0,6) -- (7,4.5);
    \draw [->, shorten <= 0.5cm, shorten >= 0.5cm] (0,6) -- (7,7.5);
    %Arrows from B1
    \draw [->, shorten <= 0.5cm, shorten >= 0.5cm] (0,9) -- (7,1.5);
    \draw [->, ultra thick, shorten <= 0.5cm, shorten >= 0.5cm] (0,9) -- (7,4.5);
    \draw [->, shorten <= 0.5cm, shorten >= 0.5cm] (0,9) -- (7,7.5);  

\end{tikzpicture}
\caption{}
\label{fig:label}
\end{figure}

Execution of my code:

My current code executed

Can anyone suggest a package that can be used to solve this? Thanks!

1
  • Simply change the arrow tip to another better one ,>=latex' for example from the arrows library.
    – AboAmmar
    Nov 1 '16 at 19:26
6

enter image description here

I reduce your code significantly ... and for this employ TikZ libraries calc, chains and quotes:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,chains,quotes}

\begin{document}
\begin{tikzpicture}[
node distance = 11mm and 44mm,
  start chain = going above,
    MN/.style = {% My Node ...
        circle, draw, thick, minimum size=6mm, inner sep=0mm,
        on chain},
every edge quotes/.style={%
        auto, inner sep=0.5pt, font=\footnotesize, 
        pos=0.15}
                    ]
\node (a1) [MN,label=left:4] {$B_{4}$};
\node (a2) [MN,label=left:6] {$B_{3}$};
\node (a3) [MN,label=left:8] {$B_{2}$};
\node (a4) [MN,label=left:5] {$B_{1}$};
%
\node (b1) [MN,label=right:9,
            right=of $(a1)!0.5!(a2)$]    {$B_{3}$};
\node (b2) [MN,label=right:6]           {$B_{2}$};
\node (b3) [MN,label=right:8]           {$B_{1}$};
    %Arrows from B4
\draw[->]   (a1) edge ["2"] (b1)
            (a1) edge ["4"] (b2)
            (a1) edge ["7"] (b3);
    %Arrows from B3
\draw[->]   (a2) edge ["4"] (b1)
            (a2) edge ["2"] (b2)
            (a2) edge ["3"] (b3);
    %Arrows from B2
\draw[->]   (a3) edge ["3"] (b1)
            (a3) edge ["2"] (b2)
            (a3) edge ["4"] (b3);
    %Arrows from B1
\draw[->]   (a4) edge ["4"] (b1)
            (a4) edge ["3"] (b2)
            (a4) edge ["5"] (b3);
\end{tikzpicture}
\end{document}

As you can see, the drawing circle, puting in it a node and use node for labeling is replaced by one node with label. Naming this nodes make drawing arrows simple.

Nodes are in chain, so their placement is simple. Distance between them is controlled by node distance. Nodes' style is determined in tikzpicture options.

Addendum: Above solution doesn't place edges' labels on the same x coordinate. This seem is caused by option auto. if it is replaced by above, i.e.

every edge quotes/.style={%
        above, font=\footnotesize, 
        pos=0.15}

which gives:

enter image description here

4
  • Wauw! That's way more elegant. I have spent some time to be able to name my nodes in the future like you just did. I have one more question: how can I align/position the labels so they have same "x-coordinate"?
    – Line
    Nov 2 '16 at 3:39
  • @Line, discrepancy of labels' positions is caused by option auto. See addendum in the my answer.
    – Zarko
    Nov 2 '16 at 8:53
  • I did already add above to my code, but it is still not perfectly aligned, it is acceptable though. I have been trying to edit 'pos=' but it doesn't help. Thanks anyway. You solved the primary issue Zarko
    – Line
    Nov 2 '16 at 16:46
  • it can not be exactly positioned, since pos=... determine percentage of line width. And they are of different width ... You can positioned each label individually (lot of work ... :-( ) with add individual value to each label, like ... edge [pos=<value>,"<number>"] ....
    – Zarko
    Nov 2 '16 at 17:03

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.