TeX - LaTeX Asked on December 30, 2020
I’m typesetting code that includes lots of blank lines, like this:
documentclass{minimal}
usepackage{fancyvrb}
begin{document}
begin{Verbatim}
def xyz():
"""Some documentation.
More docs.
More docs."""
some code
more code
end{Verbatim}
end{document}
For compactness, I’d like to shrink the blank lines (left is before, right is after, red indicates blank lines):
Note that I don’t want to change the normal line spacing (the def
in the example above doesn’t get closer to the """
right below it).
I need fancyvrb
as the contents are actually generated using pygments. I tried reading through the definition of Verbatim
using latexdef
. It seems that it makes ^^M
active and that’s how it preserves newlines. I imagine I could redefine that to count how many newlines we’ve seen in a row, but I’m not sure how to check whether they were immediately consecutive — maybe an if@nextchar
?
As a first step, I tried adding markers manually to shrink the lines:
documentclass{minimal}
usepackage{fancyvrb}
begin{document}
begin{Verbatim}[commandchars={}]
def xyz():
"""Some documentation.
unskipvspace{-0.5baselineskip}
More docs.
unskipvspace{-0.5baselineskip}
More docs.
"""
unskipvspace{-0.5baselineskip}
some code
unskipvspace{-0.5baselineskip}
more code
end{Verbatim}
end{document}
But I got an error, and I can’t tell where it comes from:
ERROR: Missing number, treated as zero.
--- TeX said ---
<to be read again>
unhbox
l.8 unskipvspace{-0.5baselineskip}
How can I reduce the height of blank lines in Verbatim environments?
Try this:
documentclass{article}
usepackage{fancyvrb}
makeatletter
newifif@FV@emptyline
begingroup
catcode`^^M=active%
gdefFancyVerbGetLine#1^^M{%
@nil%
FV@CheckEnd{#1}%
%% modification begin
if@FV@emptyline
vskip-.5baselineskip
fi
%% modification end
ifx@tempaFV@EnvironName% % True if end is found
ifx@tempbFV@@@CheckEndelseFV@BadEndErrorfi%
letnextFV@EndScanning%
else%
defFV@Line{#1}%
%% modification begin
ifxFV@Line@empty
global@FV@emptylinetrue
else
global@FV@emptylinefalse
fi
%% modification end
defnext{FV@PreProcessLineFV@GetLine}%
fi%
next}%
endgroup
makeatother
begin{document}
begin{Verbatim}[numberblanklines=false,numbers=left]
def xyz():
"""Some documentation.
More docs.
More docs.
"""
some code
more code
end{Verbatim}
end{document}
Correct answer by muzimuzhi Z on December 30, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP