Latex伪代码Switch Case(简单暴力版)

自己添加新命令的Switch Case,用的algorithm和algorithmicx安装包

格式可调整!

效果图:

代码:

\usepackage{algorithm}
\usepackage{algorithmicx}
\newcommand{\SWITCH}[1]{\textbf{switch} (#1)}
\newcommand{\ENDSWITCH}{\textbf{end switch}}
\newcommand{\CASE}[1]{\textbf{case} #1\textbf{: } }
\newcommand{\ENDCASE}{\textbf{end}}
\newcommand{\DEFAULT}{\textbf{default:} }
\renewcommand{\algorithmicrequire}{\textbf{Input:}} %输入
\renewcommand{\algorithmicensure}{\textbf{Output:}} %输出

\begin{algorithm}
    \caption{Switch Case} %标题
    \begin{algorithmic}[1] %每行显示行号,1表示每1行进行显示
        \Require c
        \Ensure b
        \State \SWITCH{c}
            \State \{ \CASE{1} c=1
                \State \qquad\qquad b=1
            \State \CASE{2} c=2
                \State \qquad\qquad b=2
            \State \CASE{3} c=3
                \State \qquad\qquad b=3
            \State \CASE{4} c=4
                \State \qquad\qquad b=4
            \State \DEFAULT 
            \State \qquad\qquad break;
        \}
        \State \ENDSWITCH
    \end{algorithmic}
\end{algorithm}

\usepackage{algorithm}
\usepackage{algorithmicx}

是两个伪代码的安装包

\newcommand{\SWITCH}[1]{\textbf{switch} (#1)}
\newcommand{\ENDSWITCH}{\textbf{end switch}}
\newcommand{\CASE}[1]{\textbf{case} #1\textbf{: } }
\newcommand{\ENDCASE}{\textbf{end}}
\newcommand{\DEFAULT}{\textbf{default:} }

 是添加的新命令,和安装包放一起,在文档前面

第一个括号中是定义的新命令,第二个括号中是定义的新命令格式,包括自定义文字、加粗、标点符号等。如果没有定义参数的命令,比如\ENDSWITCH,直接就是输出“加粗的end switch”。如果定义了参数,[1]表示参数个数,比如\SWITCH{a},表示输出“加粗的switch a”,如果改成[2],

命令变为:\newcommand{\SWITCH}[2{(#1) \textbf{switch} (#2)}

比如\SWITCH{a}{b},表示输出“加粗的a switch b”。

\State \qquad\qquad

用来调整缩进的,可以根据需求修改。

另外,代码中的Require和\Ensure是重新定义的命令。

\renewcommand{\algorithmicrequire}{\textbf{Input:}} %输入

\renewcommand{\algorithmicensure}{\textbf{Output:}} %输出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LaTeX中编伪代码Switch-case框架可以使用不同的包和命令。如果你熟悉algorithmic包,那么你可以使用algcompatible布局来切换。对于新的算法,你可以使用algpseudocode布局。只需使用\usepackage{algpseudocode}即可。不需要手动加载algorithmicx包,因为algpseudocode会自动加载它。[1] 如果你想使用Switch-case框架,可以参考官方使用手册中的示例。网络上的大多数方法基于\usepackage{algorithmic}库,但是官方使用手册提供了更好的解决方案。下面是一个示例代码,可以实现Switch-case框架: \algnewcommand\algorithmicswitch{\textbf{switch}} \algnewcommand\algorithmiccase{\textbf{case}} \algnewcommand\algorithmicassert{\texttt{assert}} \algnewcommand\Assert[1]{\State \algorithmicassert(#1)} \algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch} \algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase} 使用以上定义的命令,你可以编类似下面的伪代码: \begin{algorithmic} \Switch{$x$} \Case{$1$} \State Do something for case 1 \EndCase \Case{$2$} \State Do something for case 2 \EndCase \Case{$3$} \State Do something for case 3 \EndCase \Case{$4$} \State Do something for case 4 \EndCase \Case{$5$} \State Do something for case 5 \EndCase \EndSwitch \end{algorithmic} 这样就可以实现Switch-case框架的伪代码了。[2]在LaTeX中还有其他一些包和命令可以用于编伪代码,如algorithmicx、algorithm2e等,你可以根据自己的需求选择适合的方法。[3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值