1

I want to recreate the match-with construct typical in functional programming languages using the algorithm2e package. I've found that \SetKwSwitch gets me most of the way there, but it forces the 'do' to be the same for the switch and the case statements.

This is my code:

\documentclass{article} \usepackage[linesnumbered,lined,ruled]{algorithm2e} \begin{document} \begin{figure} \begin{algorithm}[H] \DontPrintSemicolon \SetKwSwitch{Match}{MatchCase}{MatchOther}{match}{with}{|}{\_}{}{end} \Match{a}{ \lMatchCase{4}{Some 1\;} \lMatchCase{8}{Some 2\;} } \end{algorithm} \end{figure} \end{document} 

What I get is something like:

match a with | 4 with Some 1 | 8 with Some 2 end 

What I'd like is:

match a with | 4 => Some 1 | 8 => Some 2 end 
1
  • 3
    You need to show your code you have now, which achieved that you don't want, though. Commented 10 hours ago

1 Answer 1

4

You can patch (the internal version of) \lMatchCase to use \KwSty{$\Rightarrow$} instead of \KwSty{with}.

\documentclass{article} \usepackage[linesnumbered,lined,ruled]{algorithm2e} \usepackage{etoolbox} \SetKwSwitch{Match}{MatchCase}{MatchOther}{match}{with}{|}{\_}{}{end} \makeatletter \patchcmd{\algocf@algocf@lMatchCasemain} {\KwSty{with}}% search {\KwSty{$\Rightarrow$}}% replace {}{} \makeatother \begin{document} \begin{algorithm}[H] \caption{Match case} \DontPrintSemicolon \Match{a}{ \lMatchCase{4}{Some 1} \lMatchCase{8}{Some 2} } \end{algorithm} \end{document} 

output

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.