如何使算法环境比 \linewidth 或 \textwidth 更宽?

如何使算法环境比 \linewidth 或 \textwidth 更宽?

我已经尝试了两件事:\makebox[1.2\textwidth][c]{...}但没有用(perhaps missing \item)。\redefinegeometry在这种情况下效果不佳,因为它不在单个页面上。MWE:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

来源:https://www.writelatex.com/examples/euclids-algorithm-an-example-of-how-to-write-algorithms-in-latex/mbysznrmktqf

答案1

您可以使其algorithm不可浮动,并将其与另一个可适应宽度的浮动环境装箱。

下面,我们tcolorbox为此目的使用了不可见元素。调整 grow to left bygrow to right by选项以适应所需的宽度。此外,浮动设置由选项完成float

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{lipsum}

\usepackage[skins]{tcolorbox}

\begin{document}

\lipsum[1]

\begin{tcolorbox}[blanker,float=tbp,
  grow to left by=1cm,grow to right by=1cm]
\begin{algorithm}[H]
\caption{Euclid's algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{tcolorbox}

\lipsum[1]

\end{document}

在此处输入图片描述

如果有许多算法框需要适配,则可以使用以下talgorithm构建环境的变体。它将对称增长值作为强制参数,并将任何tcolorbox选项作为可选参数:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{lipsum}

\usepackage[skins]{tcolorbox}

\newtcolorbox{talgorithm}[2][]{%
  blanker,float=tbp,grow to left by=#2,grow to right by=#2,
  before upper={\begin{algorithm}[H]},
  after upper={\end{algorithm}},
  #1
}

\begin{document}

\lipsum[1]

\begin{talgorithm}{1cm}
\caption{Euclid's algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{talgorithm}

\lipsum[1]

\end{document}

答案2

问题是,这algorithm是一个浮动环境,无法被装箱。

H但是你可以用浮点说明符 ( \begin{algorithm}[H])来告诉它不要浮动

fullwidth并使用来自fullwidth以这种方式包装

\begin{fullwidth}[width=\linewidth+2cm,leftmargin=-1cm,rightmargin=-1cm]
  <your stuff here>
\end{fullwidth}

在这种情况下,您将左右边距都减少1cm。根据需要调整值。

完整示例:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{fullwidth}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{fullwidth}[width=\linewidth+2cm,leftmargin=-1cm,rightmargin=-1cm]
\begin{algorithm}[H]
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{fullwidth}

\lipsum[1]

\end{document} 

输出:

在此处输入图片描述

答案3

如果您不想让它algorithm漂浮,那么您可以将其放在一个盒子中,您可以根据需要重新调整:

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,lipsum,changepage}
\usepackage[noend]{algpseudocode}

\begin{document}

\lipsum[1]

\noindent\hspace*{-.1\textwidth}%
\begin{minipage}{1.2\textwidth}%
  \begin{algorithm}[H]
    \caption{Euclid’s algorithm}\label{alg:euclid}
    \begin{algorithmic}[1]
      \Procedure{Euclid}{$m,l$}\Comment{The g.c.d.\ of $m$ and $l$}
        \State $r \gets m \bmod l$
        \While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
        \State $m \gets l$
        \State $l \gets r$
        \State $r \gets m \bmod l$
        \EndWhile
        \State \textbf{return} $l$\Comment{The g.c.d.\ is $l$}
      \EndProcedure
    \end{algorithmic}
  \end{algorithm}
\end{minipage}

\end{document}

对于一般的调整浮动的宽度,你可以使用下面的前导修改:

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,lipsum,etoolbox}
\usepackage[noend]{algpseudocode}

\makeatletter
\newif\if@algorithm
\patchcmd{\@float@Hx}% <cmd>
  {\@nodocument}% <search>
  {\@nodocument%
   \ifnum\pdfstrcmp{#1}{algorithm}=0 % Check whether using algorithm float
     \@algorithmtrue% Inside algorithm environment
     \setlength{\columnwidth}{\algorithmwidth}% Update column width
   \fi}% <replace>
  {}{}% <success><failure>
\renewcommand\float@makebox[1]{%
  \hbox{%
    % Adjust horizontally to give a centred look
    \if@algorithm\hspace*{-.5\dimexpr\algorithmwidth-\textwidth}\fi%
    \vbox{\hsize=#1 \@parboxrestore
      \@fs@pre\@fs@iftopcapt
        \ifvoid\@floatcapt\else\unvbox\@floatcapt\par\@fs@mid\fi
        \unvbox\@currbox
      \else\unvbox\@currbox
        \ifvoid\@floatcapt\else\par\@fs@mid\unvbox\@floatcapt\fi
      \fi\par\@fs@post\vskip\z@}}}

\makeatother

\newlength{\algorithmwidth}
\AtBeginDocument{\setlength{\algorithmwidth}{1.2\textwidth}}

\begin{document}

\lipsum[1]

\begin{algorithm}[t]
  \caption{Euclid’s algorithm}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$m,l$}\Comment{The g.c.d.\ of $m$ and $l$}
      \State $r \gets m \bmod l$
      \While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
      \State $m \gets l$
      \State $l \gets r$
      \State $r \gets m \bmod l$
      \EndWhile
      \State \textbf{return} $l$\Comment{The g.c.d.\ is $l$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\lipsum[2]

\setlength{\algorithmwidth}{.8\textwidth}

\begin{algorithm}[b]
  \caption{Euclid’s algorithm}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$m,l$}\Comment{The g.c.d.\ of $m$ and $l$}
      \State $r \gets m \bmod l$
      \While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
      \State $m \gets l$
      \State $l \gets r$
      \State $r \gets m \bmod l$
      \EndWhile
      \State \textbf{return} $l$\Comment{The g.c.d.\ is $l$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\lipsum[3]
\end{document}

前言部分修改包括针对特定algorithm环境的浮动生成器补丁。浮动框的宽度被调整为\algorithmwidth(可在整个文档中设置/更改),同样仅在使用浮动时才调整algorithm

由于对传统浮动和使用 ERE 放置的(非)浮动的处理[H]完全不同,因此上述方法是互斥的。如果您希望结合使用它们,则必须结合两种方法。 的非浮动替代方案[H][h!]

相关内容