Text Formattingalgorithmic | Extension with "SWITCH" Statement

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
farn
Posts: 1
Joined: Sat Feb 18, 2012 4:53 am

algorithmic | Extension with "SWITCH" Statement

Post by farn »

Hi, dear all.

I tried to write SWITCH statements with the "algorithmic" package from the algorithms bundle. But I could not find it. So I wrote the following few new commands in my LaTeX documents. They seem to work.

Code: Select all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% The following definitions are to extend the LaTeX algorithmic 
%% package with SWITCH statements and one-line structures.
%% The extension is by 
%%   Prof. Farn Wang 
%%   Dept. of Electrical Engineering, 
%%   National Taiwan University. 
%% 
\newcommand{\SWITCH}[1]{\STATE \textbf{switch} (#1)}
\newcommand{\ENDSWITCH}{\STATE \textbf{end switch}}
\newcommand{\CASE}[1]{\STATE \textbf{case} #1\textbf{:} \begin{ALC@g}}
\newcommand{\ENDCASE}{\end{ALC@g}}
\newcommand{\CASELINE}[1]{\STATE \textbf{case} #1\textbf{:} }
\newcommand{\DEFAULT}{\STATE \textbf{default:} \begin{ALC@g}}
\newcommand{\ENDDEFAULT}{\end{ALC@g}}
\newcommand{\DEFAULTLINE}[1]{\STATE \textbf{default:} }
%% 
%% End of the LaTeX algorithmic package extension.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Just insert the above lines anywhere after your inclusion statement of the algorithmic package. Especially, to make the algorithm presentation concise, I distinguish between compound cases and one-line cases. The one-line cases begin with \CASELINE. The compound cases begin with \CASE and end with \ENDCASE. Similar to the default statements. You can try the following code.

Code: Select all

\SWITCH {$\theta$}
\CASE {1}
  Hello
\ENDCASE
\CASELINE {2}
  Good-bye
\DEFAULT
  Again ?
\ENDDEFAULT
\ENDSWITCH

Hope it works for you.


Best regards,
Farn

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Post Reply