23

In the below link

http://www.math.uiowa.edu/~goodman/algebrabook.dir/bookmt.pdf

the author highlights anything which is important using a greyish color background. For example, if you see Equation 1.5.3 (which on page 22 of the book) it is highlighted in grey. I would like to know the TeX commands for producing similar output.

cmhughes
  • 100,947
  • 5
    check out the mdframed package. –  Mar 05 '12 at 03:53
  • Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. – Tobi Mar 05 '12 at 04:03
  • 2
    In addition to Harish’s comment I recommend to define commands and environments for those thing you want to highlight. So all of them look the same but can be change easily afterword if necessary. After checking mdframed you may post a minimal working example (MWE) to show, what you’ve tried so far … – Tobi Mar 05 '12 at 04:06

2 Answers2

22

As mentioned in the comments, mdframed is perfect for this. You can combine it with your favourite theorem package too- in the MWE below I've used it with ntheorem, but there's nothing to stop you from using amsthm or something similar.

enter image description here

This example is very simple, but you can get very sophisticated using tikz or pstricks as the engine, as shown in the excellent documentation

\documentclass{article}

\usepackage{xcolor}     % for colour
\usepackage{lipsum}     % for sample text
\usepackage{ntheorem}   % for theorem-like environments
\usepackage{mdframed}   % for framing

\theoremstyle{break}
\theoremheaderfont{\bfseries}
\newmdtheoremenv[%
linecolor=gray,leftmargin=60,%
rightmargin=40,
backgroundcolor=gray!40,%
innertopmargin=0pt,%
ntheorem]{myprop}{Proposition}[section]

\begin{document}

\section{mdframed for the win}
\begin{myprop}
\lipsum[1]
\end{myprop}

\end{document}
cmhughes
  • 100,947
20

With tcolorbox:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{kantlipsum}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[breakable, theorems, skins]{tcolorbox}
\tcbset{enhanced}

\DeclareRobustCommand{\mybox}[2][gray!20]{%
\begin{tcolorbox}[   %% Adjust the following parameters at will.
        breakable,
        left=0pt,
        right=0pt,
        top=0pt,
        bottom=0pt,
        colback=#1,
        colframe=#1,
        width=\dimexpr\textwidth\relax, 
        enlarge left by=0mm,
        boxsep=5pt,
        arc=0pt,outer arc=0pt,
        ]
        #2
\end{tcolorbox}
}

\begin{document}
\mybox[green!20]{\kant[1]}
\mybox{\kant[2]}
\mybox[red!20!white]{\kant}

\end{document}

enter image description here

tcolorbox integrates flawlessly with theorem packages and math. Further, it can produce more fancier boxes than one can imagine. I feel that this is the best option to adopt when one has to highlight text / math / theorems. Though I have defined a command, new environments can also be defined with tcolorbox. For details, texdoc tcolorbox or visit www.texdoc.net.

A sample preposition:

\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{kantlipsum}

\newtcbtheorem{Proposition}{Proposition}%
{breakable,colframe=red!50!white!50!black,fonttitle=\bfseries}{}

\begin{document}
\begin{Proposition}{This is my proposition}{}
  \kant
\end{Proposition}
\end{document}

Earlier attempt:

Sorry for joining late. I would like to add to cmhughes's excellent answer. Though his reply is already accepted as the answer, I wish to share mine. My method is simple and direct where colors can be changed very easily.

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{lipsum}
\newcommand*{\mybox}[2]{\colorbox{#1!30}{\parbox{.98\linewidth}{#2}}}
\begin{document}
%=====================
\section{First section}
%=====================
\mybox{green}{\lipsum[1]}
\par
\noindent\mybox{red}{\lipsum[2]}
\end{document}

enter image description here

  • 2
    this won't break across pages though... – cmhughes Mar 05 '12 at 16:58
  • @cmhughes Correct. But the file provided above by the OP does not require breaking across pages. It is for just highlighting small bits of text (like a line or two. I also go with mdframed for things like these. (as I commented above) My answer is added just for fun. –  Mar 05 '12 at 22:52
  • @HarishKumar:Very nice way of doing it! – Thanos Oct 07 '12 at 11:56
  • What if I would like to add other text than \kant[2]? Putting simple text there, or sections, results in an error. – zabop Apr 13 '21 at 18:25