fvextra icon indicating copy to clipboard operation
fvextra copied to clipboard

demo of new feature: alternate line color

Open muzimuzhi opened this issue 6 years ago • 3 comments

The purpose of this feature is to let odd- and even lines have different highlight colors. I have committed a demo implementation to my forked repo. Only sty file is changed in that commit.

Some Explanations

  1. Now highlightcolor can be used as highlightcolor = SingleColor | {<OddLineColor>, <EvenLineColor>}.
  2. New option highlightlinenumbercolor changes the highlight color of line number, and this option can be used the same as highlightcolor.
  3. Since calculating the max width of actual line number (\arabic{FancyVerbLine}) is hard, a new option linenumberwidth=<dimen> is defined.
  4. If breaklines=true, the total heigh of a line is not determined until \FV@SaveLineBox. Therefore \FV@LeftListNumber (with its background) is typeset after \FancyVerbFormatLine.

Shortcut:

  1. \def\FV@LineNumberBackground{} is a compromise since \def\FV@LineNumberBackground\@empty and \def\FV@LineNumberBackground\relax both raise error. I guess it is a problem of order of macro expanding.
  2. The \pickTwo macro may have nicer implementation.

I am not fully understood about packages fancyvrb and fvextra, so any suggestions are helpful. And, more test is needed of course.

muzimuzhi avatar Aug 09 '18 08:08 muzimuzhi

Here are some simple examples:

\documentclass{article}
\usepackage{fvextra}
\usepackage[x11names]{xcolor}

\fvset{
    highlightcolor={LightBlue1!50, LightBlue1!30},
    highlightlinenumbercolor={yellow!50, yellow!30},
    numbersep=5pt
}

\begin{document}

\begin{Verbatim}[linenos, numbers=right, breaklines=true]
\def\FV@HighlightLine#1{%
\@tempcnta=\c@FancyVerbLine
\@tempcntb=\c@FancyVerbLine
\ifcsname FV@HighlightLine:\number\@tempcnta\endcsname %\@tempcnta\endcsname
\advance\@tempcntb\m@ne % some some some some some some some comments
\ifcsname FV@HighlightLine:\number\@tempcntb\endcsname
\advance\@tempcntb\tw@
\end{Verbatim}

\bigskip

\begin{Verbatim}[linenos, numbers=left, breaklines=false]
\def\FV@HighlightLine#1{%
\@tempcnta=\c@FancyVerbLine
\@tempcntb=\c@FancyVerbLine
\ifcsname FV@HighlightLine:\number\@tempcnta\endcsname
\advance\@tempcntb\m@ne
\ifcsname FV@HighlightLine:\number\@tempcntb\endcsname
\advance\@tempcntb\tw@
\end{Verbatim}

\bigskip

\begin{Verbatim}[linenos, numbers=both, breaklines=false, frame=single]
\def\FV@HighlightLine#1{%
\@tempcnta=\c@FancyVerbLine
\@tempcntb=\c@FancyVerbLine
\ifcsname FV@HighlightLine:\number\@tempcnta\endcsname
\advance\@tempcntb\m@ne
\ifcsname FV@HighlightLine:\number\@tempcntb\endcsname
\advance\@tempcntb\tw@
\end{Verbatim}

\end{document}

image

muzimuzhi avatar Aug 09 '18 08:08 muzimuzhi

This looks like a very nice feature and I'll be happy to include it. I will need to think a little about the best way to do some things. Your \FancyVerbHighlightLineBase is definitely going in the right direction in terms of sharing core line configuration. I'll need to think more about exactly how it relates to \FancyVerbHighlightLineFirst, \FancyVerbHighlightLineMiddle, \FancyVerbHighlightLineLast, etc. Unfortunately, I have a lot of other projects going at the moment, so it may be a while before I can get back to this.

gpoore avatar Aug 12 '18 01:08 gpoore

Thanks so much for your response.

Before you getting back, if I get new ideas related to this feature, I will post them here as well.

muzimuzhi avatar Aug 12 '18 06:08 muzimuzhi