1

I needed two algorithms where client-server interact. I wrote a figure with two minipages each containing an algorithm.

\begin{figure}[!htb]
\begin{minipage}[t]{.48\linewidth}
    \algrenewcommand\alglinenumber[1]{\AlgLyn{s}{algo:FigName}}
    \begin{algorithm}[H]
    \end{algorithm}
\end{minipage}
\begin{minipage}[t]{.48\linewidth}
    \algrenewcommand\alglinenumber[1]{\AlgLyn{c}{algo:FigName}}
    \begin{algorithm}[H]
    \end{algorithm}
\end{minipage}
\label{algo:FigName}
\end{figure}

I need lines of both the algorithms are numbered. Precisely, the client algorithm is numbered as (c101,c102,..., c110,c111) and the server algorithm is numbered as (s101,s102,..., s120,s121) where c-1-01 stands for client, this is 1st figure, 1th line etc. To identify each line such a way, I have used fmtcount package that allows zero-filling a string. I defined two macros:

\newcommand{\AlgLyn}[2]{#1\ref{#2}\padzeroes[\zfill]{\decimal{ALG@line}}:}
\newcommand{\AlgLyne}[3]{#1\ref{#2}\padzeroes[\zfill]{\decimalnum{\ref{#3}}}}

The first-one prints line numbers in the description of the algorithms as required. The second one is used to refer to a particular line of the algorithm. For example, one can refer to the 5th line of server code by calling \AlgLyne{s}{algo:FigName}{line:5} if one already has labeled the 5th line by \label{line:5}. It runs quite well even with cleveref included.

Now I face a problem when I try to include the package hyperref. The problem I am facing is two-fold:

  1. Notice the s-1-05 (output due to \AlgLyn{s}{algo:FigName}), the 1 is coming due to \ref{#2} in \AlgLyn. Now, is trying to add hyperlink to all the 1s i.e. all (s101, s102, s103,...) printed in the algorithm description, are dereferenced and a hyperlink is added to the algo:FigName. I don't want that. I want the way it was behaving previously. Like, in the description of the algorithm, the line numbers will be printed (s101, s102, s103,...) but without any hyperlink to algo:FigName.
  2. The \AlgLyne{s}{algo:FigName}{line:5} fails with a message "Missing number, treated as zero". I don't know exactly why it is failing?

In case, these are intrinsic limitations of the way I have defined the two macros above, please suggest some other way so that I can get the following:

  1. server-client interactive algortihm with line number in (c101,..., c111),(s101,..., s121) the algorithm description.
  2. No compatibility issue with cleveref.
  3. No compatibility issue with hyperef. That too only when I define a label, \AlgLyne will show the line number and hyperef will put a hyperlink to that particular line.

I am attaching two pics of the current situation.

  1. This is due to \AlgLyn{c}{algo:FigName-3} and \AlgLyn{s}{algo:FigName-3} printing line number to each line of the algorithm. Pic-1
  2. This is due to \AlgLyne{s}{algo:FigName-3}{line:3} referring to a particular line of the algorithm.Pic-2

Note: As per egreg's request, I am posting an example code.

\documentclass[12pt]{article}
\usepackage{mathtools}
\usepackage{fmtcount}
\usepackage{algpseudocode
\usepackage[lined,linesnumbered,commentsnumbered]{algorithm}
%\usepackage[hypertexnames=false]{hyperref}
\usepackage{cleveref}
\floatname{algorithm}{Algorithm}
\def\zfill{2}
\newcommand{\AlgLyn}[2]{#1\ref{#2}\padzeroes[\zfill]{\decimal{ALG@line}}:}
\newcommand{\AlgLyne}[3]{#1\ref{#2}\padzeroes[\zfill]{\decimalnum{\ref{#3}}}}
\newcommand{\rarrow}[2]{\xrightarrow{\makebox[#1]{$#2$}}}
\begin{document}
\begin{figure}[!htb]
\begin{minipage}[t]{.5\linewidth}
     \algrenewcommand\alglinenumber[1]{\AlgLyn{c}{algo:FigName}}
     \begin{algorithm}[H]
        \caption*{Client}
        \begin{scriptsize}
             \begin{algorithmic}[1]
                   \State{$K_i\leftarrow F(K, z_1||i)$ for $i\in[0,2]$; ${\sf Msg}\leftarrow \bot$}
                   \Statex{\hspace*{0.64\linewidth}$\rarrow{0.5\textwidth}{\Phi}$}%
            \For{$c=1,2,\ldots$} \label{cli-cstart}
                \For{$\alpha\in[2,n]$}
                    \State{$X[c,\alpha]\leftarrow g^{F_p(K_Z,z_1||c)F_p(K_X,z_\alpha)}$}
                \EndFor
           \EndFor   \label{cli-cend}
    \end{algorithmic}
        \end{scriptsize}
\end{algorithm}
\end{minipage}
\begin{minipage}[t]{.5\linewidth}
\algrenewcommand\alglinenumber[1]{\AlgLyn{s}{algo:FigName}}
\begin{algorithm}[H]
        \caption*{Server}
        \begin{scriptsize}
    \begin{algorithmic}[1]
        \State{$Answer\_Set\leftarrow \phi$}
        \Statex\Statex
        \For{$c=1,2,\ldots$} \label{srv-cstart}
            \State{$List[c]\leftarrow c$}
        \EndFor         \label{srv-cend}
    \end{algorithmic}
        \end{scriptsize}
\end{algorithm}
\end{minipage}
\caption{Client Server Conversion}
\label{algo:FigName}
\end{figure}
\AlgLyne{c}{algo:FigName}{cli-cstart}

\AlgLyne{s}{algo:FigName}{srv-cend}
\end{document}

`

\AlgLyne{c}{algo:FigName}{cli-cstart} produces c102 and \AlgLyne{s}{algo:FigName}{srv-cend} produces s104. I need [c102] or [s304] hyperlink. If we uncomment hyperref,

  1. Get error "Missing number, treated as zero"
  2. In place of hyperlink to c102 or s304 line, we sometimes get hyperlink at c[1]0[2] and s[1]0[4].
  3. All the lines of above the algorithms sometimes get hyperlinked line number looks like c[1]01, c[1]02, c[1]03, and so on.
2
  • 2
    Please, can you make the code snippet into a full compilable example to play with?
    – egreg
    Jun 25 '18 at 9:25
  • Hey @egreg, I have added the code snippet. Thanks.
    – mxant
    Jun 25 '18 at 13:46
2

You can exploit refcount and the arithmetic capabilities of TeX with \numexpr. In order to get unique identifiers, also \theHALG@line should be defined.

\documentclass[12pt]{article}
\usepackage{mathtools}
\usepackage{refcount}
\usepackage{algpseudocode}
\usepackage[lined,linesnumbered,commentsnumbered]{algorithm}
\usepackage{hyperref}
\usepackage{cleveref}
\floatname{algorithm}{Algorithm}

\newcommand{\AlgLyn}[2]{%
  #1\the\numexpr\getrefnumber{#2}*100+\arabic{ALG@line}\relax
}
\newcommand{\AlgLyne}[3]{%
  #1\the\numexpr\getrefnumber{#2}*100+\getrefnumber{#3}\relax
}
\newcommand{\AlgoRef}[3]{%
  \hyperref[#3]{#1\the\numexpr\getrefnumber{#2}*100+\getrefnumber{#3}\relax}%
}

\newcommand{\rarrow}[2]{\xrightarrow{\makebox[#1]{$#2$}}}

\begin{document}
\begin{figure}[!htb]
\begin{minipage}[t]{.5\linewidth}
\algrenewcommand\alglinenumber[1]{\AlgLyn{c}{algo:FigName}}
\makeatletter\newcommand\theHALG@line{c\arabic{ALG@line}}\makeatother
\begin{algorithm}[H]
\caption*{Client}
\scriptsize
\begin{algorithmic}[1]
  \State{$K_i\leftarrow F(K, z_1||i)$ for $i\in[0,2]$; $\textsf{Msg}\leftarrow \bot$}
  \Statex{\hspace*{0.64\linewidth}$\rarrow{0.5\textwidth}{\Phi}$}%
  \For{$c=1,2,\ldots$} \label{cli-cstart}
    \For{$\alpha\in[2,n]$}
      \State{$X[c,\alpha]\leftarrow g^{F_p(K_Z,z_1||c)F_p(K_X,z_\alpha)}$}
    \EndFor
 \EndFor   \label{cli-cend}
\end{algorithmic}
\end{algorithm}
\end{minipage}
\begin{minipage}[t]{.5\linewidth}
\algrenewcommand\alglinenumber[1]{\AlgLyn{s}{algo:FigName}}
\makeatletter\newcommand\theHALG@line{s\arabic{ALG@line}}\makeatother
\begin{algorithm}[H]
\caption*{Server}
\scriptsize
\begin{algorithmic}[1]
  \State{$\mathit{Answer\_Set}\leftarrow \phi$}
  \Statex\Statex
  \For{$c=1,2,\ldots$} \label{srv-cstart}
    \State{$\mathit{List}[c]\leftarrow c$}
  \EndFor         \label{srv-cend}
\end{algorithmic}
\end{algorithm}
\end{minipage}
\caption{Client Server Conversion}
\label{algo:FigName}
\end{figure}

\AlgoRef{c}{algo:FigName}{cli-cstart}

\AlgoRef{s}{algo:FigName}{srv-cend}

\end{document}

enter image description here

1
  • Thanks a lot @egreg. I know almost nothing about the usage of numexpr. Can you please share some document/link regarding this with ample example?
    – mxant
    Jun 26 '18 at 4:58

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.