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:

<TeXmacs|2.1.1>

<style|<tuple|generic|automate>>

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

    <algo-ensure|<math|y=x<rsup|n>>>

    <algo-state|<math|y\<leftarrow\>1>>

    <algo-state|<math|X\<leftarrow\>x>>

    <algo-state|<math|N\<leftarrow\>n>>

    <\algo-while|<math|N\<neq\>0>>
      <\algo-if-else-if|<math|N> is even>
        <algo-state|<math|X\<leftarrow\>X\<times\>X>>

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

        <algo-state|<math|N\<leftarrow\>N-1>>
      </algo-if-else-if>
    </algo-while>
  </specified-algorithm>
</body>

<\initial>
  <\collection>
    <associate|page-medium|paper>
  </collection>
</initial>

<\references>
  <\collection>
    <associate|alg:cap|<tuple|1|1>>
  </collection>
</references>
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:
https://twitter.com/gnu_texmacs/status/1638653497800368129

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))))
      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?