2

I am using algorithmic inside a tcolorbox. It has by itself a margin and combined with the indent from algorithmic it is too much:

enter image description here

\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}

How can I decrease the indent, such that the line numbers are below the A of Algorithm?

1 Answer 1

3

Try this:

\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}

enter image description here

Explanation

The contents of environment algorithmic is output in a list.

\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}%
   }%

These three lines relate to the indentation of line number:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .