How to maintain font color when text is imported but style is changed?

I import Matlab script with colored statements, copying from Matlab and pasting into writer doc (odt).
I need to change the font style whose font color is defined automatic. What happen?
All the color internal to a row ,say
xxxxxxx (black) yyyy (green) green is maintained.
But colored rows starting from the left margin , say
xxxxxxxxxxxxx (green) are automatically changed to black.
Of course I can recover by hand the original color (green) . I could also shift to the right the text,
but I am wandering whether in the style definition there is a way to impede font color change.
Enrico Canuto

Matlab is an external program with its own rules. How this is encoded is per Matlab specification. Could you attach to your question a sample Matlab data file you import (the shorter the better) so that I can have a look at its markup? From that, I could then suggest a robust solution based on styles.

Note you can attach only to a question (not to a comment). For that, edit your question, scroll to bottom, type twice Enter and use the “paperclip” tool.

Please do not use Add Answer but edit your original question to enhance the details of your question (answers are reserved for solutions to a problem on this Q&A site).

Actually, you can attach anything in a comment (done that already in several of my comments). Just “prepare” the operation by editing your question (or even starting a new one). BUT just CUT the new content without validating the answer and paste that code in the comment. This way, you keep the chronological chain of exchanges.

The first snippet which is supposed to be the raw import (but I’d have liked to see it before it is imported in Writer) is a real mess of direct-formatting with a mixture of direct paragraph styles (or rather anonymous paragraph styles) and direct character styles.

In the second snippet, you forced the paragraph style to Text Code which replaced the paragraph style layer but left intact the direct character style layer.

Where the initial formatting used no character style, i.e. the font colour was specified in the paragraph style, you end up with formatting dictated by Text Code (as expected). This happens on lines which are homogeneous (a single set of attributes for the whole line).

If your line has several highlightings, the corresponding attributes are set in various character styles which are not erased by your user paragraph style.

The “Choose orbital type” line kept its colour because the initial space is separately encoded. The line has thus two sections necessitating two direct formatting sequences. These are not overridden, explaining why the line remained green.

To solve your problem, it is necessary to know exactly what Matlab hands over to Writer. In the absence of this information, you must design your own character styles and apply them on the sequences. After that, you can Ctrl+M to clear direct formatting. If your character styles are correctly designed, you should see no change.

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!

In case you need clarification, edit your question (not an answer which is reserved for solutions) or comment the relevant answer.

I were you, I would try to learn a bit about macros and then create one that searches for text with each color and applies the relevant style depending on the color.

The time spent on building the code may be a time saver if you have to reformat many snippets like that.

You can ask in the forum (see my profile), there are macro gurus and already a lot of code examples.

Prova.odtThanks for your kind and quick reply.
Actually i made other tests and I am confused about the reasons of the color change.
Before to enclosed a case I will made other tests. Any way, the raw copy remains intact
and if I change the font size intact. I will continue after other tests.

Always nice to have feedback, but on this site answers are reserved for solutions. In order not to confuse other users, be kind enough to use the more link at bottom of your answer (only you as answer owner can see this link) to “repost as a comment”. Thanks.