1

If I have a file containing some code (somecodefile):

Var x = io.input("x", UInt(32));
Var y = x + 1;
io.output("y", y, UInt(32));

... and I set some "delineators" (e.g. TEXBLOCK) ...

/*TEXBLOCK {x}*/
Var x = io.input("x", UInt(32));
/*TEXBLOCK {end}*/
/*TEXBLOCK {y}*/
Var y = x + 1;
io.output("y", y, UInt(32));
/*TEXBLOCK {end}*/

... is it possible to use the linerange option in an lstlisting environment (or something similar) to say have something like "linerange=x-end" produce ONLY:

Var x = io.input("x", UInt(32));

The MWE might then be, using the TEXBLOCKs "x" and "end":

\documentclass{article}
\begin{document}
\begin{lstlisting}[linerange=x-end] 
\input{somecodefile}
\end{lstlisting} 
\end{document}

As another example, have linerange=y-end produce:

Var y = x + 1;
io.output("y", y, UInt(32));

For which the MWE would be:

\documentclass{article}
\begin{document}
\begin{lstlisting}[linerange=y-end] 
\input{somecodefile}
\end{lstlisting} 
\end{document}

I have this working in Python 2.7 already in a Makefile, but I'd really like to just use LaTeX for it. Using the delineator ("TEXBLOCK") and the lstlisting environment is just what I've got the Python script doing: if there is a way to do the same thing but with a different environment then I would of course be interested in that, too. I basically just want to be able to pick out specific parts of files that contain lines of code.

1 Answer 1

0

Well I had all sorts of fun with this before eventually coming up with this dirty mess:

\newcommand{\newcode}[2]{

  \foreach \x in {0,...,#2} {
    \edef\dolisting{\noexpand\lstinputlisting[linerange=test\x,rangeprefix=//---,includerangemarker=false]{#1}}
    \dolisting
\vspace{-1em}

  }}

\begin{mdframed}
\newcode{Kernel.code}{2}
\end{mdframed}

Where the code (Kernel.code) is:

//---test0

this is some code; //---test0 //---test1

import some more code; //---test1 //---test2

class code extends morecode {
        code(code parameters) {
                super(parameters);
                code x = io.input("x", codecodecode(32));
                code y = x + 1;
                io.output("y", y, codecodecode(32));
        }
}
//---test2

You must log in to answer this question.

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