2

I would like to create a custom environment that lets me write text into a gray box. Basically this:

\begin{center}
\noindent\fcolorbox{black}{gray!30}{%
    \parbox{33em}{%
        \vspace{15pt}
         text
        \vspace{15pt}
    }%
}
\end{center}

but with just one \begin and \end statement.

2
  • 2
    Welcome to TeX.SE. With just one \begin and \end statement? What should that mean? \begin{foo}...\end{foo}? I think mdframed, tcolorbox and other packages can do this 'out-of-the-box' (pun intended!)
    – user31729
    Commented Apr 20, 2017 at 19:31
  • Yes. Just \begin{mybox} text inside the box \end{mybox} for example
    – YvesCB
    Commented Apr 20, 2017 at 19:33

3 Answers 3

4

Here an example using mdframed, showing some of the numerous options to configure such a box environment.

\documentclass{article}
\usepackage{mdframed}
\usepackage{xcolor}
\begin{document}

\newmdenv[linecolor=black, leftmargin=2em,rightmargin=2em,innertopmargin=2em,innerbottommargin=2em]{infobox}

\begin{infobox}[backgroundcolor={lightgray}]
foo foo foo
\end{infobox}

\end{document}

enter image description here

5

You can create your own environment using

\newenvironment{name}{begin code}{end code}

and then use it as

\begin{name}
...
\end{name}

This does not work in your case, since it is not possible to split pairs of braces and put the opening one into begin code and the closing one into end code. In such cases the package environ helps with its \NewEnviron. It puts everything between \begin{name} and \end{name} into \BODY, which then can be used in the code defining the environment.

\documentclass{article}
\usepackage{xcolor}
\usepackage{environ}
\NewEnviron{graybox}%
  {\begin{center}%
   \noindent\fcolorbox{black}{gray!30}%
     {\parbox{33em}%
       {\vspace{15pt}\par
        \BODY
        \vspace{15pt}\par
       }%
     }%
   \end{center}%
  }
\begin{document}
\begin{graybox}
text
\end{graybox}
\end{document}

enter image description here

0
3
\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcolorbox{mybox}[1][]{%
  %
  enhanced jigsaw,
  width=33em, % Width of the box
  boxsep=0pt, % No extra spacing
  top=15pt, % top spacing
  bottom=15pt, % bottom spacing
  sharp corners,
  colframe={black},
  colback={gray!30},
  #1
}
\begin{document}

\begin{mybox}
  And now for something completely different
\end{mybox}

\begin{mybox}[colback=green, colframe=blue,boxrule=2pt]
  And now for something completely different and nicer!
\end{mybox}


\end{document}

enter image description here

6
  • that green would definitely look at home on a tropical beetle. Commented Apr 20, 2017 at 19:56
  • 2
    Well the 'nicer' is definitely arguable.
    – Skillmon
    Commented Apr 20, 2017 at 19:58
  • @barbarabeeton: I want to provoke ;-)
    – user31729
    Commented Apr 20, 2017 at 20:00
  • @Skillmon: Ok, lets argue....
    – user31729
    Commented Apr 20, 2017 at 20:00
  • 1
    First: It does produce eye-cancer imho :) And the green is way too shiny to concentrate on the text.
    – Skillmon
    Commented Apr 20, 2017 at 20:02

You must log in to answer this question.

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