7

I am using showexpl.sty to display side by side the code snippet and its corresponding output.

Because of the paper size limit, I intentionally want to remove the "redundant" rows, i.e., the first n rows and the last m rows from the code snippet.

Is it possible to do that in showexpl.sty ?

\documentclass{article}

\usepackage{showexpl}
\lstset{%
% any setting goes here.
}
\begin{document}

\begin{LTXexample}[%
%any option goes here.
]
%redundant n rows begin
% one
% two
% ...
% n th rows
Important parts go here.
Again... it is important!
...
%redundant m rows begin
% one 
% ...
% m th rows
\end{LTXexample}

% others important parts go here.
\end{document}

I reopen this question because using linerange is not possible when I don't know the number of lines until at "runtime".


I will use @Herbert's solution for inline code inclusion and @TH's solution for external code inclusion. Unfortunately I cannot make 2 green-checked.

1
  • there is no difference in using the linerange argument for inline or external code. The external code can also be marked with \START or %START and same for STOP.
    – user2478
    Dec 29, 2010 at 19:15

3 Answers 3

3

Define an own environment, which uses the \START--\STOP sequence of the part which should be listed. These two commands itself have no meaning. You do not need to know how many lines your code has.

\documentclass{article}
\makeatletter
\let\pc\@percentchar
\makeatother

\usepackage{pstricks,fancyvrb,array,listings}
\lstset{basicstyle=\ttfamily\small}

\def\endExample{\end{VerbatimOut}
  \def\START{}\def\STOP{}\input{\jobname.tmp}}
\newcommand\Example{%
\VerbatimEnvironment
\begin{VerbatimOut}{\jobname.tmp}}

\begin{document}

\begin{tabular}{@{} m{0.5\linewidth}@{} 
                    >{\lstinputlisting[includerangemarker=false,
                    linerange=\\START-\\STOP]{\jobname.tmp}}m{0.5\linewidth} @{}}

\begin{Example}
\pspicture(3,2)
\START
\psframe*[linecolor=blue!30](3,2)
\STOP
\endpspicture
\end{Example}
& 
\tabularnewline

\begin{Example}
\pspicture(3,2)
\START
\psframe*[linecolor=red!30](3,2)
\endpspicture
\STOP
\end{Example}
& 
\tabularnewline

\end{tabular}

\end{document}
2
  • I think your solution is faster than @TH's because iteration done by @TH needs extra time. Thank for your smart solution. Dec 29, 2010 at 18:19
  • I do like this [rangeprefix=\%, rangesuffix=, includerangemarker=false, linerange=start-stop]. thanks. Jan 3, 2011 at 5:02
4

You can always count the number of lines in the file before hand.

% This line will be removed
\documentclass{article}
\usepackage{listings}
\newread\lf
\newcount\linecount
\newcommand\linesinfile[1]{%
        \linecount0
        \openin\lf#1
        \unless\ifeof\lf
                \loop
                        \readline\lf to\lfline
                \unless\ifeof\lf
                        \advance\linecount by1
                \repeat
                \closein\lf
        \fi
}
\begin{document}
\linesinfile{\jobname.tex}
There are \number\linecount\ lines in the file `\jobname.tex'.

\small
\lstinputlisting[language={[LaTeX]TeX},firstline=2,
lastline=\numexpr\linecount-1]{\jobname.tex}
\end{document}
% This line will be removed
18
  • How to pass the number to linerange ? I have more than 500 source code files. Dec 29, 2010 at 17:18
  • Passing numbers to linerange is harder because it looks for x-y and when I give it 2-\numexpr\linecount-1 it doesn't like that. Instead, you can just set another count register (or just adjust \linecount) and use that.
    – TH.
    Dec 29, 2010 at 17:26
  • I am trying to combine them into a single command. Dec 29, 2010 at 17:31
  • Can \lf and \linecount be placed inside \linesinfile as local variables? Dec 29, 2010 at 17:32
  • @xport: \linesinfile is not expandable so you can't get it to just expand to the number of lines in the file. I don't really know what you mean by put it inside. You could always make a new macro \newcommand\foo[1]{\linesinfile{#1}\lstinputlisting[firstline=n,lastline=\numexpr\linecount-m]{#1}}, if that's what you're asking.
    – TH.
    Dec 29, 2010 at 17:40
1

In case someone is interested in a similar result in ConTeXt:

\typefile[range={+5,-8}]{filename}

will remove the first five and the last eight lines.

0

You must log in to answer this question.

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