Algorithm2e for Texmacs

Is there any way to write pseudocode in a format that is similar to the algorithm2e package in LaTeX with TeXmacs? The default algorithm environment in texmacs does not contain line number

You can try this:



  <\specified-algorithm|An algorithm with caption>
    <label|alg:cap> \ <algo-require|<math|n\<geq\>0>>





      <\algo-if-else-if|<math|N> is even>

        <algo-state|<math|N\<leftarrow\><frac|N|2>> <algo-comment|This is a
      <|algo-if-else-if|<math|N> is odd>



1 Like

How do I use this script? create a .ts file and add this to style folder?

You can copy the part from <\specified-algorithm| until </specified-algorithm> to the clipboard and then do Edit->Paste from-> TeXmacs.

If you want to recreate this structure yourself, do Insert->Program->Algorithm, then Focus->Specified from inside the algorithm.

I don’t see a way to insert the algo-while elements etc. from the menus or with shortcuts, so you’d have to insert them “manually” by typing \algo-while + Enter.

Thanks. However, there is no line number in this template

Thanks, do you know how to include line number in pseudocode?

Have a look at the \numbered element.

You might try something like this:

Thanks. However,

the line for conditions like “if” "“else if” are not numbered

That is a statement, not a question :slight_smile:

The numbered block is created by a Scheme routine called ext-numbered (see TeXmacs/packages/environment/env-program.ts line 359 and TeXmacs/progs/utils/misc/markup-funcs.scm). There is some code there that indicates that only the body gets numbered. You could investigate if there’s a way to number also lines that are inserted by the macro.

The easiest way to hack around this, though, would probably be to make a custom algo-if-else-if etc., which explicitly insert \numbered-line elements.

I found it hard to understand the .ts file you provided. However, the scheme file does provide some information. It seems that the numbered line will only be executed if that line belongs to “document”, the source code is in line 175 of TeXmacs/progs/utils/misc/markup-funcs.scm.

  (if (tm-func? body 'document)
      `(numbered-block (document ,@(map ext-numbered-line (tm-children body))))

I think it will be difficult to alter the definition of tm-func. Besides these options, I have no idea how to “make a custom algo-if-else-if etc., which explicitly insert \numbered-line elements.”, which documentation should I read? Should I figure out what is happening inside TeXmacs/packages/environment/env-program.ts?