我用来\cryptocode
显示如下代码:
默认情况下,它似乎不是浮动的。但是,当游戏很大时,我更愿意让它们浮动。此外,为了引用它们,我只想输入\cref{mygame}
并获取如下链接:
目前,即使使用锚点并手动复制代码名称的手动方法也不起作用:链接未指向游戏。我不知道如何自动要求 cref 添加游戏名称。
平均能量损失
\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}
答案1
对于浮动游戏,定义一个新的浮动类型。
为了合作,必须做出cleveref
一些调整。cryptocode
\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}
答案2
由于我没有找到明显的解决方案,所以我实施了自己的解决方案。如果有更好的解决方案,请告诉我:
\cref
不幸的是,它没有与(如果您知道如何操作,请告诉我)链接,而是与自定义\refGame{myGreatGame}
函数链接(确保不要在定理的标签内使用数字……我不确定如何使它与数字一起工作,如果您知道,请告诉我)。以下是定义新游戏的语法(删除星星以获得非浮动环境):
\begin{game}*[myGreatGame][short title]{long title}
game
\end{game}
完整示例:
\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}
编辑
我LaTeX:使浮动出现在“列表”中而不显示任何标签一个解决方案,也可以创建像目录一样的游戏列表。