Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
}
share|improve this question
    
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 Sep 9 '12 at 14:06
add comment

1 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

share|improve this answer
    
Is it also your account that is asking the question ? If so we can ask a mod to merge these accounts. –  percusse Sep 9 '12 at 14:43
    
Yes it was, sorry for all this trouble :/ –  tollo Sep 9 '12 at 14:48
    
@percusse and tollo: I merged the accounts. –  Stefan Kottwitz Sep 9 '12 at 15:02
    
Danke/thank you Stefan –  tollo Sep 9 '12 at 15:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.