2

I'm using \cryptocode to display codes like:

enter image description here

By default, it seems that it's not floating. However, when the games are big I'd prefer to make them float. Moreover, to refer to them, I'd like to just type \cref{mygame} and get a link like:

enter image description here

For now, even the manual method using anchors and manually copying the name of the code does not work: the link does not point to the game. And I've no idea how I could automatically ask cref to add the name of the game.

MWE

\documentclass[]{article}

\usepackage{amsmath}
\usepackage [
n,
advantage,
operators,
sets,
adversary,
landau,
probability,
notions,
logic,
ff,
mm,
primitives,
events,
complexity,
asymptotics,
keys
] {cryptocode}
\createprocedureblock{game}{center,boxed}{}{}{}
\usepackage{lipsum}

\usepackage{float}
\floatstyle{ruled} % Defines the "rule" style for next environments defined using newfloat.
\newfloat{protocol}{htb!}{Protocol}

\usepackage{hyperref}
\usepackage{cleveref}
\crefname{protocol}{Protocol}{Protocols}
\Crefname{protocol}{Protocol}{Protocols}

\begin{document}
\lipsum[1-4]\\

As you can see if there is not eough space, the game is pushed on the next page leaving a large white space:
\begin{pcimage}
  {\normalfont\game[linenumbering]{$\indcpa_\enc^\adv(\secpar)$}{\phantomsection\label{mygame}
      b \sample \bin  \\
      (\pk,\sk) \sample \kgen (\secparam)  \\
      (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
      c \sample \enc(\pk,m_b)  \\
      b' \sample \adv(\secparam, \pk, c, \state) \\
      \pcreturn b = b'
    }}
\end{pcimage}

Moreover, I cannot really refer to it: \cref{mygame}.

\section{What I would like to do}

Sometimes (for big games mostly), I would prefer to be able to put it inside a float, and I'd like to be able to refer to them using cref, like I can do for protocols: \cref{protocol:myprotocol} (this should be feasible for all games). More precisely, what I'd like is that when I type \verb|\cref{mygame}| I should get something like \hyperlink{mygame}{$\indcpa_\enc^\adv(\secpar)$}, but I don't know why the link is broken.  \lipsum[1]

Note that there is no white space anymore after this line.

\begin{protocol}
  \caption{My protocol\label{protocol:myprotocol}}
  \lipsum[1-2]
\end{protocol}

\lipsum[1]
\end{document}

2 Answers 2

3

For floating games, define a new float type.

For cooperation with cleveref a couple of adjustments to cryptocode have to be made.

\documentclass[]{article}

\usepackage{amsmath}
\usepackage[
n,
advantage,
operators,
sets,
adversary,
landau,
probability,
notions,
logic,
ff,
mm,
primitives,
events,
complexity,
asymptotics,
keys
] {cryptocode}
\createprocedureblock{game}{center,boxed}{}{}{}

\usepackage{lipsum}

\usepackage{float}

\usepackage{hyperref}
\usepackage{cleveref}

\floatstyle{ruled} % Defines the "rule" style for next environments defined using newfloat.
\newfloat{protocol}{htb!}{Protocol}
\newfloat{printgame}{htb!}{Game}
\floatname{printgame}{Game}

\crefname{protocol}{Protocol}{Protocols}
\Crefname{protocol}{Protocol}{Protocols}

\newcounter{dummygame} % a counter for setting references to games
\crefname{dummygame}{Game}{Games} % its cleveref name

\makeatletter
% redefine \game so that the first mandatory argument can be used for cross-reference
% save the original meaning of \game
\let\savedgame\game
% redefine it so at the start of the body the counter is stepped
\RenewDocumentCommand{\game}{omm}{%
  \IfNoValueTF{#1}
    {\savedgame{#2}{\game@addlabel{#2}#3}}
    {\savedgame[#1]{#2}{\game@addlabel{#2}#3}}%
}
% an auxiliary command for avoiding code duplication
\newcommand{\game@addlabel}[1]{%
  % the current label will be the argument
  \def\thedummygame{\unexpanded{\unexpanded{#1}}}%
  % step the counter
  \refstepcounter{dummygame}%
}
% fix cryptocode that doesn't really cooperate with hyperref
\AtBeginDocument{\let\@pc@original@label\label}
\makeatother

\begin{document}

\lipsum[1-4]

\begin{printgame}
\begin{pcimage}
  \game[linenumbering]{$\indcpa_\enc^\adv(\secpar)$}{\label{mygame}
      b \sample \bin  \\
      (\pk,\sk) \sample \kgen (\secparam)  \\
      (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
      c \sample \enc(\pk,m_b)  \\
      b' \sample \adv(\secparam, \pk, c, \state) \\
      \pcreturn b = b'
  }
\end{pcimage}
\caption{A game}
\end{printgame}

Moreover, I can refer to it: \ref{mygame} or \cref{mygame}


\section{What I would like to do}

Sometimes (for big games mostly), I would prefer to be able to put it 
inside a float, and I'd like to be able to refer to them using cref, 
like I can do for protocols: \cref{protocol:myprotocol} (this should 
be feasible for all games). More precisely, what I'd like is that when 

Note that there is no white space anymore after this line.

\begin{protocol}
  \caption{My protocol\label{protocol:myprotocol}}
  \lipsum[1-2]
\end{protocol}

\lipsum[1]

\end{document}

enter image description here

3
  • Oh nice way to label things with cleveref! I'm not a big fan of how games are wrapped into a ruled float since they are already boxed with a title, but I guess I can put them inside empty figures if I prefer like I already did in my other answer. Thanks!
    – tobiasBora
    Commented Nov 20, 2021 at 17:12
  • 1
    @tobiasBora Or move the \newfloat declaration before \floatstyle{ruled}.
    – egreg
    Commented Nov 20, 2021 at 20:58
  • I think \AtBeginDocument{\let\@pc@original@label\label} can be replaced by \pcfixcleveref (or \pcfixhyperref if you use only hyperref but not cleveref) in cryptocode >= 0.43, see github.com/arnomi/cryptocode/issues/5. Commented Dec 23, 2021 at 10:37
0

As I found no obvious solution, I implemented mine. Let me know if there is a better solution:

enter image description here

It's unfortunately not linked with \cref (let me know if you know how to do), but with a custom function \refGame{myGreatGame} (make sure not to use numbers inside the label of the theorem... I'm not sure how to make it work with numbers, let me know if you know). Here is the syntax to define a new game (remove the star to obtain a non-floating environment):

\begin{game}*[myGreatGame][short title]{long title}
  game
\end{game}

Full example:

\documentclass[]{article}

\usepackage{amsmath}
\usepackage [
n,
advantage,
operators,
sets,
adversary,
landau,
probability,
notions,
logic,
ff,
mm,
primitives,
events,
complexity,
asymptotics,
keys
] {cryptocode}
\createprocedureblock{gameProc}{center,boxed}{}{}{}
\usepackage{lipsum}

\makeatletter
% Usage: \begin{mygame}[label][short title]{title} content \end{mygame}
\NewDocumentEnvironment{game}{soomb}{%
  \IfBooleanTF{#1}{\begin{figure}[htbp]}{}%
  \begin{pcimage}%
    {\normalfont\gameProc[linenumbering]{#4}{\IfValueTF{#2}{%
          %% Add an anchor if a label is present
          \raisebox{1em}{\hypertarget{#2}{}}%
          %% Create a macro "mygametitle@nameoflabel" to store the title
          \IfValueTF{#3}{% If a short title is provided
            \expandafter\gdef\csname mygametitle@#2 \endcsname{#3}%%
            \write\@auxout{\gdef\string\mygametitle@#2{#3}}%
          }{%
            \expandafter\gdef\csname mygametitle@#2 \endcsname{#4}%%
            \write\@auxout{\gdef\string\mygametitle@#2{#4}}%
          }%
        }{} #5 }}%
  \end{pcimage}
  \IfBooleanTF{#1}{\end{figure}}{}%
}{}

% Usage: \refGame{label}
\NewDocumentCommand{\refGame}{m}{%
  \hyperlink{#1}{\csname mygametitle@#1\endcsname}% Do not put a white space after #1!
}

\makeatother

%\definecolor{secondaryColor}{RGB}{71,152,255} %% blue
%\definecolor{secondaryColor}{RGB}{255,177,45}  %% yellow
\definecolor{secondaryColor}{RGB}{206,149,0} %% darker orange, looks like gold. <3
%\definecolor{secondaryColor}{RGB}{255,169,45}  %% yellow
\usepackage[
  colorlinks,
  allcolors=secondaryColor % https://tex.stackexchange.com/a/50754/
]{hyperref}

%\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}

I provide a new environment \verb|game|, where you can specify an optional star (to insert it into a floating figure), an optional label (\textbf{WARNING: do not use any number in the label}) if you want to refer to it later, an optional short title (used when citing the game), the real title, and finally the content.
\begin{game}*[myGreatGame][$\indcpa$]{$\indcpa_\enc^\adv(\secpar)$}
  b \sample \bin  \\
  (\pk,\sk) \sample \kgen (\secparam)  \\
  (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c, \state) \\
  \pcreturn b = b'
\end{game}

You can then refer to the game using \verb|refGame|, like \refGame{myGreatGame}. Note that if the reference is before the definition of the game, you will need to compile your document twice. Here is in \refGame{myGreatGameOne} a version without a floating environment and without a short title:
\begin{game}[myGreatGameOne]{$\indcpa 1_\enc^\adv(\secpar)$}
  b \sample \bin  \\
  (\pk,\sk) \sample \kgen (\secparam)  \\
  (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c, \state) \\
  \pcreturn b = b'
\end{game}

\lipsum[1]\\
Here is in \refGame{myGreatGamea} an example at the end of a page (without any specified short title, so the main title is taken).

\begin{game}*[myGreatGamea]{$\indcpa 2_\enc^\adv(\secpar)$}
  b \sample \bin  \\
  (\pk,\sk) \sample \kgen (\secparam)  \\
  (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c, \state) \\
  \pcreturn b = b'
\end{game}

\lipsum[1]

\end{document}

EDIT

I presented in LaTeX: make float appear in "list of" without displaying any label a solution to also create a list of games like a table of contents.

You must log in to answer this question.

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