0

Say, I want to draw a picture showing the export and import from USA to Canada, the code I tried was:

\begin{tikzpicture}
\node (a) at (0,0){USA};
\node (b) at (1,0){Canada}; 
\draw [->] (a)--node[above]{export}(b); 
\draw [->](b)--node[below]{import}(a);
\end{tikzpicture}

I know this is absolutely wrong cause it only generates one arrow with two heads. How could I modify this code?

2 Answers 2

2

Welcome to TeX.SE. You can use the to operator and its bend left or bend right options. This operators accepts other options such as looseness to control the curvature, in and out to control the start and end angles, see for instance here. For more information on the available options, you can look for /tikz/curve to in the TikZ & PGF manual (page 838 in version 3.1.5b of the manual).

\documentclass[tikz, border=2mm]{standalone}

\begin{document}

\begin{tikzpicture}[scale=3]
\node (a) at (0,0) {USA};
\node (b) at (1,0) {Canada};
\draw[->] (a) to[bend left=20] node[above] {export} (b);
\draw[->] (b) to[bend left=20] node[below] {import} (a);
\end{tikzpicture}

\end{document}

enter image description here

If you omit [bend left=20] for an arrow, or use [bend left=0] or [bend right=0], TikZ will draw a straight line (a segment).

If you want to shift such arrows, I think you need to choose an anchor on each of the nodes a and b, so that the points to shift are well-known. For instance, if I choose anchors a.30 (30 is a polar angle in degrees) and b.150 (150 = 180 - 30 for symmetry), shift these points upwards by 0.1cm and increase the curvature of the upper arrow, we get:

\documentclass[tikz, border=2mm]{standalone}

\begin{document}

\begin{tikzpicture}[scale=3]
\node (a) at (0,0) {USA};
\node (b) at (1,0) {Canada};
\draw[->] ([yshift=0.1cm]a.30) to[bend left=40] node[above] {export}
          ([yshift=0.1cm]b.150);
\draw[->] (b) to[bend left=20] node[below] {import} (a);
\end{tikzpicture}

\end{document}

enter image description here

A negative value for yshift would shift downwards, of course.

Another possibility for drawing the curved arrows would be to use the .. operator, but it may be a bit more difficult, as you need to provide appropriate control points.

4
  • Thank you very much! You have completely solved my problem! BTW, what will I do if I just draw two straight arrows without any curvature(or maybe zero curvature is an alternative)? is there any option to move a given arrow upward or downward for a given distance? Thanks Commented Apr 23, 2020 at 15:09
  • Please see the updated answer.
    – frougon
    Commented Apr 23, 2020 at 15:23
  • I really appreciate you for giving me a complete and patient instruction! I learned a lot! also, I am sorry for wasting your time. Thanks! Commented Apr 23, 2020 at 15:30
  • It's okay, you didn't force me to answer. :-) Glad it helped.
    – frougon
    Commented Apr 23, 2020 at 15:32
1

A small variation og @frougon answer. With using packages arrows.meta, positioning and quotes the MWE for you diagram can be:

\documentclass[tikz, margin=2mm]{standalone}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
    node distance = 22mm,
every edge/.style = {draw, -Straight Barb, bend left=15},
                        ]
\node (a)               {USA};
\node (b) [right=of a]  {Canada};
\draw   (a) edge["export"] (b)
        (b) edge["import"] (a);
    \end{tikzpicture}

    \begin{tikzpicture}[
node distance = 22mm,
    LA/.style = {draw, -Straight Barb, transform canvas={yshift=#1}},
every edge quotes/.append style = {font=\small, inner sep=1pt}
                        ]
\node (a)               {USA};
\node (b) [right=of a]  {Canada};
\draw   (a) edge[LA=+2pt,"export"] (b)
        (b) edge[LA=-2pt,"import"] (a);
    \end{tikzpicture}
\end{document}

enter image description here

1
  • Thanks for your help! Including usetikzlibrary with such packages is a very good idea to shift an arrow using relative distance. I have tried the usetikzlibrary with calc package which is hard to manipulate cause horizontal distance is also required to avoid overlapping. It's very kind of you! Commented Apr 24, 2020 at 2:21

You must log in to answer this question.

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