[Tex/LaTex] Background colouring (highlighting) for maths formula in Lyx

highlightinglyx

I want to highlight selected maths formulas in Lyx.

Is there a simple way of doing this from the GUI ?

I have tried selecting and right-clicking -> Text style , but that seems to change the font colour, not the background colour :

enter image description here

I have also tried the box menu -> shaded highlight, but the highlighting runs all the way across the page for some reason (in the PDF as well as in the Lyx GUI):

enter image description here

I am able to do this by entering Latex code for the highlighting and the formula, but I was hoping this highlighting from the Lyx GUI.

I am using Lyx version 2.2.3 with TexLive.

Best Answer

A quick hack for using \hl, is to write only \hl{ and } in ERTs, and write the math stuff using the LyX GUI. So it might look like this:

enter image description here

Another option is to define a new custom inset for \hl. Go to Document --> Settings --> Local layout, and add this:

InsetLayout Flex:highlight
    LyxType               custom
    LabelString           hl
    LatexType             command
    LatexName             hl
    Font
      Series              Bold
    EndFont
    Preamble
    \usepackage{xcolor}
    \usepackage{soul}
    \colorlet{highlightgreen}{green!10}
    \colorlet{highlightgray}{gray!20}
    \sethlcolor{highlightgreen} 
    EndPreamble
    InToc                 false
    HTMLTag               strong
    ResetsFont true
End

(Alternatively skip the Preamble block, and add those to the preamble manually.)

Now you should find hightlight under Insert -> Custom insets:

enter image description here

Add one of those insets, and add the inline math inside it. Might look like this:

enter image description here