# Change background color across different environments

I have a long .tex document and I want to change the background color of the parts I have already checked for typos to a light green. I am looking for an easy command that can be used like

\HIGHLIGHT{

<here I want to put a part of my tex file that contains different environments like theorems, figures...>

}


But the \hl{} command cannot contain other environments, the following example does not work:

\documentclass[10pt]{article}
\usepackage[english]{babel}
\usepackage{amssymb,amsmath,amsthm,amsfonts}
\usepackage{xcolor,soul}

\newtheorem{theorem}{Theorem}
\begin{document}
\section{Introduction}
\hl{
\begin{theorem}
A theorem.
\end{theorem}
}
\end{document}


Do you have any suggestions which command could be used here (the typesetting shall not be changed, only the color)?

• In my opinion you should use tcolorbox and using it's theorem features, as such you can easily change the background colour
– user31729
Jan 21, 2017 at 21:40
• @ChristianHupfer But can I also avoid that the typesetting is changed? For example LaTeX tries to align the beginning of the tcolorbox with a new page. Jan 22, 2017 at 9:28
• By default tcolorbox does not set the boxes at the top of a new page.
– user31729
Jan 22, 2017 at 9:30
• But for a real clean interface you are likely to change your code -- with \hl{...} statements you would have to do that anyway
– user31729
Jan 22, 2017 at 9:36
• @ChristianHupfer The title may need an amendment: "the Counter and TCB Wizard" i.stack.imgur.com/FBQgC.png Sep 16, 2017 at 20:08

As suggested in one of my comments to the question, I would use tcolorbox and its \newtcbtheorem macros, having two basically identical environments, where one uses the checkedstyle and the other one the uncheckedstyle. If a theorem is not checked yet, use \begin{uctheorem}...\end{uctheorem} and edit it later to \begin{theorem}...\end{theorem} or just remove the colback={green!50!white} statement from the definition of uncheckedstyle.

In the very end, a search-and-replace of all uctheorem occurrences by theorem would be the best way, in my point of view, but that is a matter of taste, of course.

Another option is to use a 'real' tcolorbox environment which allows local specification of options, which is not possible with the tcbtheorem environments.

\documentclass[10pt]{article}
\usepackage[english]{babel}
\usepackage{amssymb,amsmath,amsthm,amsfonts}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}

\tcbset{%
checkedstyle/.style={breakable,enhanced, sharp corners,colback={yellow!50!white}},
uncheckedstyle/.style={checkedstyle,colback={green!50!white}}
}

\newtcbtheorem{theorem}{Theorem}{checkedstyle}{theo}

\newtcbtheorem{uctheorem}{Theorem}{uncheckedstyle}{theo}

\begin{document}
\section{Introduction}
\begin{theorem}{My theorem}{foo}
A theorem.
\end{theorem}

\begin{uctheorem}{My other theorem}{foobar}
An unchecked theorem
\end{uctheorem}

\end{document}


As an alternative that should work well for your use-case of highlighting arbitrary parts of a LaTeX document, an interesting approach is to partially color the page background using the color and afterpage packages (based on https://tex.stackexchange.com/a/237427/223749). This has the advantage that you do not need to change your content at all and formatting will not be affected. The highlighting will only cover full lines and include the left and right margins as well, but I guess that is not an issue for your use-case. Also, if there are floats inside your highlighting environment and they float out of it, they will not be highlighted.

MWE, including a theorem, a figure and a page break:

\documentclass{article}

\usepackage{mwe}

\usepackage{amssymb,amsmath,amsthm,amsfonts}
\newtheorem{theorem}{Theorem}

\usepackage{color}
\definecolor{lightgreen}{rgb}{0.56, 0.93, 0.56}

\usepackage{afterpage}

\makeatletter
% Macro \changepagecolor has the same syntax as \pagecolor or \color
% with an optional argument and a mandatory argument.
\newcommand*{\changepagecolor}{%
\@ifnextchar[\@changepagecolor@i\@changepagecolor@ii
}
% Case: \changepagecolor[...]{...}
\def\@changepagecolor@i[#1]#2{%
\@changepagecolor@do{[{#1}]{#2}}%
}
% Case: \changepagecolor{...}
\newcommand*{\@changepagecolor@ii}[1]{%
\@changepagecolor@do{{#1}}%
}
\newcommand*{\@changepagecolor@do}[1]{%
% Fill the remaining space with a colored rule
\begingroup
\offinterlineskip
\hbox to 0pt{%
\kern-\paperwidth
\vtop to 0pt{%
\color#1%
\hrule width 2\paperwidth height \paperheight
\vss
}%
\hss
}%
\endgroup
% Set page color for the next page
\afterpage{\pagecolor#1}%
}
\makeatother

\newenvironment{highlight}%
{\changepagecolor{lightgreen}}%
{\changepagecolor{white}}

\begin{document}
\blindtext
\blindtext
\blindtext
\blindtext

\begin{highlight}
\blindtext

\begin{theorem}
a theorem.
\end{theorem}

\begin{figure}[!h]
\includegraphics[width=.4\textwidth]{example-image-a}\hfill
\includegraphics[width=.4\textwidth]{example-image-b}
\caption{This is a figure caption within the custom highlight environment}
\end{figure}
\end{highlight}

\blindtext

\end{document}


The result: