1

I'm currently writing my masters thesis and I'm using algorithm2e to write pseudocode. I regularly use SetKwData to display data types. However I would slighty like to change how this is displayed when using arguments.

If I for example would like to display a key value map data type that is polymorphic in both the key, and data I would write something like this

\SetKwData{DFoo}{Foo}
\SetKwData{DBar}{Bar}
\SetKwData{DMap}{Map}

\Dmap{\DFoo, DBar}

Then the compiled document would look something like

Map(Foo, Bar)

In my opinion this looks too much like a function call. I would like to change the parenthesis to something like Map<Foo, Bar>.

1 Answer 1

2

In the example below, based on how \SetKwData{Kw}{the text} is defined in algorithm2e.sty, a generalized macro \SetKwMetaData{Kw}{the text}{left delimiter}{right delimiter} is provided, which allows you to specify the delimiters. These delimiters were hard coded to be ( and ) in \SetKwData.

\documentclass{article}
\usepackage{algorithm2e}

\makeatletter
% \SetKwMetaData{Kw}{the text}{left delimiter}{right delimiter}
\newcommand{\SetKwMetaData}[4]{%
  \algocf@newcommand{@#1}[1]{\DataSty{#2#3}\ArgSty{##1}\DataSty{#4}}%
  \algocf@newcommand{#1}{%
    \@ifnextchar\bgroup{\csname @#1\endcsname}{\DataSty{#2}\xspace}}%
  }%
\makeatother

\SetKwData{DFoo}{Foo}
\SetKwData{DBar}{Bar}
\SetKwMetaData{DMap}{Map}{$\langle$}{$\rangle$}

\begin{document}
\begin{algorithm}
  \DFoo{a}\\
  \DMap{\DFoo, \DBar}
\end{algorithm}
\end{document}

enter image description here

2
  • Thank you, i tried something similar but without the \makeatletter, and \makeatother. Not sure why they are required but it works! thank you Commented Jun 13, 2021 at 13:37
  • This pair of macros change and restore the catcode of @ character, so @ can be used as part of the name of control sequence, directly and locally (for example in \algocf@newcommand). Find more explanation in, e.g. tex.stackexchange.com/questions/106174/makeatletter-explained . Commented Jun 13, 2021 at 13:43

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.