0

When a statement of if is long, its then part is printed misaligned as following example:

while
    if statement
then
        undertand_this

Origin of code comes from following solution. I have following code:

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage[table]{xcolor}
\usepackage{eulervm}
\usepackage{booktabs}
\usepackage{algorithmicx}
\usepackage{tabularx, longtable}
\usepackage{algorithm}
\usepackage[LGR, T1]{fontenc}
\usepackage[noend]{algpseudocode}
\newcommand{\quot}{\textup{\textquotesingle}}
\algnewcommand{\algorithmicor}{$ $ \mathrm{or} $ $}
\algnewcommand{\FALSE}{\textit{false}}
\algnewcommand{\TRUE}{\textit{true}}
\algnewcommand{\NOT}{\textbf{not }}
\algnewcommand{\OR}{\vee}
\algnewcommand{\AND}{\wedge}

\begin{document} \begin{algorithm} \caption{The Function} \hspace{\algorithmicindent} \textbf{Input:} {} \begin{algorithmic}[1] \While{$TRUE$} \If{\begin{tabular}{@{\hspace{}}l@{}} $alper \AND understand_some_more \AND understand$ \end{tabular}} \State{}$X = 1$ \EndIf{} \EndWhile{}~\label{lwhileend:algo-storage} \end{algorithmic} \end{algorithm} \end{document}

enter image description here

It is possible to fix indent of the then, wanted output would be, where then is written inside the if statement:

1: while TRUE do 
2:     if alper ∧ understand_some_more ∧ underst and
           then
3:         X = 1
alper
  • 1,389
  • Unrelated, but you're loading three different packages (tabularx, algorithm, algpseudocode) more than once. You should only load them once each. – frabjous May 14 '22 at 20:24
  • 3
  • I have removed multiple loaded packages – alper May 15 '22 at 11:43
  • I know that this doesn't solve your problem because you are using LaTeX (I see it from your example file). Yet let me note that if you use OpTeX and not LaTeX, then there is no problem. OpTeX users have trick 0078 for pseudo-codes, see http://petr.olsak.net/optex/optex-tricks.html#algol and they have direct control of indentation: the indentation in the source is equal to indentation of the output. Moreover, the source looks more transparent, see example at the URL of the trick. And your example has following source: http://petr.olsak.net/img/algoli.png – wipet May 16 '22 at 03:20
  • Hmm the referred answer uses algorithm2e package, while you use algpseudocode. Hence it seems the tabular trick doesn't work for you, and can be removed. – muzimuzhi Z May 18 '22 at 06:39

1 Answers1

1
%\documentclass[10pt,journal,compsoc]{IEEEtran}
\documentclass{article}
\usepackage{algorithm}            % for float env `algorithm`
\usepackage[noend]{algpseudocode} % for env `algorithmic`

\algnewcommand{\AND}{\wedge}

\begin{document}
\begin{algorithm}
    \caption{The Function}
    \textbf{Input:}
    \begin{algorithmic}[1]
        \While{$TRUE$}
          \If{$alper \AND understand\_some\_more \AND understand$}
            \State $X = 1$
          \EndIf
        \EndWhile~\label{lwhileend:algo-storage}
    \end{algorithmic}
\end{algorithm}
\end{document}

enter image description here

Note in OP's screenshot, "then" starts a new line because the "if ... then" is longer than the line width, hence wrapped into two lines.

Update

A new command \algoIndent{<num of level>} is provided:

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{algorithm}            % for float env `algorithm`
\usepackage[noend]{algpseudocode} % for env `algorithmic`

\algnewcommand{\AND}{\wedge}

\newcommand\algoIndent[1]{% \linebreak\hspace{\dimexpr\algorithmicindent#1}% }

\begin{document} \begin{algorithm} \caption{The Function} \textbf{Input:} \begin{algorithmic}[1] \While{$TRUE$} \If{$alper \AND understand_some_more \AND understand$\algoIndent{2}} \State $X = 1$ \EndIf \EndWhile~\label{lwhileend:algo-storage} \end{algorithmic} \end{algorithm} \end{document}

enter image description here

muzimuzhi Z
  • 26,474