我有以下一段 Latex 代码。我希望文本位于该框内,但它却在框外。有人可以帮忙吗?
\documentclass[]{article}
\usepackage{varwidth}
\usepackage{refcount}
\usepackage[colorlinks]{hyperref}
\makeatletter
\setlength{\@fptop}{0pt}
\makeatother
\usepackage [
n,
advantage,
operators,
sets,
adversary,
landau,
probability,
notions,
logic,
ff,
mm,
primitives,
events,
complexity,
asymptotics,
keys
] {cryptocode}
\usepackage{cryptocode}
\usepackage{changepage}
\begin{figure}
\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
\procedure[linenumbering]{${\bf Exp}_{{\sf IB-MSS}}^{uf-cma}(\adv)$}{%
({\sf pp},MSK) \gets {\sf Setup}(\eta); {\sf MIdLst}\gets \emptyset ; {\sf CIdLst} \gets \emptyset \\
\text{Run } \adv({\sf pp}), \text{and handle } \adv \text{’s key derivation and signature queries as follows:} \\
\text{On a key derivation query on identity ${\sf Id}$, add ${\sf Id}$ to ${\sf CIdLst}$, run ${\sf KeyGen}$ on input $(MSK, {\sf Id})$ and return ${\sf sk_{Id}}$ to $\adv$}\\
\text{On a signing query on pair $({\sf msg, Id})$, add ${\sf (msg, Id)}$ to ${\sf MIdLst}$}\\
\text{run ${\sf Sign}$ protocol on behalf of identity ${\sf Id}$ on message ${\sf msg}$ forwarding messages to and from $\adv$.}\\
\text{When $\adv$ halts, parse its output as ${\sf (msg}, IdSet, \sigma)$} \\
\text{If ${\sf Verify}({\sf pp, msg},IdSet, \sigma )=1 \wedge (\exists;\ {\sf Id}\in IdSet$ such that $({\sf Id}\notin {\sf CIdLst}) \wedge (({\sf msg, Id})\notin {\sf MIdLst})) $ then return $1$, otherwise return $0$.}
}\end{minipage}}
\end{figure}
答案1
问题在于数学模式不会自动添加换行符。在数学模式下使用加密代码时,一种选择是使用手动换行符,可能与\pcskipln
以下方法一起使用
\procedure[linenumbering]{Header}{%
\text{first line} \\
\text{some long} \pcskipln\\
\text{line} \\
\text{some long} \pcskipln\\
\text{line}
}
这会产生类似下列的结果。
由于您的情况与文本相比,数学部分实际上很小,因此我建议在文本模式下使用加密代码,mode=text
并使用选项width
。以下是调整为 8cm 宽度的示例。
\documentclass[]{article}
\usepackage{varwidth}
\usepackage{refcount}
\usepackage[colorlinks]{hyperref}
\makeatletter
\setlength{\@fptop}{0pt}
\makeatother
\usepackage [
n,
advantage,
operators,
sets,
adversary,
landau,
probability,
notions,
logic,
ff,
mm,
primitives,
events,
complexity,
asymptotics,
keys
] {cryptocode}
\usepackage{cryptocode}
\usepackage{changepage}
\begin{document}
\begin{figure}
\begin{pchstack}[center]
\fbox{\procedure[linenumbering,width=8cm,mode=text]{${\bf Exp}_{{\sf IB-MSS}}^{uf-cma}(\adv)$}{%
$({\sf pp},MSK) \gets {\sf Setup}(\eta); {\sf MIdLst}\gets \emptyset ; {\sf CIdLst} \gets \emptyset$ \\
Run $\adv({\sf pp})$, and handle $\adv$’s key derivation and signature queries as follows: \\
On a key derivation query on identity ${\sf Id}$, add ${\sf Id}$ to ${\sf CIdLst}$, run ${\sf KeyGen}$ on input $(MSK, {\sf Id})$ and return ${\sf sk_{Id}}$ to $\adv$\\
On a signing query on pair $({\sf msg, Id})$, add ${\sf (msg, Id)}$ to ${\sf MIdLst}$\\
run ${\sf Sign}$ protocol on behalf of identity ${\sf Id}$ on message ${\sf msg}$ forwarding messages to and from $\adv$.\\
When $\adv$ halts, parse its output as ${\sf (msg}, IdSet, \sigma)$ \\
If ${\sf Verify}({\sf pp, msg},IdSet, \sigma )=1 \wedge (\exists;\ {\sf Id}\in IdSet$ such that $({\sf Id}\notin {\sf CIdLst}) \wedge (({\sf msg, Id})\notin {\sf MIdLst})) $ then return $1$, otherwise return $0$.
}}
\end{pchstack}
\end{figure}
\end{document}
得出的结果为:
附注:您可能需要查看\createprocedureblock
为居中和框式伪代码部分创建便捷命令。