Page LayoutVariable size box with background colour

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
andysmith87
Posts: 1
Joined: Fri Mar 04, 2011 7:34 am

Variable size box with background colour

Post by andysmith87 »

I've searched a few forums to determine code that will make an environment that will make the background of the encapsulated content a specific colour. This is what I have:

Code: Select all

\definecolor{MyGray}{rgb}{0.9,0.9,0.9}
\makeatletter\newenvironment{graybox}{%
   \paragraph{}\begin{lrbox}{\@tempboxa}\begin{minipage}{15.9cm}}{\end{minipage}\end{lrbox}%
   \colorbox{MyGray}{\usebox{\@tempboxa}}\paragraph{}
}\makeatother
However, that 15.9cm needs to be variable, as if it's in a subsection (or indented for some other reason), that width is longer than the width of the (part where the text can fit on the) page, and hence, I get an overfull horizontal box.

I was wondering if there's a code (or some completely different way) to do this??

Thanks for your help!
Andrew

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Variable size box with background colour

Post by localghost »

Probably you are searching for a feature provided by the varwidth package.


Best regards and welcome to the board
Thorsten
Post Reply