减少算法和算法伪代码中特定行的字体大小

减少算法和算法伪代码中特定行的字体大小

我想减小我使用algorithmalgorithmic打包的算法伪代码中某一行的字体大小。我该怎么做?例如,我想将第 9 行放在一行中:

\STATE $w_h^{k,j} = w_h^{k,j-1} - \eta_k \nabla L_k^h(w_h^{k,j-1}) + \sqrt{2\eta_k \beta_k^{-1}}\epsilon_h^{k,j}$
\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Algorithm}
\date{January 2023}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\twocolumn
\usepackage{algorithm}
\usepackage{algorithmic}
\begin{document}

\maketitle

\begin{algorithm}[t]
\caption{\label{Algorithm:LMC} LMC}
\begin{algorithmic}[1]
\STATE Input: step sizes $\{\eta_k > 0\}_{k\geq 1}$, inverse temperature $\{\beta_k\}_{k\geq 1}$, loss function $L_k(w)$. 
\STATE Initialize $w_h^{1,0} =  \textbf{0}$ for $h \in [H]$, $J_0 = 0$.
\FOR{episode $k=1,2,\ldots, K$}
\STATE Receive the initial state $s_1^k$.

\FOR{step $h=H, H-1,\ldots, 1$}
    \STATE $w_h^{k,0} = w_h^{k-1, J_{k-1}}$\label{line:warm-start}
    \FOR{$j = 1, \ldots, J_k$}
        \STATE $\epsilon_h^{k,j} \sim \mathcal{N}(0, I)$
        \STATE $w_h^{k,j} = w_h^{k,j-1} - \eta_k \nabla L_k^h(w_h^{k,j-1}) + \sqrt{2\eta_k \beta_k^{-1}}\epsilon_h^{k,j}$
    \ENDFOR

\STATE  $Q^k_{h}(\cdot,\cdot) \leftarrow Q(w_h^{k,J_k};\phi(\cdot, \cdot))$  \label{algline:q-update}
\STATE  $V^k_{h}(\cdot) \leftarrow \max_{a \in \mathcal{A}} Q_{h}^{k}(\cdot,a)$.
~\label{Alg:min-for-Q}
\ENDFOR


\FOR{step $h=1, 2, \ldots, H$}

\STATE Take  action  $a^k_{h} \leftarrow \argmax_{a \in \mathcal{A}} Q_h^{k}(s_h^k,a)$.
\STATE  Observe reward $r^k_{h}(s_h^k,a_h^k)$, get next state $s^k_{h+1}$. 
\ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

答案1

我不会减小字体大小,而是将公式分成多行。

\documentclass[twocolumn]{article}

\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algorithmic}

\DeclareMathOperator{\argmax}{arg\,max}


\begin{document}

\title{Algorithm}
\author{hi15}
\date{January 2023}

\maketitle

\begin{algorithm}[t]
\caption{\label{Algorithm:LMC} LMC}
\raggedright
\begin{algorithmic}[1]
\STATE Input: step sizes $\{\eta_k > 0\}_{k\geq 1}$,
       inverse temperature $\{\beta_k\}_{k\geq 1}$, loss function $L_k(w)$. 
\STATE Initialize $w_h^{1,0} =  \mathbf{0}$ for $h \in [H]$, $J_0 = 0$.
\FOR{episode $k=1,2,\dots, K$}
  \STATE Receive the initial state $s_1^k$.

  \FOR{step $h=H, H-1,\dots, 1$}
    \STATE $w_h^{k,0} = w_h^{k-1, J_{k-1}}$\label{line:warm-start}
    \FOR{$j = 1, \dots, J_k$}
      \STATE $\epsilon_h^{k,j} \sim \mathcal{N}(0, I)$
      \STATE $\begin{aligned}[t]
                 w_h^{k,j} = w_h^{k,j-1}
                 & - \eta_k \nabla L_k^h(w_h^{k,j-1})
                 \\
                 & + \sqrt{\smash[b]{2\eta_k^{\vphantom{-1}} \beta_k^{-1}}}\,\epsilon_h^{k,j}
             \end{aligned}$
    \ENDFOR

    \STATE  $Q^k_{h}(\cdot,\cdot) \leftarrow Q(w_h^{k,J_k};\phi(\cdot, \cdot))$
            \label{algline:q-update}
    \STATE  $V^k_{h}(\cdot) \leftarrow \max_{a \in \mathcal{A}} Q_{h}^{k}(\cdot,a)$.
            \label{Alg:min-for-Q}
  \ENDFOR

  \FOR{step $h=1, 2, \dots, H$}
    \STATE Take  action  $a^k_{h} \leftarrow \argmax_{a \in \mathcal{A}} Q_h^{k}(s_h^k,a)$.
    \STATE  Observe reward $r^k_{h}(s_h^k,a_h^k)$, get next state $s^k_{h+1}$. 
  \ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}
\end{document}

\,在平方根后面添加了 ,这样读者更容易解析公式。这个^{\vphantom{-1}}技巧将下标移动到与下一个符号相同的级别;\smash[b]减少了平方根的大小,并且不需要补偿缺失的深度,因为平方根位于空白处上方。

我将其改为\textbf正确的\mathbf,并将\ldots其改为\dots

最后,\raggedright避免笨拙的连字和未充满的方框。

在此处输入图片描述

答案2

您可以使用如下内容\resizeboxgraphicx

\STATE \resizebox{\linewidth}{!}{\ $w_h^{k,j} = w_h^{k,j-1} - \eta_k \nabla L_k^h(w_h^{k,j-1}) + \sqrt{2\eta_k \beta_k^{-1}}\epsilon_h^{k,j}$}

在此处输入图片描述

相关内容