
長い.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
質問に対する私のコメントの 1 つで示唆されているように、私はtcolorbox
とそのマクロを使用します。これらは基本的に同一の 2 つの環境を持ち、1 つは を使用し、もう 1 つは を\newtcbtheorem
使用します。定理がまだチェックされていない場合は、 を使用して後で編集して にするか、の定義からステートメントを削除します。checkedstyle
uncheckedstyle
\begin{uctheorem}...\end{uctheorem}
\begin{theorem}...\end{theorem}
colback={green!50!white}
uncheckedstyle
結局のところ、私の見解では、 search-and-replace
すべてのuctheorem
発生を で表すのが最善の方法ですが、もちろんそれは好みの問題です。theorem
もう 1 つのオプションは、オプションのローカル指定を可能にする「実際の」環境を使用することです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}
結果: