6

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)?

phinz
  • 289
  • 2
    In my opinion you should use tcolorbox and using it's theorem features, as such you can easily change the background colour –  Jan 21 '17 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. – phinz Jan 22 '17 at 09:28
  • By default tcolorbox does not set the boxes at the top of a new page. –  Jan 22 '17 at 09:30
  • But for a real clean interface you are likely to change your code -- with \hl{...} statements you would have to do that anyway –  Jan 22 '17 at 09:36
  • @ChristianHupfer When I try it without changing any default settings, it does not align small boxes of a few lines but for longer ones, it does. And how can I make tcolorbox span over multiple pages? – phinz Jan 22 '17 at 09:37
  • @ChristianHupfer When I use \tcbuselibrary{breakable} and \begin{tcolorbox}[breakable], it does not realign the box and spans over multiple pages. So this is a solution that does what I want. Thanks for pointing me in the right direction! – phinz Jan 22 '17 at 10:22
  • @ChristianHupfer Would you like to write an answer? – samcarter_is_at_topanswers.xyz Sep 14 '17 at 19:44
  • @samcarter: Answered ... –  Sep 16 '17 at 15:58
  • @samcarter: It's all-a-days-work for the Counter Wizard ;-) –  Sep 16 '17 at 18:22
  • 1
    @ChristianHupfer The title may need an amendment: "the Counter and TCB Wizard" https://i.stack.imgur.com/FBQgC.png – samcarter_is_at_topanswers.xyz Sep 16 '17 at 20:08
  • @samcarter: :D :D :D ... well, that's nice, but the real TCB wizard is Thomas Sturm of course –  Sep 16 '17 at 20:48

2 Answers2

4

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}

enter image description here

2

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:

enter image description here

buddemat
  • 236