What are the advantages of using a pseudocode package (like algorithm2e) above simply putting code inside of a verbatim block?

In order to make the question less vague, I will say this: Sometimes, inside of a LaTeX document, a person will want to display code. The code to display might be pseudocode, Java, C, C++, Fortran, LaTeX, or be written in some other language. There are various goals. For example, indentation is a key feature of code. We wish to have indents preserved in the final output .pdf. \verbatim preserves indentation, displays the result in a mono-spaced font, etc... Packages like algorithm2e (the specific package doesn't matter) can also be used to display code (many kinds of code -- pseudocode, LaTeX code, Java...). The question is, what are the advantages of using a package tool to display your code instead of simply using the \verbatim command to display your code? \verbatim might be defined in some LaTeX package, but \verbatim is comes with LaTeX distributions more often than things like algorithm2e Often, no import statement is required for for using \verbatim. Does anything bad happen while using \verbatim which won't happen when using a tool like algorithm2e?

closed as unclear what you're asking by Werner, Kurt, Stefan Pinnow, TeXnician, Johannes_B Jan 10 at 6:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    your question is rather vague, pseudocode is good for laying out pseudocode (typically with italic meta-variables and arrows and typeset math expressions) and verbatim is good for laying out real code, verbatim. So they take different input and produce different output, so the question of advantages of one over the other doesn't really arise so often. – David Carlisle Jan 7 at 21:12
  • @DavidCarlisle If you believe that verbatim is for "real-code" and that something like algorithm2e is better for "pseudo-code," then you could give that as an answer. You could say that The advantage of verbatim is that verbatim is better for displaying "real-code." Just because you think the answer to the question is obvious, that does not mean that the is vague. – IdleCustard Jan 12 at 2:07
  • No, I did not vote to close the question but I wouldn't answer it (and I wouldn't vote to open) it unless you edit it to be more specific. No answers can be posted anyway while it's on hold. – David Carlisle Jan 12 at 2:09
  • @DavidCarlisle Many text editors don't distinguish between "pseudo-code" and "real-code." Also, human authors usually display both "pseudo-code" and "real-code" using very similar tools. For example, on stack overflow users choose to display their pseudo-code in a mono-spaced font. Stack overflow users also usually put real-code in a mono-spaced font. I would think that a tool good for displaying pseudo-code would also be good for real-code. By "real-code," did you mean LaTeX code? Are you saying that \verbatim is good for displaying LaTeX code? – IdleCustard Jan 12 at 2:17

The one (algorithm2e, but also algorithms, algorithmicx, ...) is meant to be used with pseudocode and therefore contains formatting options for statements. The latter (verbatim) is typically used to place actual code in the document as-is, without formatting.

The two can be combined when using listings or fancyvrb, for example.

What's the advantage? It depends on the application. Pseudocode works well to provide a generic description of an algorithm in an article. This also doesn't require an understanding of the language used, whereas verbatim requires an understanding of the language, or may require smaller pieces of code for a clear understanding.

  • Why would code inside of a \verbatim block need to be something which "requires an understanding of the language?" It is true that you must understand LaTeX in order to use\verbatim. However, I presume that you mean an understanding of the language is required to to read what is visible in the output document. You seem to be saying, put "actual" code, like Java, inside a \verbatim block, but use something like listings, or fancyvrb for pseudo-code. Why? \verbatim doesn't know whether you give it Java or pseudo-code. It's about making code readable in the output. – IdleCustard Jan 12 at 2:43
  • @IdleCustard: True. Since people typically post actual code within a verbatim environment (because languages each have their own syntax that might require the use of special characters like % or $ or & or ...), in order to understand the actual code, you'll have to understand that language. – Werner Jan 12 at 3:06
  • @IdleCustard the input to verbatim, listings, minted, fancyverb is typically a file in some language (perhaps annotated with tex) to be shown verbatim. the input to packages like algorith... is essentially a stylized latex math mode, with help for indenting to show program flow, so it is normally used for describing program logic/pseudo-code rather than displaying anything that could be used verbatim with a programming language compiler. – David Carlisle Jan 12 at 15:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.