如果块没有显示在算法环境中

如果块没有显示在算法环境中

当我将 If 块放入算法中时,它不显示。

\documentclass[a4paper,english,12pt,oneside]{book}
%\documentclass[11pt,draft]{article}
%\usepackage{fullpage}
%\usepackage[top=1in, bottom=1in, left=1in, %right=1in]{geometry}
%\usepackage[margin=1in, paperwidth=8.5in, paperheight=11in]{geometry}
\usepackage[left=2cm, right=2cm, top=2.5cm, bottom=2.5cm]{geometry}
%\usepackage[left=20mm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{refstyle}
\usepackage{subcaption}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{enumerate}
\usepackage{epstopdf}
\usepackage{breqn}
\usepackage{mathtools}
%--------------------------------------------------------------------------------------------------
%\usepackage{a4wide}
\usepackage{amsfonts}
\usepackage{amstext}
\usepackage{amsthm}
\usepackage{newlfont}
\usepackage{graphics}
\usepackage{tabularx}
\usepackage{geometry}
\usepackage{lscape}
\usepackage{multirow}
\usepackage{epsfig}
%Alg Package ------------------------------------------------------------------------------------------
\usepackage{algorithm}
\usepackage{algcompatible}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{pifont}

\begin{document}
1) first case:

\begin{algorithmic}[1]
\If{$quality\ge 9$}
\State $v'_{\theta} = - \|U\|'_{\theta} e_1$
\Else
\State $v'_{\theta} = + \|U\|'_{\theta} e_1$
\ENDIF
\EndIf
\end{algorithmic}

2) second case:

\begin{algorithm}
\caption{DER ($U,U'_{\theta}$)}
\label{CHalgorithm}
\begin{algorithmic}[1]
\Procedure{process}{}
\State $\|U\|'$
\IF {$U(1)\ge 0$}
    \State $v'_{\theta} = - \|U\|'_{\theta} e_1$
\ELSE
    \State $v'_{\theta} = + \|U\|'_{\theta} e_1$
\ENDIF
\State $\|v\|'_{\theta} = $
\State $u'_{\theta} = $
\State $(H_u)'_{\theta} = $  
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

答案1

你加载了太多算法包。我建议只加载

\usepackage{algorithm}
\usepackage{algpseudocode}

algpseudocode无论如何都会加载algorithmicx

if-then-else 没有显示的问题只是你选择了错误的关键字。因为algpseudocode它们是\If- \Else- \EndIf

在此处输入图片描述

\documentclass{book}
\usepackage{algorithm}
%\usepackage{algcompatible}
%\usepackage{algorithmicx}
\usepackage{algpseudocode}

\begin{document}
1) first case:

\begin{algorithmic}[1]
\If{$quality\ge 9$}
\State $v'_{\theta} = - \|U\|'_{\theta} e_1$
\Else
\State $v'_{\theta} = + \|U\|'_{\theta} e_1$
\EndIf
\end{algorithmic}

2) second case:

\begin{algorithm}
\caption{DER ($U,U'_{\theta}$)}
\label{CHalgorithm}
\begin{algorithmic}[1]
\Procedure{process}{}
\State $\|U\|'$
\If {$U(1)\ge 0$}
    \State $v'_{\theta} = - \|U\|'_{\theta} e_1$
\Else
    \State $v'_{\theta} = + \|U\|'_{\theta} e_1$
\EndIf
\State $\|v\|'_{\theta} = $
\State $u'_{\theta} = $
\State $(H_u)'_{\theta} = $  
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

相关内容