Conditional formatting in Writer?

Hi would like to automatically highlight certain types of character in an Writer document.
For example, all mathematical operators would be highlighted with a coloured background.

Is this possible?

Automatically may be understood in several ways.

The most elaborate: you type your math operator and magically it receives its highlighting background. Unfortunately, this high degree of automation is not possible.

The standard way, but requires manual editing:

  1. Define a character style with all required attributes, such as italics, font colour and background. Call it Highlight.
  2. Before typing your sequence of characters, switch to Highlight character style, type the characters, switch back to Default Style character style.

Alternatively, select the sequence of characters to highlight and give them Highlight character style.

In the above procedure, character is strongly emphasized to draw your attention on this family of styles which are different of the better known paragraph styles present in all document processing applications. Don’t confuse them. This is important because there is also a paragraph style named Default Style, but the effect is different. Character style Default Style removes any other applied character styles and reverts to defaults defined in paragraph style. Paragraph style Default Style is just a style like any other though, as ancestor of all other styles, it should never be used directly in documents.

If you’re not familiar with styles and their variations (paragraph, character, page, list, frame, …), you are encouraged to read the documentation (or at least the built-in help) to get an idea of the possibilities and power of the feature.

To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!

It sounds like you are asking about syntax highlighting. Andrew Pitonyak did this in his macro document at http://www.pitonyak.org/oo.php. To do it, he used macros. The colors wouldn’t update instantly when typed. Instead, run the macros to update the colors as needed.

You would need to write your own macros for the syntax and colors you want. If it were me, I would use a language such as Python that makes it easy to use regular expressions and string manipulation. It is difficult but possible to do these things in Basic.

Syntax highlighting is a standard feature of text editors, for example, Notepad++ and Vim. In text editors, colors are updated immediately while typing. Custom syntax highlighting can be done by adding a syntax highlighting config file.