自动缩放伪代码块以适合页面

自动缩放伪代码块以适合页面

我有一个环境(pseudocodeblock来自cryptocode包裹) 如果内容太大,则会进入边距:

在此处输入图片描述

现在,我可以使用\scalebox和手动调整它的minipage大小,但我需要手动调整设置,因为我不知道的宽度pseudocodeblock(而且我不知道如何计算它)。

是否可以测量这个块的宽度,并在其太大而无法容纳在页面中时自动缩放它?

平均能量损失

\documentclass[]{article}
\usepackage [
n,
lambda,
advantage,
operators,
sets,
adversary,
landau,
probability,
notions,
logic,
ff,
mm,
primitives,
events,
complexity,
asymptotics,
keys
] {cryptocode}

\begin{document}

\noindent This block should be scaled to fit on page (ideally, if it's short enough, it is not scaled up, of its too big, it's scaled down), for now it is going in the margin:

\noindent\rule{\linewidth}{1mm}

\pseudocodeblock{
  \textbf{Alice} \< \< \textbf{Bob} \\[][\hline]
  \\[-.5\baselineskip]
  \text{Run Alice will do some stuff that are hard to write shortly.} \< \sendmessage{<->}{} \< \text{Same for Bob}\\
}

\end{document}

答案1

varwidth好的,我发现(提供最大宽度的上限)的组合\maxsizebox正好提供了我想要的内容: 在此处输入图片描述

\documentclass[]{article}
\usepackage [
n,
lambda,
advantage,
operators,
sets,
adversary,
landau,
probability,
notions,
logic,
ff,
mm,
primitives,
events,
complexity,
asymptotics,
keys
] {cryptocode}
\usepackage{adjustbox}
\begin{document}

\noindent\rule{\linewidth}{1mm}

\noindent Content already short enough is not changed:

\noindent\maxsizebox{\linewidth}{!}{
  \begin{varwidth}{10\linewidth}% Upper bound on the width of the text
    \pseudocodeblock{
      \textbf{Alice} \< \< \textbf{Bob} \\[][\hline]
      \\[-.5\baselineskip]
      \text{Run Alice.} \< \sendmessage{<->}{} \< \text{Run Bob}\\
    }
  \end{varwidth}
}

\noindent Too large content is shrinked


\noindent\maxsizebox{\linewidth}{!}{
  \begin{varwidth}{10\linewidth}% Upper bound on the width of the text
    \pseudocodeblock{
      \textbf{Alice} \< \< \textbf{Bob} \\[][\hline]
      \\[-.5\baselineskip]
      \text{Run Alice will do some stuff that are hard to write shortly.} \< \sendmessage{<->}{} \< \text{Same for Bob}\\
    }
  \end{varwidth}
}

\end{document}

您还可以创建一个环境来自动执行此操作:

\NewDocumentEnvironment{autoFit}{O{\linewidth}}{\noindent\maxsizebox{\linewidth}{!}\bgroup%
    \begin{varwidth}{10\linewidth}% Upper bound on the width of the text
    }{\end{varwidth}\egroup}

相关内容