改變不同環境的背景顏色

改變不同環境的背景顏色

我有一個很長的 .tex 文檔,我想將已經檢查過拼寫錯誤的部分的背景顏色更改為淺綠色。我正在尋找一個簡單的命令,可以像這樣使用

\HIGHLIGHT{

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

}

但該\hl{}命令不能包含其他環境,以下範例不起作用:

\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}

您對這裡可以使用哪個命令有什麼建議(排版不應更改,僅更改顏色)?

答案1

正如我對這個問題的評論之一所建議的,我將使用tcolorbox和 它的\newtcbtheorem宏,有兩個基本相同的環境,一個使用 ,checkedstyle另一個使用uncheckedstyle.如果尚未檢查定理,請\begin{uctheorem}...\end{uctheorem}稍後使用並編輯它,\begin{theorem}...\end{theorem}或只是colback={green!50!white}從 的定義中刪除該語句uncheckedstyle

最後,在我看來, a search-and-replaceof all uctheoremevents by將是最好的方式,但這當然是品味問題。theorem

另一種選擇是使用tcolorbox允許本地指定選項的「真實」環境,而這在環境中是不可能的tcbtheorem

\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}

在此輸入影像描述

答案2

作為一種適合突出顯示 LaTeX 文件任意部分的用例的替代方案,一種有趣的方法是使用color和對頁面背景進行部分著色afterpage套件對頁面背景進行部分著色(基於https://tex.stackexchange.com/a/237427/223749)。這樣做的優點是您根本不需要更改內容並且格式不會受到影響。突出顯示僅覆蓋整行並包括左右邊距,但我想這對您的用例來說不是問題。另外,如果您的突出顯示環境中有浮動並且它們浮出環境,則它們不會突出顯示。

MWE,包括定理、圖形和分頁符號:

\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}

結果:

在此輸入影像描述

相關內容