1

I think the title says it all, just look at this image in which is a common thing lawyers are doing around here.

The easiest way. No tables. No packages. If there is need for some, I can't complain though. It's just because I haven't seen a question quite like this one for TEX. The image is from Word and what the person basically did was to select the entire line and fill with a background colour fill.

Thank you.

2
  • Welcome to TeX.SX! Please note, you should always show us, what have tried. And if you don't have any idea, you should at least show a minimal working example (MWE) without the special formatting but that can be used to test our suggestions and show you a working solution. So please always help us to help you. However, TeX itself does not provide colors. So you need \colorbox of package color or xcolor. Sep 6, 2017 at 10:13
  • without using the packages either "color.sty" or "xcolor.sty", you can't meet this.
    – MadyYuvi
    Sep 6, 2017 at 12:20

4 Answers 4

2

Pretty easy with tcolorbox and same special settings, although this is a package and at least one package must be loaded to support colours.

\documentclass{article}

\usepackage{blindtext}
\usepackage[most]{tcolorbox}

\begin{document}
\blindtext
\begin{tcolorbox}[colback=gray!50,enhanced,sharp corners,frame hidden,halign=center]
HOW CAN I DO THIS IN \LaTeX{} THE SIMPLER WAY?
\end{tcolorbox}
\blindtext
\end{document}

enter image description here

1

You need at least a color package, either xcolor or color:

\documentclass{article}

\usepackage{xcolor}

\begin{document}
\noindent\colorbox{gray!50}{\strut\makebox[\dimexpr \linewidth-2\fboxsep][c]{HOW CAN I DO THIS
    IN \LaTeX{} THE SIMPLER WAY?}}
\end{document}

enter image description here

1

Here's a way with only xcolor:

\documentclass{article}

\usepackage[svgnames]{xcolor}

\begin{document}

{\large\rlap{\vspace{-0.95\baselineskip}\color{Gainsboro!80!Lavender}\rule[-0.3\baselineskip]{\linewidth}{1.2\baselineskip}}\noindent\makebox[\linewidth]{ HOW CAN I DO THIS
    IN \LaTeX{} THE SIMPLER WAY?}}
\bigskip

{\rlap{\vspace{-1\baselineskip}\color{Gainsboro!60!GhostWhite}\rule[-0.3\baselineskip]{\linewidth}{1.2\baselineskip}}\noindent\makebox[\linewidth]{ HOW CAN I DO THIS
    IN \LaTeX{} THE SIMPLER WAY?}}
\bigskip

{\rlap{\small\vspace{-1.06\baselineskip}\color{GhostWhite!80!Lavender}\rule[-0.3\baselineskip]{\linewidth}{1.2\baselineskip}}\noindent\makebox[\linewidth]{ HOW CAN I DO THIS
    IN \LaTeX{} THE SIMPLER WAY?}}

\end{document} 

enter image description here

4
  • Others have posted similar answer, but this answer looks like the closest thing I'm trying to reach, for one you were able to determine the text perfectly fit into the box similar to what the example showed.
    – Jason
    Sep 7, 2017 at 1:44
  • Bernard, what is the trick to align to that text to the left?
    – Jason
    Sep 7, 2017 at 5:42
  • @djzn: Do you mean you want the grey background textwidth-wide, and the text left-aligned? Just remove \makebox[\linewidth] from the code. If you want it right-aligned, add the [r] optional argument, like this: \makebox[\linewidth][r]{HOW CAN I …}
    – Bernard
    Sep 7, 2017 at 9:43
  • worked beautifully. I just added [l] to the command and a space before "H". It really looks GREAT! Thank you!
    – Jason
    Sep 7, 2017 at 22:00
0

You can use the xcolor package and boxes:

\noindent% prevent the box from being shifted
\colorbox{gray}{% create a colored box
    \makebox[\textwidth][c]{% center the text on the page
    \bf HOW CAN I DO THIS IN \LaTeX{} THE SIMPLER WAY?
    }
}

LaTeX/Colors - Wikibooks

LaTeX/Boxes - Wikibooks

Edit: See Schweinebacke's comments and answer to prevent overfull \hbox

1
  • You will get an overfull \hbox with this solution, because the \colorbox already consumes 2\fboxsep. So the whole box will have the width of \textwidth+2\fboxsep which is greater than \textwidth. Use the solution of my answer, that I've given 2 minutes before, to avoid this. Sep 6, 2017 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .