9

When I input a listing with \lstinputlisting from the listings package, I get an additional vertical space, when I use this in combination with the hyperref package. As the picture shows, a certain amount of space is added to the listing.

The output is: enter image description here

And the code for this is:

MWE without hyperref:

\documentclass{article}

\usepackage{filecontents}
\usepackage{listings}
%\usepackage{hyperref}

\begin{document}

\begin{filecontents*}{\jobname.temp}
Line of code
\end{filecontents*}

Without hyperref:

\fboxsep=0pt\fbox{\lstinputlisting[nolol]{\jobname.temp}}

\end{document}

MWE with hyperref:

\documentclass{article}

\usepackage{filecontents}
\usepackage{listings}
\usepackage{hyperref}

\begin{document}

\begin{filecontents*}{\jobname.temp}
Line of code
\end{filecontents*}

With hyperref:

\fboxsep=0pt\fbox{\lstinputlisting[nolol]{\jobname.temp}}

\end{document}

I played around a lot with settings and even altering the listings package code a bit, but I was'nt able to figure out how to get rid of this space. I want the same output, if hyperref is loaded or not using the same source code.

3

Package hyperref has to put the anchors (link targets) somewhere. The inserted whatsit nodes cannot be made entirely invisible.

At least there is a workaround for this case. It moves the destination setting outside the \fbox in vertical mode before. Then it disables the caption stuff of listings:

\documentclass{article}

\usepackage{filecontents}
\usepackage{listings}

\usepackage{hyperref}

\begin{document}

\begin{filecontents*}{\jobname.temp}
Line of code
\end{filecontents*}

With hyperref:

\begingroup
  \makeatletter
  \lst@HRefStepCounter{lstlisting}%
  \let\lst@MakeCaption\@gobble
  \setlength{\fboxsep}{0pt}%
  \fbox{%
    \lstinputlisting[nolol]{\jobname.temp}%
  }%
\endgroup

\end{document}

Result

  • I was playing around before with somehow deactivating \lst@MakeCaption but I always got errors with a real-world example. As far as I can see now, your code runs perfectly without any error for a huge example file! Thank you very much! :-) – Thomas F. Sturm Jul 22 '13 at 15:02
  • @Heiko Oberdiek - the way I see he is just using a simple example for a much larger problem -- that is -- the layout changes up loading hyperrref even if you do NOT use it. Of course, one cannot chase the problems one by one, so what is the correct paradigm to follow when you want to produce a file for screen and print (links and no links) from the same sources? – Paulo Ney Feb 4 '17 at 2:29
  • @PauloNey There is not "the correct paradigm", there are many, many of more or less correct ways. – Heiko Oberdiek Feb 4 '17 at 8:05
  • @Heiko Oberdiek Would you care to describe them to us? Probably expanding the answer, since this question is extremely important. I would like to understand "why" is it necessary for Hyperref to change the layout, and then how to avoid it on a dual-production system. – Paulo Ney Feb 4 '17 at 20:27
  • @PauloNey Please, reread the answer, the first paragraph already gives hints for the "why". A full comprehensive answer would require an understanding of TeX's paragraph builder, TeX's page breaking, whatsits (e.g. \special), how the hyper features can be implemented with the different drivers, ... This would fill several chapters of a book. A comment is the wrong place for this. A best way to proceed depends very much on the circumstances (one document vs. two documents, features used, TeX expertise, ...). – Heiko Oberdiek Feb 4 '17 at 21:17

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.