TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...

\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:

    linebackgroundsep=3pt,      % lstaddons: fills the line from the left frame
    linebackgroundwidth={\lstwidth}, % lstaddons: ... to the right
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

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.

    % [...]
    % [...]

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:

    linebackgroundwidth={\dimexpr\linewidth + (\lst@linebgrdsep)*2 \relax},

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

Your Answer


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.