与其他布局的样式算法

与其他布局的样式算法

我正在使用algorithm和在我的 LaTeX 文档中编写算法algpseudocode

在此处输入图片描述

问题在于,算法中的字体太“重”,夺走了其他文本的注意力。它似乎与普通文本不同 - 不仅因为它使用粗体字体,而且看起来也更大一些。

是否可以让字体更浅 - 最终使用一点行距?

我曾尝试寻找算法的样式,但改变布局似乎有点困难。

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

Some text

\begin{algorithm}
\caption{The Bellman-Kalaba algorithm}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
\ForAll {$v \in V(G)$}
\State $l(v) \leftarrow \infty$
\EndFor
\State $l(u) \leftarrow 0$
\Repeat
\For {$i \leftarrow 1, n$}
\State $min \leftarrow l(v_i)$
\For {$j \leftarrow 1, n$}
\If {$min > e(v_i, v_j) + l(v_j)$}
\State $min \leftarrow e(v_i, v_j) + l(v_j)$
\State $p(i) \leftarrow v_j$
\EndIf
\EndFor
\State $l’(i) \leftarrow min$
\EndFor
\State $changed \leftarrow l \not= l’$
\State $l \leftarrow l’$
\Until{$\neg changed$}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

答案1

不幸的是,粗体在algpseudocode包中是硬编码的。因此我们必须将所有定义从包中复制出来。我用\textbf新的替换了\algkeyword,可以对其进行调整以执行任何看起来不错的操作。在这里我选择了无操作,仅打印输入。

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\newcommand\algkeyword[1]{#1}

\algrenewcommand\algorithmicend{\algkeyword{end}}
\algrenewcommand\algorithmicdo{\algkeyword{do}}
\algrenewcommand\algorithmicwhile{\algkeyword{while}}
\algrenewcommand\algorithmicfor{\algkeyword{for}}
\algrenewcommand\algorithmicforall{\algkeyword{for all}}
\algrenewcommand\algorithmicloop{\algkeyword{loop}}
\algrenewcommand\algorithmicrepeat{\algkeyword{repeat}}
\algrenewcommand\algorithmicuntil{\algkeyword{until}}
\algrenewcommand\algorithmicprocedure{\algkeyword{procedure}}
\algrenewcommand\algorithmicfunction{\algkeyword{function}}
\algrenewcommand\algorithmicif{\algkeyword{if}}
\algrenewcommand\algorithmicthen{\algkeyword{then}}
\algrenewcommand\algorithmicelse{\algkeyword{else}}
\algrenewcommand\algorithmicrequire{\algkeyword{Require:}}
\algrenewcommand\algorithmicensure{\algkeyword{Ensure:}}
\algrenewcommand\algorithmicreturn{\algkeyword{return}}

\begin{document}
\begin{algorithm}
  \caption{The Bellman-Kalaba algorithm}
  \begin{algorithmic}[1]
    \Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
      \ForAll {$v \in V(G)$}
        \State $l(v) \leftarrow \infty$
      \EndFor
      \State $l(u) \leftarrow 0$
      \Repeat
        \For {$i \leftarrow 1, n$}
          \State $min \leftarrow l(v_i)$
          \For {$j \leftarrow 1, n$}
            \If {$min > e(v_i, v_j) + l(v_j)$}
              \State $min \leftarrow e(v_i, v_j) + l(v_j)$
              \State $p(i) \leftarrow v_j$
            \EndIf
          \EndFor
          \State $l’(i) \leftarrow min$
        \EndFor
        \State $changed \leftarrow l \not= l’$
        \State $l \leftarrow l’$
      \Until{$\neg changed$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

答案2

如果您不在\textbf \textsf setspace\textbf中自行使用:algorithmic environment, you can fool LaTeX pretendingis, for instance. As to the spacing, you do it with

\documentclass{article}
\usepackage{amsmath} \usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{setspace, etoolbox, caption}
\AtBeginEnvironment{algorithmic}{\setstretch{1.25}\let\textbf\textsf\vspace{0.4ex}}

\begin{document}

Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text.Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text.

 \begin{algorithm}
\caption{The Bellman-Kalaba algorithm}
\begin{algorithmic}[1]
\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
\ForAll {$v \in V(G)$}
\State $l(v) \leftarrow \infty$
\EndFor
\State $l(u) \leftarrow 0$
\Repeat
\For {$i \leftarrow 1, n$}
\State $\min \leftarrow l(v_i)$
\For {$j \leftarrow 1, n$}
\If {$\min > e(v_i, v_j) + l(v_j)$}
\State $\min \leftarrow e(v_i, v_j) + l(v_j)$
\State $p(i) \leftarrow v_j$
\EndIf
\EndFor
\State $l’(i) \leftarrow \min$
\EndFor
\State $\text{changed} \leftarrow l \not= l’$
\State $l \leftarrow l’$
\Until{$\neg \text{changed}$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\textbf{Some more text.} Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text.Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text.

\end{document} 

在此处输入图片描述

相关内容