How to convert algorithm from latex to word

algorithmicalgorithmsmsword

I have this algorithm steps I coded in LaTeX as follows:


\begin{algorithm}
    \caption{Deep Q-Learning with Experience Replay}
    \label{alg:DQN}
    \begin{algorithmic}
    \State Initialize replay memory $\mathcal{D}$ to capacity $N$
    \State Initialize action-value function $Q$ with two random sets of weights $\theta, \theta'$
    \For{$episode = 1,M$}
        \For{$t = 1,T$}
        \State
        Select a random action $a_t$ with probability $\varepsilon$.
        \State  
        Otherwise, select $a_t = {\arg\max}_a Q(s_t, a; \theta)$
        \State Execute action $a_t$, collect reward $r_{t+1}$ and observe next state $s_{t+1}$
        \State Store the transition $(s_t, a_t, r_{t+1}, s_{t+1})$ in $\mathcal{D}$
        \State Sample mini-batch of transitions $(s_j, a_j, r_{j+1}, s_{j+1})$ from $\mathcal{D}$
        \State Set $ y_j = \begin{cases} 
                    r_{j+1}, & \mbox{if } s_{j+1}\mbox{ is terminal} \\ 
                    r_{j+1} + \gamma \max_{a'} Q(s_{j+1}, a'; \theta'), & \mbox{otherwise}
                    \end{cases}$
        \State Perform a gradient descent step using targets $y_j$ with respect to the online parameters $\theta$
        \State Every $C$ steps, set $\theta' \leftarrow \theta$
        \EndFor
    \EndFor
    \end{algorithmic}
\end{algorithm}

The output as follows:
enter image description here

Is there a way to convert this photo or algorithm to a word document without using an image of a latex document?
I tried to use insert equation in MS word but it didn't work.

Best Answer

This is a method I use often. (A 5 minute job)

(1) Prepare your LaTeX file with the content to be converted. In this example called Test2WORD.tex. You can use a system font that is already on your system. Here I am using calibri, the font I will be using in my main word document.

File Test2WORD.tex

%%% File Test2WORD.tex

\documentclass{article}
\usepackage{caption}
\usepackage{algorithm, algorithmic}%

\usepackage{fontspec}   
\setmainfont{calibri} % a system font <<<<

\begin{document}
    
    Some text.

\begin{algorithm}   
        \caption{Deep Q-Learning with Experience Replay} \label{alg:DQN}
    \begin{algorithmic}
            \STATE Initialize replay memory $\mathcal{D}$ to capacity $N$
            \STATE Initialize action-value function $Q$ with two random sets of weights $\theta, \theta'$
            \FOR{$episode = 1,M$}
            \FOR{$t = 1,T$}
            \STATE
            Select a random action $a_t$ with probability $\varepsilon$.
            \STATE  
            Otherwise, select $a_t = {\arg\max}_a Q(s_t, a; \theta)$
            \STATE Execute action $a_t$, collect reward $r_{t+1}$ and observe next state $s_{t+1}$
            \STATE Store the transition $(s_t, a_t, r_{t+1}, s_{t+1})$ in $\mathcal{D}$
            \STATE Sample mini-batch of transitions $(s_j, a_j, r_{j+1}, s_{j+1})$ from $\mathcal{D}$
            \STATE Perform a gradient descent step using targets $y_j$ with respect to the online parameters $\theta$
            \STATE Every $C$ steps, set $\theta' \leftarrow \theta$
            \ENDFOR
            \ENDFOR
    \end{algorithmic}
\end{algorithm}

See Algorithm \ref{alg:DQN}.

\end{document}

(2) Compile and generate Test2WORD.pdf

Output

b

(3) Open Test2WORD.pdf using ms-word (I am using word 2013).

The Word document is in the selected font and is fully editable. See the yellow highlight I made.

a

I found two places that need to be corrected (in red).

c