如何对齐算法中“then”的缩进

如何对齐算法中“then”的缩进

当 if 语句很长时,其then部分打印会错位,如下例所示:

while
    if statement
then
        undertand_this

代码来源如下解决方案.我有以下代码:

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

在此处输入图片描述

可以修复的缩进then,想要的输出是,then在 if 语句中写入:

1: while TRUE do 
2:     if alper ∧ understand_some_more ∧ underst and
           then
3:         X = 1

答案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}

在此处输入图片描述

请注意,在 OP 的屏幕截图中,“then”开始一个新行,因为“if ... then”比行宽长,因此换行成两行。

更新

\algoIndent{<num of level>}提供了一个新命令:

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

在此处输入图片描述

相关内容