3

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.

projetmbc
  • 13,315

2 Answers2

3

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

Marco Daniel
  • 95,681
1

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

Marco Daniel
  • 95,681
qubyte
  • 17,299