TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For some reason, I am forced to create a LTXexample-like environment.

The following is my effort, but it cannot work. :-)

I don't know how to fix the problem.

Thank you in advance.


    literate=   {ï}{}0





testing.... \LaTeX !


EDIT 1: This example is the simplified version of my real scenario. :-)

share|improve this question
\renewcommand{\temporary}%? – Kilgore Dec 19 '10 at 3:46
@Kilgore, ????? – xport Dec 19 '10 at 4:00
why can't you use LTXexample? Your example defines nothing new? – Herbert Dec 19 '10 at 7:57
@Herbert, I need more flexible way to put code region and formatted region. For example, I want the code part in the left cell and the formatted part in the right cell of a tabular row. There are many formations I want to make that is difficult to obtain using LTXexample. – xport Dec 19 '10 at 8:48
then look at the package environ, it can collect environments and lets you use it later. – Herbert Dec 19 '10 at 14:27
up vote 2 down vote accepted

Here is a working solution with fancyvrb. The & before \tabularnewline is needed, the code itself is read by the hedaer definition.





\begin{tabular}{@{} m{0.5\linewidth}@{} 
                    >{\lstinputlisting{\jobname.tmp}}m{0.5\linewidth} @{}}




share|improve this answer
thanks for your help. It is very satisfactory result. 19 hours must be elapsed to send the bounty. – xport Dec 22 '10 at 13:26

You can't collect your listing in a command "for later use". listings has to change a lot of catcodes and this won't work when the code has already been read (catcodes are fixed at this moment). You would need \scantokens to reset them.

Also you can't hide \end{lstlisting} deep in some code: listings has to find the end of the listing without executing the code in between. That's why listings has its own \newenvironment command (\lstnewenvironment).

If you want to write your own LTXexample you will have to copy the method of showexpl. Basically LTXexample does this:

{\lst@BeginWriteFile{\SX@codefile}} %At start of env begin to write the code to a file
{\lst@EndWriteFile % stop writing to file
  %now reinput code file \SX@codefile "as listing"
  %reinput code file "as result"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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