TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a question on using the tikzpicture package.

I have a protocol interaction on which two participants want to exchange some numbers, say α and β. I need bilateral arrows, and on top of it would be α and below it the β. That means these two participants are exchanging the numbers simultaneously. BTW, I don't need dots in the side of arrows, but some calculations which results in α or β must be placed there.

alt text

How can I have this output? I don't know if I have chosen the right package but it seems to me that this is the right choice. Every sample that I have seen so far uses some dots and creates a matrix, but I don't know how to replace dots with these terms. BTW, I have no idea how can I have that arrow and even making it larger.
Any good tutorial with examples close to my need will be appreciated.

share|improve this question
@Yasser: If you put the image on imgur or a similar site and linked to that instead, it would be more convenient, since we wouldn't have to download the image to our hard drives. – Antal Spector-Zabusky Dec 2 '10 at 9:52
@Antal S-Z: I think that might require a certain amount of reputation. But I'll embed the image for you and him. – Matthew Leingang Dec 2 '10 at 10:22
@Yasser: can you please upload your image to a different site, preferably imgur? The one you chose looks like it requires users to create another account to see your file. – Matthew Leingang Dec 2 '10 at 10:26
@Matthew: I thought I'd seen links to images on similar posts (by low-rep posters) before, though I could very well be mistaken. Also, although ifile's a bit ugly, you can download the image if you click enough times. I did, so here it is on imgur. – Antal Spector-Zabusky Dec 2 '10 at 10:28
@Antal: thanks for bravely clicking through. I embedded it. I have done that kind of edit for low-rep posters before as well, so if you saw that it might have been my handiwork. – Matthew Leingang Dec 2 '10 at 10:38
up vote 11 down vote accepted

You don't need TikZ for this. If you're using LaTeX, you just need amsmath and the mathtools package (both of which you likely have):


      x \xleftarrow{\quad R\quad} \mathcal{Z}_q            &\quad
      \xrightleftharpoons[\beta=g^y]{\quad\alpha=g^x\quad} \quad&
      y \xleftarrow{\quad R\quad} \mathcal{Z}_q            \\
      g^{xy} & & g^{xy} \\

This produces the following output:

Extensible arrows and harpoons.

The amsmath package provides the \xleftarrow and \xrigharrow commands, which set an eXtensible \leftarrow and \righarrow, respectively; their mandatory argument is set on top, and their optional argument is set below (\xleftarrow[below]{above}). Normally, it's just as wide as necessary for the text, so I inserted \quads around the text to space it out. The mathtools package, which extends amsmath, provides more extensible arrows, including \xleftrightarrow (for an extensible ↔), \xLeftrightarrow (for an extensible ⇔), \xleftrighharpoons (for an extensible ⇋; the upside-down version of the symbol in the example picture), and \xrightleftharpoons (for an extensible ⇌, as seen in the example picture), any of which could be used here depending on your tastes.

Also, note that you shouldn't need to space out the \xrightleftharpoons like I did (which is why there's &\quad ... \quad&), but apparently I needed to here (though in LaTeXit I didn't, so with some small change you probably wouldn't need to). You only need the matrix here since you want to align the g^{xy}s with the first and third arrows; if you just wanted the top line, you wouldn't need to bother.

share|improve this answer
Thank you Antal. Excellent! – Yasser Sobhdel Dec 2 '10 at 12:43

Here's one with eplain:

\input eplain
  \matrix{x \mapleft^R {\cal Z}_q &
  \adjmapleft^{\mathstrut\alpha = g^x}_{\beta = g^y} &
  y \mapleft^R {\cal Z}_q \cr
  g^{xy} && g^{xy}}

Which looks like:

No harpoons, though :-(

Update: Regarding the way some arrows and such expand to the “contents”, I just wanted to point out that there seems to be a couple of ways of achieving that:

  1. Character combinations with \leaders
    • pro: expands nicely—as long or as short as necessary
    • contra: the combination can be clearly seen on display for some reason (not in print). Depending on zoom-levels and the used reader, the effect can be jarring. I remember seeing a discussion about this, but can't remember if there was some good results achieved.
  2. Selecting a fitting variant based on the content
    • pro: looks nice and crisp on screen, too
    • contra: doesn't necessary expand as long as needed.

Now, regarding the second list item in the context of XeTeX and Unicode, some characters seem to support automatic variant selection. I don't know if that happens at the font itself, XeTeX, or somewhere else. Regardless, I've compiled a set of macros to support the expansion of \rightleftharpoons in XeTeX. At the risk of being the laughingstock of tex.SE, I'd like to share them:

\input something_that_sets_up_fonts_and_mathchars_etc_with_say_XITS_Math
  \setbox2=\hbox{\rlMedharpoons}  \setbox3=\hbox{\rllongharpoons}
  \setbox5=\hbox{$#1$} \setbox6=\hbox{$#2$}
  \ifdim\wd5<\wd6 \dimen7=\wd6 \else\dimen7=\wd5 \fi
  \ifdim\dimen7<\wd0 \buildharprel{\box0}{#1}{#2}
    \else\ifdim\dimen7<\wd1 \buildharprel{\box1}{#1}{#2}
      \else\ifdim\dimen7<\wd2 \buildharprel{\box2}{#1}{#2}
        \else\ifdim\dimen7<\wd3 \buildharprel{\box3}{#1}{#2}
  x \mathop{\longleftarrow}\limits^R {\cal Z}
  \harpoons{\alpha=g^x}{\beta = g^y} y
  \harpoons{\alpha}{\beta} q
  \mathop{\longleftarrow}\limits^R {\cal Z_q}

Which could look like (I modified the formula just to show the expansion, quite probably making it nonsense in the process):

I realize the \harpoons-macro sure could use some optimization, but there you go.

share|improve this answer
Thanks a lot. much easier :D. – Yasser Sobhdel Dec 2 '10 at 14:33

Here's my attempt, using TikZ, as requested:


      \node (A) at (0,0) {$x$};
      \node (B) at (1,-1) {$g^{xy}$};
      \node (C) at (2,0) {$Z_{q}$};
      \node (D) at (4,0) {$y$};
      \node (E) at (5,-1) {$g^{xy}$};
      \node (F) at (6,0) {$Z_{q}$};

  \begin{scope}[every node/.style={font= \scriptsize}]

     \draw [latex-] (A) -- node[above]{$R$}  (C);
     \draw[transform canvas={yshift=0.2ex},-left to] (C) -- node[above]{$\alpha=g^{x}$} (D);
     \draw[transform canvas={yshift=-0.2ex},left to-] (C) -- node[below]{$\beta=g^{y}$} (D);
     \draw[latex-] (D) -- node[above]{$R$}  (F);



which produces: enter image description here

The trick is the transform canvascommand to obtain a rightleftharpoon equivalent. The spacing can be tinkered with to one's heart's desire by changing the \node settings.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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