![减少算法缩进](https://linux22.com/image/416453/%E5%87%8F%E5%B0%91%E7%AE%97%E6%B3%95%E7%BC%A9%E8%BF%9B.png)
我在 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}%