I want to

  1. make reference to lines in Verbatim environment provided by fancyvrb pacakge, and
  2. make the reference a hyperlink which jumps to the right position where corresponding label is defined, with the help of hyperref

If simply use fancyvrb with hyperref, these references all jump to hyperref's named destination Doc-Start (use the following MWE). Thus I modified \FV@refstepcounter to directly use \steprefcounter.

This modification works on the label of first and second line of Verbatim env. But, from the third line, \ref{vrb:n} jumps to the position of \label{vrb:n-1} (use the following MWE).


%  \PackageError{FV}{pre step: \the\c@FancyVerbLine, \@currentHref}{}%
%  \PackageError{FV}{post step: \the\c@FancyVerbLine, \@currentHref}{}%

%% original def
%  \stepcounter{#1}
%  \protected@edef\@currentlabel{\csname p@#1\endcsname\arabic{FancyVerbLine}}%

1. For n = 1 or 2, \ref{vrb:n} jumps to destination \label{n}.
2. For n >= 3, \ref{vrb:n} jumps to destination \label{n - 1}.

  first   \label{vrb:1}
  second  \label{vrb:2}
  third   \label{vrb:3}
  forth   \label{vrb:4}
  fifth   \label{vrb:5}
  sixth   \label{vrb:6}

\ref{vrb:1}, \ref{vrb:2}, \ref{vrb:3}, \ref{vrb:4}, \ref{vrb:5}, and \ref{vrb:6}

output of mwe

This problem is re-producible with either pdflatex, lualatex or xelatex contained in texlive 2019. And I have latest fancyvrb and hyperref installed.

What I have investigated

  1. .aux shows that every label creates distinct hyper target, e.g., \label{vrb:3} creates \newlabel{vrb:3}{...}{1}{}{FancyVerbLine.3}{}.
  2. Contents of output PDF shows that the coordinates of named destination of FancyVerbLine.2 and FancyVerbLine.3 are the same, which causes the jump-to-wrong-position problem.
  3. Un-comment the lines of \PackageError will show that the counter stepping is only taken inside \FV@refstepcounter.
  4. Further more, listings and hyperref works ok.

% use example in `texdoc listings`, sec. 8
for i:=maxint to 0 do(*@\label{lst:1}@*)
begin                (*@\label{lst:2}@*)
{ comment }          (*@\label{lst:3}@*)
\ref{lst:1}, \ref{lst:2} and \ref{lst:3}

This question is related about fancyvrb only. hyperref makes the "internal info" obvious, but the info is unchanged. The key info that the mwe in question provides (and I am still confusing about) is,

  • the ypos of "FancyVerbLine.2" is always 10pt lower than that of "FancyVerbLine.1" (no matter how much the font size is set outside Verbatim env), and
  • ypos of "FancyVerbLine.3" is always the same as "FancyVerbLine.2"

By moving \FV@StepLineNo from the early \FV@@PreProcessLine to the very late command \FV@ListProcessLine (which does the actual output line-by-line), this problems seems solved. I am not sure if this patch would cause any new problems.






\xlink{1}, \xlink{2}, \xlink{3}, \xlink{4}, \xlink{5}, \xlink{6}

Some explanations, or personal understanding:

  1. hyperref redefined \refstepcounter to

    1. update \@currentHref (which stores FancyVerbLine.<n> in the mwe) and
    2. write named destination to PDF (more general speaking, the driver).
  2. When named destinations are written, indirect coordinates are used. For xetex + (x)dvipdfmx, @xpos and @ypos are used (instead of direct coordinate numbers), which are inherited from dvipdfm.
  3. Hence, the problem is, \refstepcounter{FancyVerbLine} is called at a wrong position. One can check this with the savepos module of zref package, and without loading of hyperref. (see the following example)
  4. fancyvrb uses \FV@StepLineNo as a wrapper of \refstepcounter, in order to support options like numberblanklines
% !TeX program = pdflatex


  \PackageError{FV}{#1, line: \the\c@FancyVerbLine, ref \the\c@zRef, ypos \x}{}%

  \showYPos[before step]%  ypos is wrong steped

  \hbox to \hsize{%  inside this hbox, ypos is ok
%    \showYPos[inside hbox]%  ypos is ok
    \hbox to \linewidth{%
%  \showYPos[outside hbox]%  ypos is wrong steped


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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