This question already has an answer here:
In (La)TeX documentation and tutorials, it is common to show a snippet of code next to the output it generates. It might be two minipages side by side, or a code listing followed by boxed output, or whatever. For a trivial example, we might see
\[ sin \theta \] next to its output, sin θ
Whenever I've done this, I had to use two copies of the code, e.g., once inside
verbatim and once inside a minipage or framed box). This just begs for inconsistencies and maintainability problems, since it's very easy to fix one copy and not the other. So, is there any way to display a bunch of code and to execute (a copy of) it, showing the results?
I'm pretty sure it's not straightforward: the code needs to have different catcodes for reading and displaying, but catcodes get assigned only once, when the text is encountered. But if there's no better way, it should be doable by reading the code verbatim and saving a copy in the
.aux file, from where it'll get read and interpreted the next time around. But I've never seen such a thing. Has anyone else here?