1

I want to reduce the font size of a particular line in an algorithm pseudocode where I use algorithm and algorithmic package. How can I do it? For example in the following I want to fit line 9 in one line:

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

enter image description here

1
  • You're not at all concerned about what the output will look like by (say) scaling things down to fit? Of course, you're making it easy on yourself by using twocolumn mode either. I'd recommend adding some \STATEs where you define some more terms (like \STATE $\Delta = \eta_k \nabla L_k^h(w_h^{k,j-1}) + \sqrt{2\eta_k \beta_k^{-1}}\epsilon_h^{k,j}$ and then use something like \STATE $w_h^{k,j} = w_h^{k,j-1} + \Delta$. This tackles line 9... what about line 16? Commented Jan 13, 2023 at 6:21

2 Answers 2

2

Rather than reducing the font size, I'd split the formula across lines.

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

I added \, after the square root so it's easier for the reader to parse the formula. The ^{\vphantom{-1}} trick moves the subscript to the same level as the next symbol; \smash[b] reduces the size of the square root and there's no need to compensate for the missing depth because the square roots is above blank space.

I changed \textbf to the correct \mathbf and \ldots to \dots.

Finally, \raggedright avoids clumsy hyphenation and underfull boxes.

enter image description here

0

You can use the \resizebox in graphicx such as:

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

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.