I would like to write a pseduocode inside a post with latex, how can I format it. For example in the question: How to find the interior convex polytope with the maximal volume given a set of 3D points?

How can I have something like this latex output:

enter image description here

  • $\begingroup$ Why LaTeX? Use Markdown code formatting. You can even have highlighting. $\endgroup$ – Raphael Feb 10 '18 at 0:34
  • $\begingroup$ @Raphael I think the problem is that the code-blocks don't interpret LaTeX. (Although this example doesn't desperately need LaTeX, some pseudo-code blocks definitely do) So the question would be how to get a block which can both (1) interpret LaTeX code and (2) support indentation. That seems like a reasonable question. Personally though, I think a good (and precise!) textual description is almost always clearer than a block of pseudo-code. So another option is to simply not write pseudo-code at all. $\endgroup$ – Discrete lizard Feb 11 '18 at 10:05
  • $\begingroup$ @Discretelizard If the pseudocode needs (non-unicode) mathematics, it's probably too mathy. $\endgroup$ – Raphael Feb 11 '18 at 12:28
  • $\begingroup$ Anyway, it's not possible. You can use LaTeX to typeset algorithms, but with the limitations of MathJax it's a pain in the ass for several reasons. $\endgroup$ – Raphael Feb 11 '18 at 12:28
  • 1
    $\begingroup$ @Raphael Yes, but 'unicode replaceable' LaTeX such as greek letters, arrows and summation symbols are used quite often in pseudo-code I've encountered and unicode math is generally a pain in the ass as well. Also, what exactly is the problem with 'too mathy' pseudocode? $\endgroup$ – Discrete lizard Feb 11 '18 at 13:06
  • $\begingroup$ @Discretelizard Depends on what questions you're asking, of course. As a rule of thumb, "mathy" pseudocode is rarely useful for detailed analyses. It can be useful for expressing ideas, or formulating high-level/meta algorithms. $\endgroup$ – Raphael Feb 16 '18 at 11:37

You can do the formatting using a table for alignment of the labels, manual spacing for indentation and manual typesetting of each word (paying attention to spacing).

$$ \begin{array}{ll} & \mathit{stringlen} \leftarrow \text{length of } \mathit{string}. \\ & \mathit{i} \leftarrow \mathit{patlen}. \\ \mathit{top}: & \text{if } \mathit{i} \gt \mathit{stringlen} \text{ then return false}. \\ … \\ & \text{if } \mathit{string}(\mathit{i}) = \mathit{pat}(\mathit{i}) \\ & \quad \text{then} \\ … \\ \end{array} $$

But this is painful for many reasons. The source is hard to read and edit. The rendering of such large blocks of MathJax is slow in some browsers. I hate to think what this would sound like on a screen reader. And I don't think your sample output is particularly pretty anyway.

I would use the native text markup. It's perfectly suitable for a nested structure.

  • Let $\mathit{stringlen} = \mathrm{length}(\mathit{string})$
  • Set $i = \mathit{patlen}$
  • While $i \lt \mathit{stringlen}$:
    • Set $j = \mathit{patlen}$
    • While $j \lt 0$:
  • Return false.

This is easier to do with structured code than with goto. But you shouldn't be using goto when loops would do anyway.

Source code of this post (other revisions)


In this case, I think Markdown code formatting is the way to go:

stringlen <- length of string
i <- pathlen
i <- i + max( delta_1(string(i)), delta_2(j) )

You can use Unicode symbols if you prefer the look:

stringlen ← length of string
i ← pathlen
i ← i + max( delta₁(string(i)), delta₂(j) )

Note that if your pseudo code roughly corresponds to a real programming language, you may be able to use syntax highlighting to good effect.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .