减少算法缩进

减少算法缩进

我在 tcolorbox 中使用 algorithmic。它本身有一个边距,再加上 algorithmic 的缩进,就太多了:

在此处输入图片描述

\documentclass{standalone}

\usepackage[many]{tcolorbox}
\newtcbtheorem[number within=chapter]{TcbAlgorithm}{Algorithm}{
    colback=blue!5, colframe=blue!5, coltitle=red,
}{thm}

\usepackage[noend]{algpseudocode}
\usepackage[]{algorithm}

\begin{document}
    \begin{TcbAlgorithm}{My Algorithm}{ref}
        \begin{algorithmic}[1]
        \State foo
        \ForAll{$i=1\dotsc, n$}
        \State bar
        \EndFor
        \end{algorithmic}
    \end{TcbAlgorithm}
\end{document}

我怎样才能减少缩进,使得行号低于算法的 A?

答案1

尝试这个:

\documentclass{article}

\usepackage[many]{tcolorbox}
\newtcbtheorem[number within=chapter]{TcbAlgorithm}{Algorithm}{
    colback=blue!5, colframe=blue!5, coltitle=red,
}{thm}

\usepackage[noend]{algpseudocode}
\usepackage{algorithm}

\usepackage{xpatch}
\xpatchcmd\algorithmic
  {\labelwidth 1.2em}
  {\labelwidth .7em}
  {}{\fail}

\begin{document}
    \begin{TcbAlgorithm}{My Algorithm}{ref}
        \begin{algorithmic}[1]
        \State foo
        \ForAll{$i=1\dotsc, n$}
        \State bar
        \EndFor
        \end{algorithmic}
    \end{TcbAlgorithm}
\end{document}

在此处输入图片描述

解释

环境的内容algorithmic以列表形式输出。

\newenvironment{algorithmic}[1][0]%
   {%
   % ... ...
   %
   \begin{list}%
      {\ALG@step}%
      {%
      \rightmargin\z@%
      \itemsep\z@ \itemindent\z@ \listparindent2em%
      \partopsep\z@ \parskip\z@ \parsep\z@%
      \labelsep 0.5em \topsep 0.2em%\skip 1.2em 
      \ifthenelse{\equal{#1}{0}}%
         {\labelwidth 0.5em}%
         {\labelwidth 1.2em}%
      \leftmargin\labelwidth \addtolength{\leftmargin}{\labelsep}% Ok. the perfect leftmargin :-))
      \ALG@tlm\z@%
      }%
   \setcounter{ALG@nested}{0}%
   \ALG@beginalgorithmic%
   }%
   {% end{algorithmic}
   % ... ...
   \ALG@endalgorithmic%
   \end{list}%
   }%

这三行与行号缩进有关:

      \ifthenelse{\equal{#1}{0}}%
         {\labelwidth 0.5em}%
         {\labelwidth 1.2em}%

相关内容