[Tex/LaTex] lstaddons: Dynamic linewidth calculation

backgroundscolorlistings

I use the package called "lstaddons" (creator, initial question) to get colored line backgrounds on listings.

I want to color the whole line to the frame.
What I miss there is a dynamic calculation of the linewidth.

You can adjust the linebackgroundsep, which moves the colorbox to the left.
And through linebackgroundwidth you can set the width of the box.

My question is:
Is there any opportunity to calculate the linewidth dynamic through a macro?
I tried this one, but i think its a bit crappy…

\newdimen\lstwidth
\newdimen\lstxleftmargin
\lstxleftmargin = 15pt
\lstwidth = 0pt
\advance\lstwidth by \linewidth
\advance\lstwidth by -\lstxleftmargin   % Minus xleftmargin
\advance\lstwidth by 1pt            % Minus Rand rechts

and in the listing settings:

\lstset{
    linebackgroundcolor={\ifodd\value{lstnumber}\color{codegray}\fi},
    linebackgroundsep=3pt,      % lstaddons: fills the line from the left frame
    linebackgroundwidth={\lstwidth}, % lstaddons: ... to the right
    frame=lr,
    numbersep=10pt,
    xleftmargin=\lstxleftmargin,
    xrightmargin=5pt
}

Best Answer

i just created an account now. Thanks for that very helpful answer! In fact the solution is: linebackgroundwidth={\dimexpr\linewidth+6pt\relax} because the linewidth is another inside the listings environment.

\lstset{
    % [...]
    linebackgroundcolor={\ifodd\value{lstnumber}\color{codegray}\fi},
    linebackgroundsep=3pt,
    linebackgroundwidth={\dimexpr\linewidth+6pt\relax}
    % [...]
}

This options result in the desired look. Thanks a lot!

(cannot upload the picture - sorry!)


edit no.1

Thanks also to Heiko Oberdiek here. The best solution in my eyes is now:

\lstset{
    linebackgroundcolor={\ifodd\value{lstnumber}\color{codegray}\fi},
    linebackgroundsep=3pt,
    linebackgroundwidth={\dimexpr\linewidth + (\lst@linebgrdsep)*2 \relax},
    frame=lr,
    xleftmargin=15pt,
    xrightmargin=5pt,
    framexleftmargin=0pt,
    framexrightmargin=0pt
}

the resulting listing

Related Question