
Latex 構文を使用してアルゴリズムを記述しているときに、手順が 1 つの場所で始まり、別の場所で終了するというインデントの問題に遭遇しました。コードの一部:
\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\begin{document}
\begin{algorithm}
\caption{Test}\label{euclid}
\begin{algorithmic}[1]
\State $\textit{currentS} \gets \textit{initialS}$
\State $\textit{bestS} \gets \textit{currentS}$
\State $\textit{listTest} \gets \left[ \right]$
\While{$fitness(bestS) < 0$}{
\State $\textit{bestC} \gets \textit{null}$
\For {($\textit{ca} \in \textit{currentS.get})}${
\If{($\neg\textit{listTest.contains(c)})$}{
\EndIf}
\EndFor}
\EndWhile}
\State \Return $\textit{bestSolution}$
\end{algorithmic}
\end{algorithm}
\end{document}
答え1
ツールの使い方が間違っています。の構文\While
は
\While {<condition>}
...
\EndWhile
ループの内容を中括弧で囲みません。
完全なコード (\State
内部の にを追加しました\If
)。\mathit
よりも の方が望ましいことにも注意してください\textit
。null 配列の場合\left[\right]
は に変更しました[\,]
。
\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\begin{document}
\begin{algorithm}
\caption{Test}\label{euclid}
\begin{algorithmic}[1]
\State $\mathit{currentS} \gets \mathit{initialS}$
\State $\mathit{bestS} \gets \mathit{currentS}$
\State $\mathit{listTest} \gets [\,]$
\While{$\mathit{fitness}(\mathit{bestS}) < 0$}
\State $\mathit{bestC} \gets \mathit{null}$
\For {($\mathit{ca} \in \mathit{currentS.get})}$
\If{($\neg\mathit{listTest.contains(c)})$}
\State do something
\EndIf
\EndFor
\EndWhile
\State \Return $\mathit{bestSolution}$
\end{algorithmic}
\end{algorithm}
\end{document}