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 want to have one line of text with black background and the text in white. The line is alone and so it can be one paragraph.

I would like the coloration for the full width of the line and not only for the text.

The solution must work inside one minipage.

share|improve this question
    
Is the line going to be a separate paragraph, or do you want to colour a single line in a larger paragraph? –  Jake Dec 8 '11 at 12:06
    
Indeed, the line will be alone. –  projetmbc Dec 8 '11 at 12:07

2 Answers 2

up vote 3 down vote accepted

You can use a combination of \colorbox:

\documentclass{article}
\usepackage{xcolor}
\usepackage{showframe}
\usepackage{adjustbox}


\newcommand*\mylineI[1]{%
\par\noindent
\colorbox{black}{\makebox[\dimexpr\linewidth-2\fboxsep\relax][l]{\strut\color{white}#1}}%
\par}

\newcommand*\mylineII[1]{%
  \par\noindent\adjustbox{bgcolor=black,innercode={\color{white}}{},}%
                     {\makebox[\linewidth][l]{\strut #1}}%
  \par%
}
\begin{document}
\mylineI{Text Text Text Text}

\mylineII{Text Text Text Text}

\end{document}
\myline{Text Text Text Text}
\end{document}

EDIT: The second command uses the package adjustbox. So you have some extra options.

Of course the code can be extended width extra spacing and \xspace.

enter image description here

share|improve this answer
    
Thanks but this coloration is only for the text and not for the whole line... I've updated my question. –  projetmbc Dec 8 '11 at 12:16
    
@projetmbc: I edited my answer and now using makebox. –  Marco Daniel Dec 8 '11 at 12:25
    
Is there a way to minimize the spacing before the text ? –  projetmbc Dec 8 '11 at 12:30
    
@projetmbc: Do you mean the space inside the black line? I used \par to create a new line. You can manipulate this. –  Marco Daniel Dec 8 '11 at 12:36
    
Your second command looks better for my purpose. That's great ! –  projetmbc Dec 8 '11 at 13:17

How about this?

\colorbox{black}{%
\begin{minipage}[b]{\dimexpr\textwidth-2\fboxsep\relax}
\strut\color{white}hello
\end{minipage}%
}

enter image description here

A new environment to do the equivalent is

\setlength\fboxsep{0pt}
\makeatletter\newenvironment{blackbox}
    {\noindent\begin{lrbox}{\@tempboxa}\begin{minipage}[b]{\dimexpr\textwidth-2\fboxsep\relax}\strut\color{white}}
    {\end{minipage}\end{lrbox}\colorbox{black}{\usebox{\@tempboxa}}}
\makeatother

which will give the same result with

\begin{blackbox}
hello
\end{blackbox}

Note that this leads to an overfull box if unless \setlength\fboxsep{0pt} is used before it. With that, the output looks like

enter image description here

share|improve this answer
    
Sorry, I've forgotten to indicate that I'll use this formatting in one minipage... –  projetmbc Dec 8 '11 at 12:31
    
@MarkS.Everitt: You forgot some % and width \textwidth you created an overfull box. You must substrate the \fboxsep of \colorbox. –  Marco Daniel Dec 8 '11 at 12:37
    
@MarcoDaniel: Yeah, noticed that, and fixed now. –  qubyte Dec 8 '11 at 12:42
    
Eek, no. Still overfull... –  qubyte Dec 8 '11 at 12:50
    
@MarkS.Everitt: Please see my answer (\dimexpr). Your second code uses a savebox. So you allows breaks. –  Marco Daniel Dec 8 '11 at 12:55

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.