Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to implement two new key-value pairs for the fancyvrb package: head= and tail= specify how many lines to display: That is, head=10, tail=5 specifies to print the first 10 lines of the environment and last 5 lines.

(I know about the firstline= and lastline= options already available, but they are used to specify a 'middle' portion of the environment, which is a different problem.)

Getting the head= value to work isn't easy, but I think I understand how it can be done.

The tail= value I'm having trouble with. In order to display the tail portion, I need to know where the tail portion starts, which presumes I know how many lines are in the environment to begin with.

Is there a method for that?

share|improve this question
    
You would need to store the verbatim lines in some form and then discard the last lines after reaching the end of the environment. Alternatively count the number of lines in the first compiler run, write that number in the aux file and use it in the next run. –  Martin Scharrer Oct 1 '11 at 17:45
    
What engine are you using? PDFTeX, XeTeX or LuaTeX? –  topskip Oct 1 '11 at 19:33
    
@MartinScharrer I thought about writing to the aux file, but I'm not sure what you mean with your first suggestion. Is there a way to read and store the environment, count its lines, and then reparse? –  Tim A Oct 1 '11 at 20:13
    
@Patrick, I should have mentioned that, I'm using pdflatex. Probably LuaTeX would make this easier, but I need to use pdflatex for a production system. –  Tim A Oct 1 '11 at 20:13
    
@Tim did you try to compile your document with LuaTeX instead of PDFTeX? Sometimes this doesn't make any difference. Depends on the encoding of the file, though. –  topskip Oct 2 '11 at 6:55
add comment

1 Answer

up vote 1 down vote accepted

I see two ways to accomplish this task.

First method. Save the verbatim text into a temporary file and then load it. Proof of concept

\documentclass[a4paper]{article}
\usepackage{fancyvrb}

\makeatletter
\newenvironment{tail}[1]
  {\def\@tailnumber{#1}%
   \VerbatimOut{tailtemp.tmp}}
  {\endVerbatimOut
   \VerbatimInput[firstline=\number\numexpr\FV@CodeLineNo-\@tailnumber+1\relax]{tailtemp.tmp}}
\makeatother

\begin{document}
\begin{tail}{2}
a
b
c
\end{tail}

Second method. This assumes that the verbatim lines have all the same height. Save the processed verbatim environment in a \vbox and then split it using \vsplit.

share|improve this answer
    
Thanks, I think I see how to go forward from here, using your first method. Just fyi, I also found this thread: tex.stackexchange.com/questions/5870/… that has a more complicated solution. –  Tim A Oct 3 '11 at 23:32
    
There is one question I have about your second method. All the verbatim lines should have the same height and depth, but what can I use to vsplit by? Is there a command for lineheight or should I create a new vbox{\\ttfamily abcdefg} and use that dimen? –  Tim A Oct 3 '11 at 23:44
    
Basically, you can measure the height of the \vbox and divide by the baseline skip. Then you know how many lines there are and you can chop off with \vsplit the unwanted ones. –  egreg Oct 3 '11 at 23:47
add comment

Your Answer

 
discard

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.