2

I tried to break the following equation to multiple lines as it's stretching outside of Algorithm box in my LaTex document,

\begin{algorithm}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}

Thread Data$(\zeta, \epsilon)$\\
\Input{Packets}
\Output{Dataset}

\For{k iterations} {

\For {d steps} {

$$
\nabla_{\theta} \frac{1}{m} \sum_{i=1}^{m} \left[ \log{D_{\theta}(x^{(i)})} +\log({1 - D_{\theta}(G_{\phi}(z^{(i)}))}) \right]
$$\\

}

}
\caption{Algorithm 1.}
\end{algorithm}

Those are the packages I used,

\usepackage{cite} 
\usepackage{mathtools,amssymb}  
\usepackage{graphicx} 
\usepackage{textcomp} 
\usepackage[table]{xcolor} 
\usepackage{lipsum} 
\usepackage{tabularx}
\usepackage{algorithmic} 
\usepackage[noend]{algpseudocode} 
\usepackage[linesnumbered,ruled]{algorithm2e}

Output,

enter image description here

Edit:

This is a two-column document.

4
  • Which document class do you employ? Is the document in twol-column mode?
    – Mico
    Mar 25 '21 at 16:58
  • @Mico. Yeah two-column mode. Sorry I forgot to add that. I edited it.
    – Avv
    Mar 25 '21 at 17:00
  • Which document class do you employ?
    – Mico
    Mar 25 '21 at 17:14
  • @Mico. It's IEEEtran.
    – Avv
    Mar 25 '21 at 17:15
2

Some suggestions: use a Times compatible math font instead of the default (which is Computer Modern Math); don't use $$ ... $$, instead, use $\displaystyle ... $; write \nabla_{\!\theta}, D_{\!\theta}, and G_{\!\phi}, i.e., reduce the space between the main symbols and their respective subscripts; and don't use \left and \right -- instead, use \bigl and \bigr.

Aside: If you use inline math instead of display math and refrain from using \left and \right, LaTeX will likely find a suitable line break on its own, should the equation still be too long to fit. To verify this, add the document class option 12pt to the following code and recompile.

enter image description here

\documentclass{IEEEtran}
%% (pared the preamble down to the bare minimum)
\usepackage{mathtools,amssymb}
\usepackage{newtxmath}   % <-- use a Times-type math font
\usepackage[noend]{algpseudocode} 
\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}
\begin{algorithm}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}

Thread Data$(\zeta, \epsilon)$\\
\Input{Packets}
\Output{Dataset}

\For{$k$ iterations} {

\For {$d$ steps} {
$\displaystyle
\nabla_{\!\theta}\, \frac{1}{m} \sum_{i=1}^{m} 
   \bigl[ \log D_{\!\theta}(x^{(i)}) +
   \log\bigl({1 - D_{\!\theta}(G_{\!\phi}(z^{(i)}))}\bigr) \bigr]
$\\
}
}
\caption{Algorithm 1.}
\end{algorithm}
\end{document}
18
  • Thank you. It's working now. Could you please tell me what are the packages that you used? \usepackage{mathtools,amssymb} \usepackage{newtxmath} % <-- use a Times-type math font
    – Avv
    Mar 25 '21 at 19:02
  • 1
    @Abraheem -- Sorry, that was a typo. It should have been \documentclass[12pt]{IEEEtran}, not \usepackage[12pt]{IEEEtran}.
    – Mico
    Mar 25 '21 at 19:13
  • 1
    @Abraheem - You asked, "Do you mean I should add mathtools instead of amsmath?" Yes.
    – Mico
    Mar 25 '21 at 19:19
  • 1
    @Abraheem - Did you miss the "refrain from using \left and \right" condition in my answer? For sure, I wrote this for a very good reason. You ignore this at your own peril.
    – Mico
    Mar 25 '21 at 19:27
  • 1
    @Abraheem - Please ask a new question regarding the meaning of \bigl and \bigr.
    – Mico
    Mar 25 '21 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.