一、前言
小白用LaTeX写伪代码的Switch-case框架时,尝试网络答案3小时无果,最后被官方使用手册救赎。网络上大多方法基于\usepackage{algorithmic}库,看到下段代码就能明白小白所指:
% New definitions
\algnewcommand\algorithmicswitch{\textbf{switch}}
\algnewcommand\algorithmiccase{\textbf{case}}
\algnewcommand\algorithmicassert{\texttt{assert}}
\algnewcommand\Assert[1]{\State \algorithmicassert(#1)}%
% New "environments"
\algdef{SE}[SWITCH]{Switch}{EndSwitch}[1]{\algorithmicswitch\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicswitch}%
\algdef{SE}[CASE]{Case}{EndCase}[1]{\algorithmiccase\ #1}{\algorithmicend\ \algorithmiccase}%
\algtext*{EndSwitch}%
\algtext*{EndCase}%
但无论怎么尝试,都不能在小白的环境下运行,后来得知,\usepackage{algorithmic}与\usepackage[linesnumbered,ruled,vlined]{algorithm2e}不能共存,为了漂亮的版式怎能放弃algorithm2e,终于在官方使用手册中找到了解决方法。
二、技术
无需自定义环境,algorithm2e库里已安排的明明白白,直接插入如下代码即可:
\Switch{the value of T}{
\uCase{a value}{
do this\;
do that\; }
\lCase{another value}{one line}\;
\Case{last value}{
do this\;
break\; }
\Other{
for the other values\;
do that\;
} }
三、实现效果
*亲测有效