[Tex/LaTex] Color highlighting text and environments in LyX

colorenvironmentshighlightinglyx

I am using LyX 2.0.5 with document class article and 'Theorems (AMS)' in the modules. I am wondering if it is possible to make it so that the titles of different environments will be highlighted in different colors ("highlighted" in the sense of a highlight marker. I.e., having a colored background, w/o changing the font color). E.g., in the Theorem environment the words Theorem 12.1 will be typeset with red highlight, in the Definition environment the words Definition 5.9 will be typeset with yellow highlight, etc.

I am guessing the will require changing the module?

Best Answer

(Note: there are parts of this solution that are sub-optimal, and I'm aware of that, but I'll have to fix that later. I'll remove this note when I find the time to improve things.)

You can change the files theorems-ams.module and theorems-ams.inc, or better create copies as described in my answer to How to make "claim" environments look like "theorem" environment?

In theorems-ams.module, add xcolor to the required packages. I.e. where it says

Requires    amsmath,amsthm

edit this to

Requires    amsmath,amsthm,xcolor

(This bit is untested, but I assume it will work.)

In theorems-ams.inc, find the style you want to change, for example the block defining the look of theorems starts with Style Theorem.

In the Style block there is a sub block Preamble, which by default looks like

Preamble
    \theoremstyle{plain}
    \newtheorem{thm}{\protect\theoremname}
EndPreamble

Using code taken from egreg's answer to amsthm environment and colorbox, edit this to read

Preamble    
    \newtheoremstyle{Coloredtheo}%
    {3pt}    %Space above
    {3pt}    %Space below
    {}   %Body font
    {}   %Indent amount
    {\bfseries}    %Theorem head font
    {}   %Punctuation after theorem head
    {0.25em} %Space after theorem head
    {\colorbox{red}{\color{black}\thmname{#1}%
      \thmnumber{\@ifnotempty{#1}{ }\@upn{#2}}}%
      \thmnote{ {--- \the\thm@notefont#3.}}%
    }   %Theorem head spec
    \theoremstyle{Coloredtheo}
    \newtheorem{thm}{\protect\theoremname}    
EndPreamble

Note that this will not change the view in LyX, only in the PDF. Do similarly for Definition, but use a different name for the \newtheoremstyle. The background colour is set by the first argument to \colorbox, here red.

For a single document, you could use the Local layout under Documents --> Settings. Add the entire Style block to the local layout. You may need to also include \usepackage{xcolor} to the Preamble block.