![tufte-latex:全宽 algorithm2e 块](https://linux22.com/image/417665/tufte-latex%EF%BC%9A%E5%85%A8%E5%AE%BD%20algorithm2e%20%E5%9D%97.png)
我正在使用\documentclass{tufte-book}
并尝试获取全宽algorithm
环境(具体来说,使用algorithm2e
)。
最小工作示例:
\documentclass{tufte-book}
\title{Full-width algorithm2e algorithms}
\usepackage{lipsum} % for dummy text only
\usepackage[lined, ruled]{algorithm2e}
\begin{document}
\lipsum[2]
\begin{algorithm}[H]
\tcp{Main loop}
\While{not converged}{
$a = a + 1$ \;
}
\caption{A text-width algorithm}
\end{algorithm}
\lipsum[2]
\marginnote[10pt]{This algorithm should be full-width, but isn't}
\begin{fullwidth}
\begin{algorithm}[H]
\tcp{Main loop}
\While{not converged}{
$a = a + 1$ \;
}
\caption{A full-width algorithm}
\end{algorithm}
\end{fullwidth}
\lipsum[2]
\end{document}
其渲染效果为:
在里面algorithm2e.sty
源代码,我发现该尺寸algowidth
在文档中大约一半的位置定义。
%[snip]
\newdimen\algowidth%
%[snip]
因此我尝试\setlength{\algowidth}{XXX}
在我的示例中调整这个长度,但似乎没有任何效果。
- 如何使用实现全宽 algorithm2e
algorithm
块tufte-latex
? algorithm*
如果可以默认调整为全宽,则可获得加分。tufte-latex
如果算法标题也可以移动到边缘处悬挂,就像全宽图形和表格的样式一样,则可以获得额外的加分。
可能相关的问题似乎无法回答这个问题:
答案1
这可能不是最理想的解决方案,因为您将它algorithm
封闭在中而失去了它的浮动行为minipage
,但是您已经通过添加来停用浮动机制[H]
......
\documentclass{tufte-book}
\title{Full-width algorithm2e algorithms}
\usepackage{lipsum} % for dummy text only
\usepackage[lined, ruled]{algorithm2e}
\newenvironment{widealgorithm}{\begin{fullwidth}
\begin{minipage}{\linewidth}
\begin{algorithm}[H]}{\end{algorithm}
\end{minipage}
\end{fullwidth}}
\begin{document}
\lipsum[2]
\begin{algorithm}[H]
\tcp{Main loop}
\While{not converged}{
$a = a + 1$ \;
}
\caption{A text-width algorithm}
\end{algorithm}
\lipsum[2]
\begin{fullwidth}
\begin{minipage}{\linewidth}
\begin{algorithm}[H]
\tcp{Main loop}
\While{not converged}{
$a = a + 1$ \;
}
\caption{A full-width algorithm}
\end{algorithm}
\end{minipage}
\end{fullwidth}
\lipsum[2]
\begin{widealgorithm}
\tcp{Main loop}
\While{not converged}{
$a = a + 1$ \;
}
\caption{A full-width algorithm}
\end{widealgorithm}
\end{document}
答案2
为了解决浮动算法的这个问题(我有一个也是整个页面的算法),你可以定义一个新的浮动并将小页面放入该浮动中。这是这个答案如果我理解正确的话,这应该需要float
tufte-latex 为您加载的包。
% Reusing the preamble of leandriis' excellent answer
\documentclass{tufte-book}
\title{Floaty Full-width algorithm2e algorithms}
\usepackage{lipsum} % for dummy text only
\usepackage[lined, ruled]{algorithm2e}
% Define a new float with name myalgo
% Default placement tbp and a file extension mya (for temp files)
% Cf. https://ctan.mc1.root.project-creative.net/macros/latex/contrib/float/float.pdf
\newfloat{myalgo}{tbp}{mya}
% create a new environment called floatyalgorithm with one parameter (our algorithm code)
% Using this creates a new float through the myalgo float we defined above,
% which is filled with a minipage of full page width.
% The width is defined as the sum of a line, the margin and the sapce in between (since the fullwidth environment cannot help us here).
% For the variables names cf. https://www.overleaf.com/learn/latex/page_size_and_margins
% As in leandriis' answer, use [H] for the algorithm to fill the space.
\newenvironment{floatyalgorithm}[1][tbp]%
{\begin{myalgo}[#1]
\begin{minipage}{\linewidth+\marginparsep+\marginparwidth}
\begin{algorithm}[H]}%
{\end{algorithm}
\end{minipage}
\end{myalgo}
}
\begin{document}
\begin{floatyalgorithm}[tbp]
$m := m'$;
\caption{\lipsum[2]}
\end{floatyalgorithm}
\end{document}
我还没有对此进行过广泛的测试。对于我的用例来说,它运行良好,但可能会与整个页面和边距内容产生一些不必要的交互。