2

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
}
1
  • You could do this in one swoop: linebackgroundwidth=\dimexpr\linewidth-15pt+1pt\relax. Not sure whether 1pt is appropriate here, since frame borders are "usually" .4pt. Without a proper MWE it's hard to tell.
    – Werner
    Commented Sep 9, 2012 at 14:06

1 Answer 1

2

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

3
  • Is it also your account that is asking the question ? If so we can ask a mod to merge these accounts.
    – percusse
    Commented Sep 9, 2012 at 14:43
  • Yes it was, sorry for all this trouble :/
    – tollo
    Commented Sep 9, 2012 at 14:48
  • @percusse and tollo: I merged the accounts.
    – Stefan Kottwitz
    Commented Sep 9, 2012 at 15:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .