6

I'd like to display a grey rectangle behind text, so that certain parts of the text are more recognisable. I will do this to highlight problems' body.

I found various packages, like fancytooltips or tcolorbox but I feel they're too complicated for what I'm trying to achieve.

Ideally it would be a simple grey rectangle, just like what you get when you add a quote on Math.SE:

box

The special left border isn't even necessary. What's the simplest way to achieve this?

0

3 Answers 3

8

A solution with the shaded environment of the package framed:

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{framed}

\definecolor{shadecolor}{named}{LightGray}

\begin{document}
    Wikipedia claims
    \begin{shaded}
        \noindent The fundamental theorem of a field of mathematics is the theorem considered central to that field. The naming of such a theorem is not necessarily based on how often it is used or the difficulty of its proofs.
    \end{shaded}
    So I am left wondering: what \emph{is} the main criterion for a theorem to be considered fundamental?
\end{document}

enter image description here

Another example with a theorem environment inside:

\documentclass{article}
\usepackage{amsthm}
\usepackage[svgnames]{xcolor}
\usepackage{framed}

\theoremstyle{definition}
\newtheorem{mythm}{Theorem}
\definecolor{shadecolor}{named}{LightGray}

\begin{document}
    Wikipedia claims
    \begin{shaded}
        \begin{mythm}
            The fundamental theorem of a field of mathematics is the theorem considered central to that field. The naming of such a theorem is not necessarily based on how often it is used or the difficulty of its proofs.
        \end{mythm}
    \end{shaded}
    So I am left wondering: what \emph{is} the main criterion for a theorem to be considered fundamental?
\end{document}

enter image description here

2
  • 1
    This framed package is awesome. I think I'll be doing something along the lines of \definecolor{shadecolor}{gray}{0.9}. I especially like the fact that the box exceeds the lateral margins a bit.
    – rubik
    Feb 24, 2015 at 14:16
  • 2
    I got the following error: Package xcolor Error: Undefined color LightGray'.`
    – AgilePro
    May 18, 2017 at 14:28
10

Same as David's but more elaborate and with a picture ;-)

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\newcommand{\highlight}[1]{%
 \par\noindent
 \colorbox{gray!30}{%
 \parbox{\dimexpr\linewidth-2\fboxsep\relax}{%
 #1
 }%
 }}

\begin{document}
  \lipsum[1]
  \highlight{\lipsum*[1]}
\end{document}

enter image description here

But I don't agree with you in that tcolorbox is complicated. See the following for example.

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{lipsum}
\newtcolorbox{mybox}{colback=gray!30,
boxrule=0pt,arc=0pt,boxsep=2pt,left=2pt,right=2pt,leftrule=1pt}

\begin{document}
  \lipsum[1]
  \begin{mybox}
    \lipsum*[1]
  \end{mybox}
\end{document}

enter image description here

1
  • Yeah, you're right, tcolorbox is not that complicated, now that I see your proposal.
    – rubik
    Feb 24, 2015 at 14:14
4

color package and

\colorbox[gray]{0.5}{\parbox{.7\textwidth}{The fundamental......}}
2
  • What made you solve this case so easy? Simple and sufficient! A miracle flies over your head!
    – Mikasa
    Jul 2, 2023 at 13:24
  • 1
    @Mikasa I did write the latex color support:-) Jul 2, 2023 at 13:25

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