5

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.

Hendrik Vogt
  • 37,935
  • 1
    @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 02 '10 at 09: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 02 '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 02 '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 02 '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 02 '10 at 10:38
  • @Matthew: I was just thinking about inserting the raw url http://i.imgur.com/jYfNE.png, rather than embedding it as an image (which is the better option if one has the rep, of course). At any rate, thanks :-) – Antal Spector-Zabusky Dec 02 '10 at 10:48
  • @Mathew: Sorry for late response and thank you @Antal: Thank You too. – Yasser Sobhdel Dec 02 '10 at 12:28
  • @Antal: now I understand. New users can add links to any URL, and you were suggesting an imgur URL over the ifile one. I took it one step further by embedding it. – Matthew Leingang Dec 02 '10 at 13:26

3 Answers3

11

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):

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
  \[\begin{matrix}
      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} \\
  \end{matrix}\]
\end{document}

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.

Werner
  • 603,163
5

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

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
\def\buildharpoons#1#2{$\vcenter{\hbox{\ooalign{%
  \lower1.25\fontdimen5\textfont1\hbox{$#1$}\crcr
  $#2$\crcr}}}$}
\def\rlshortharpoons{\buildharpoons{%
  \XeTeXmathchar"3"1"E049}{\XeTeXmathchar"3"1"E04D}}
\def\rlmedharpoons{\buildharpoons{%
  \XeTeXmathchar"3"1"E092}{\XeTeXmathchar"3"1"E096}}
\def\rlMedharpoons{\buildharpoons{%
  \XeTeXmathchar"3"1"E0C7}{\XeTeXmathchar"3"1"E0CB}}
\def\rllongharpoons{\buildharpoons{%
  \XeTeXmathchar"3"1"E0FB}{\XeTeXmathchar"3"1"E0FF}}
\def\rlLongharpoons{\buildharpoons{%
  \XeTeXmathchar"3"1"E126}{\XeTeXmathchar"3"1"E12A}}
\def\buildharprel#1#2#3{\mathrel{\mathop{#1}\limits^{#2}_{#3}}}
\def\harpoons#1#2{%
  \setbox0=\hbox{\rlshortharpoons}\setbox1=\hbox{\rlmedharpoons}
  \setbox2=\hbox{\rlMedharpoons}  \setbox3=\hbox{\rllongharpoons}
  \setbox4=\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}
          \else\buildharprel{\box4}{#1}{#2}\fi\fi\fi\fi}
$$
  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}
$$
\bye

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.

doncherry
  • 54,637
morbusg
  • 25,490
  • 4
  • 81
  • 162
4

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

\begin{tikzpicture}

      \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);

   \end{scope}

 \end{tikzpicture}

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.

scitexter
  • 819