7

I've got that code from a website to make grey areas in my LaTeX document.

% http://www.alfredklomp.com/programming/tex/macros/

\long\def\greybox#1{%
    \newbox\contentbox%
    \newbox\bkgdbox%
    \setbox\contentbox\hbox to \hsize{%
        \vtop{
            \kern\columnsep
            \hbox to \hsize{%
                \kern\columnsep%
                \advance\hsize by -2\columnsep%
                \setlength{\textwidth}{\hsize}%
                \vbox{
                    \parskip=\baselineskip
                    \parindent=0bp
                    #1
                }%
                \kern\columnsep%
            }%
            \kern\columnsep%
        }%
    }%
    \setbox\bkgdbox\vbox{
        \pdfliteral{0.85 0.85 0.85 rg}
        \hrule width  \wd\contentbox %
               height \ht\contentbox %
               depth  \dp\contentbox
        \pdfliteral{0 0 0 rg}
    }%
    \wd\bkgdbox=0bp%
    \vbox{\hbox to \hsize{\box\bkgdbox\box\contentbox}}%
    \vskip\baselineskip%
}

But it happens that use in conjunction with nested lists, the text is not correctly wrapped.

TeX Problem with area

I'm far a novice in TeX macro, so I would be interested in advices on if I can and how to modify it fits automatically to the page layout. Would a minipage solve this? Or am I bound to make it with manual line breaks?

lockstep
  • 250,273
M'vy
  • 402

2 Answers2

12

Using \colorbox from the color package instead would be better than a plain TeX macro:

\usepackage{color}
\definecolor{lightgray}{gray}{0.75}

\newcommand\greybox[1]{%
  \vskip\baselineskip%
  \par\noindent\colorbox{lightgray}{%
    \begin{minipage}{\textwidth}#1\end{minipage}%
  }%
  \vskip\baselineskip%
}

The result:

Result of \colorbox command

You
  • 6,713
  • 2
  • 29
  • 27
  • Do you have any idea how one can preserve indentation of theorems inside the \graybox you proposed? For my theorems I set an automatic indent before they begin, but your box seems to break/ignore this indent. – Richard Oct 16 '22 at 15:14
8

The solution doesn't allow any pagebreaks. You should use a package like framed or mdframed (new version is on the way ;-) I'm working on it. ):

\documentclass{article}
\usepackage[noframe]{showframe}
\usepackage{framed}
\usepackage{lipsum}
\renewenvironment{shaded}{%
  \def\FrameCommand{\fboxsep=\FrameSep \colorbox{shadecolor}}%
  \MakeFramed{\advance\hsize-\width \FrameRestore\FrameRestore}}%
 {\endMakeFramed}
\definecolor{shadecolor}{gray}{0.75}
\begin{document}
\lipsum[1]
\begin{shaded}
\lipsum
\end{shaded}
\lipsum[2]
\end{document}
DHaw
  • 3
  • 2
Marco Daniel
  • 95,681