块外算法缩进

块外算法缩进

我想更改完整代码的水平位置。如果愿意,请删除填充。我该怎么做?

\begin{algorithm}[t!]
\algrenewcommand\algorithmicindent{.1em}%
\caption{With intent no indent}
    \begin{algorithmic}%\
    \Function{\publish[$c, data$]}{}
        \State{algorithmicindent works inside this block only}
    \EndFunction
    \end{algorithmic}%\
\end{algorithm}

在此处输入图片描述

答案1

algorithmicx设置整个algorithmic环境列表在环境开始时使用结构。它假设会有一些行号,因此会留下一些水平缩进。如果你永远不会使用行号,你可以只使用\leftmargin\labelwidth\labelsep使用0ptxpatch\algorithmic

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,algpseudocode}
\usepackage{xpatch}

\makeatletter
\xpatchcmd{\algorithmic}{\ALG@tlm\z@}{\ALG@tlm\z@\leftmargin 0pt}{}{}
\makeatother

\begin{document}

\begin{algorithm}
  \caption{With intent no indent}
  \begin{algorithmic}
  \Function{Publish[$c$, \textit{data}]}{}
    \State algorithmicindent works inside this block only
  \EndFunction
  \end{algorithmic}
\end{algorithm}

\end{document}

紧接着 后附加\xpatchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}的设置。在列表的构造中,这发生在所有列表环境测量都设置完成后。\leftmargin 0pt\ALG@tlm\z@

algorithmic以下是定义的环境视图algorithmicx.sty,以及上面\xpatchcmd插入代码的位置:

\newenvironment{algorithmic}[1][0]%
   {%
   \edef\ALG@numberfreq{#1}%
   \def\@currentlabel{\theALG@line}%
   %
   \setcounter{ALG@line}{0}%
   \setcounter{ALG@rem}{0}%
   %
   \let\\\algbreak%
   %
   \expandafter\edef\csname ALG@currentblock@\theALG@nested\endcsname{0}%
   \expandafter\let\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
   %
   \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@% <----- resetting of \leftmargin 0pt inserted here
      }%
   \setcounter{ALG@nested}{0}%
   \ALG@beginalgorithmic%
   }%
   {% end{algorithmic}
   % check if all blocks are closed
   \ALG@closeloops%
   \expandafter\ifnum\csname ALG@currentblock@\theALG@nested\endcsname=0\relax%
   \else%
      \PackageError{algorithmicx}{Some blocks are not closed!!!}{}%
   \fi%
   \ALG@endalgorithmic%
   \end{list}%
   }%

相关内容