[texhax] space between colorboxes in adjacent lines

Axel Freyn axel-freyn at gmx.de
Tue Apr 27 17:24:51 CEST 2010


Hi José
On Tue, Apr 27, 2010 at 11:14:34AM -0300, José Romildo Malaquias wrote:
> I want to set the background color of a set of consecutive lines. I am
> using the \colorbox command from the color LaTeX package. 
> 
>   \documentclass[a5paper]{article}
>   \usepackage{color}
> 
>   \newcommand{\cbox}[2]{%
>     \colorbox{#1}{\makebox[\linewidth][l]{#2}}}
> 
>   \begin{document}
> 
>   \noindent
>   \cbox{yellow}{First~line}\newline
>   \cbox{yellow}{Second line}\newline
>   \cbox{yellow}{}\newline
>   \cbox{yellow}{Fourth line}
> 
>   \end{document}
> 
> The result of running pdflatex with this document is attached.
> 
> The problem I want to solve is the small blank space between each
> line. How can it be eliminated?
The easiest (and less cleanest;-)) is probably to do it "by hand", by
replacing "\newline" by "\\[-1pt]" (this reduces the linespacing by
1pt - than it works correctly, except for the empty line, where the
colorbox is not high enough. To correct that, you could write
   \newcommand{\cbox}[2]{%
     \colorbox{#1}{\makebox[\linewidth][l]{\vphantom{F}#2}}}
in order to guarantee that the box has the necessary height:

  \documentclass[a5paper]{article}
  \usepackage{color}
  \newcommand{\cbox}[2]{%
    \colorbox{#1}{\makebox[\linewidth][l]{\vphantom{F}#2}}}
  \begin{document}
  \noindent
  \cbox{yellow}{First~line}\\[-1pt]
  \cbox{yellow}{Second line}\\[-1pt]
  \cbox{yellow}{}\\[-1pt]
  \cbox{yellow}{Fourth line}
  \end{document}

works for me. Of course, you could include the "\\[-1pt]" as last
element into your \cbox-command.

However, you will have a problem when there are paragraphs in the text:
there, LaTeX adds an additional vertical space (\parskip) - you will
have to treat that separately when it appears in your text?

Axel


More information about the texhax mailing list