4

I am quite new with TeX so I am not sure how to solve the following correctly and it would be more than great to get some hints.

So I am trying to get a grey colorbox which spans over the whole page with some centered text inside of it but the colorbox should be bigger in height than text that it is containing, but the text should be vertically aligned in the box. So here's what I've got so far (image with [showframe] option):

enter image description here

\documentclass[a4paper,11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{times}
\usepackage{color}

\definecolor{lightgray}{gray}{0.75}

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

\begin{document}
  \greybox{Some Text here}
\end{document}
2
  • By "whole page", do you mean both vertically and horizontally?
    – Werner
    Commented Nov 9, 2011 at 21:30
  • Included a figure to clarify the problem. If this is not correct, please edit it. Commented Nov 9, 2011 at 21:47

3 Answers 3

4

What you have pretty much works, except that it overflows into the margin a bit. So with a minor tweak in the width of the minipage to \linewidth-2\fboxsep the width is properly set:

enter image description here

I added the [showframe] option to geomtry so that we can see where the margins are, and included the calc package.

\documentclass[a4paper,11pt]{article}
\usepackage[margin=2cm,showframe]{geometry}
\usepackage{times}
\usepackage{color}
\usepackage{calc}

\definecolor{lightgray}{gray}{0.75}

% greybox command definition
\newcommand\greybox[1]{%
  \vskip\baselineskip%
  \par\noindent\colorbox{lightgray}{%
    \begin{minipage}{\linewidth-2\fboxsep}\large\centering\textbf{#1}\end{minipage}%
  }%
  \vskip\baselineskip%
}


\begin{document}
  \greybox{Some Text here}
\end{document}
1
  • Great thanks so much for the quick answer, I didn't know until now about the showframe option that's very very handy, I always got an error because of the width and I didn't know where it came from !!! THANKS so MUCH !!!
    – latz
    Commented Nov 9, 2011 at 21:42
1

minipage has three optional arguments.

\begin{minipage}[<vertical position>][<height>}[<inner posistion]{<width>}

So you can modify your text.

\documentclass[a4paper,11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{times}
\usepackage{color}

\definecolor{lightgray}{gray}{0.75}

% greybox command definition
\newcommand\greybox[1]{%
  \vskip\baselineskip%
  \par\noindent\colorbox{lightgray}{%
    \begin{minipage}[c][4cm]{\dimexpr\textwidth-2\fboxsep\relax}\large\centering\textbf{#1}\end{minipage}%
  }%
  \vskip\baselineskip%
}


\begin{document}
  \greybox{Some Text here}
\end{document}

Another solution is by using the package mdframed:

\documentclass[a4paper,11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{mdframed}
\definecolor{lightgray}{gray}{0.75}

\usepackage{lipsum}
\begin{document}
\begin{mdframed}[linecolor=lightgray,backgroundcolor=lightgray,innerleftmargin=1cm,innerrightmargin=1cm,innertopmargin=.5cm,innerbottommargin=1cm]
\centering\lipsum[1]

Text
\end{mdframed}

\end{document}
1
  • Kay the solution with the options on the minipage helped me a lot, also a very neat solution with mdframed will have a look into that, THANKS A LOT!!!
    – latz
    Commented Nov 9, 2011 at 22:03
1

You can do this using the adjustbox package as shown below. This easily allows you to select the inner and outer spacing as well.

The following code adds a inner separation of 1ex and an outer margin of \medskipamount. Change it to your licking. I defined two versions, one for single line and another one for multiple lines of text. See the adjustbox package manual for more options like changing the frame thickness or color.

\documentclass{article}

\usepackage{adjustbox}
\usepackage{xcolor}

\newcommand{\graybox}{\noindent\adjustbox{center,margin={-2\fboxrule} 1ex,bgcolor=gray,frame,margin=0pt \medskipamount}}

\newcommand{\Graybox}{\noindent\adjustbox{minipage=[c]{\linewidth}\centering,margin={-2\fboxrule} 1ex,bgcolor=gray,frame,margin=0pt \medskipamount}}

\begin{document}

before

\graybox{Some Text Here}

middle

\Graybox{Some\\Text\\Here}

after

\end{document}

enter image description here

You must log in to answer this question.

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