How to remove paragraph direct formatting (only) from a document? (Writer)

Hi!

In LibreOffice Writer, how do you remove all direct paragraph formatting in a document while respecting direct character formatting?

If I place the cursor in a paragraph, without selecting anything, and hit Ctrl-M, the paragraph formatting returns to that of the paragraph’s style (e.g. if somebody made it centered manually but the style says left-aligned, it will become left-aligned). That’s the behavior I want.

But if I try to do that to many paragraphs at a time, I have to select them, and then all character formats disappear. E.g. if I write:

“Let v be a vector and f be a function…”

I get

“Let v be a vector and f be a function…”

I’d like bold v and italics f to be respected.

Thank you in advance! (I’m new to LibreOffice.)

AFAIK, you can’t because attributes you can directly set can be stored both in paragraph and character formatting.

Remember that direct formatting is just another layer of styles: kind of anonymous style category. The ordering of the layers are (from deepest to shallowest):

  • paragraph style
  • character style
  • direct formatting

Command Format>Clear Direct Formatting wipes out the direct layer without consideration for attribute destination (whether it is rather paragraph or character oriented).

The behaviour you experienced is quite “logical” in this context: when nothing is selected (cursor just put somewhere), Writer only sees the paragraph (and position character settings); therefore it removes paragraph modifications. When the selection contains a run of characters (which is the case when you selected several paragraphs), Writer sees both paragraph and character formatting, thus wipes both.

In your case, the right way to address your issue is to create ad hoc paragraph styles like function, vector and to configure them. Then, you style all functions with the required style. The obvious advantage is modification/updates are centralised in a single location (the style definition) and you no longer have to care for direct formatting maintenance.

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!

But could it be a way to edit all direct formatting with a specific character style, let’s say overwrite with a character style (to not lose the formatted locations) and then CTRL-M the hole text?

Thank you! I have created two new char styles, “CharStyle_Bold” and “CharStyle_Italics” and it works like a charm. I have reassigned Ctrl-B to CharStyle_Bold and Ctrl-I to CharStyle_Italics, so now usage is “transparent” to me. I can select whole paragraphs, hit Ctrl-M, and paragraph direct formattings are cleared but bold and italics remain.
The problem now is that in a new document Ctrl-B and Ctrl-I do nothing any more, because those character styles are not defined in them. I will try to find out how to make both styles appear in all new documents (and, if possible, even in documents passed to me by other people).
Thanks also for telling me about the three layers, and thinking of direct formatting as an anonymous one. I am new to Writer, so did not know this (but can understand, since I am familiar with Word styles, where direct formatting is called “applied” formatting). (P.S. I tried to upvote your very good answer but don’t have the 5 necessary points to do it.)

Yes, Cookievore’s idea is good too for already created or received documents. Thank you!

As an improvement, I suggest you don’t name your custom styles based on the visual effect you expect. With CharStyle_Bold, you don’t make a difference between a strong emphasis in “standard” text and a math object such as a vector. In the same document you have words or groups of words rendered bold for very different reasons. This is why I prefer semantic markup to visual markup. Tuning the styles, they may end up looking the same but it you want to make them clearly distinct, you just change one of the styles.

You already have built-in Strong Emphasis for part of phrases and you can have Vector for your vectors. You control each semantic category independently from the other.

I agree with semantic names, ajlittoz. But after working on a math doc today, this is still not a good solution for me. This is why.
Inside my text I also write superscripts, subscripts, letters with special mathematical fonts (R for reals…) etc., and combinations thereof. I think it should be unnecessary to create a style for each combination. For instance, N2 should be N italics, 2 subscript; but Nf should be N italics, f subscript italics, etc. Why create char styles (with equivalent semantic names) like Style_Italic, Style_Bold,Style_ItalicBold, Style_SubscriptItalic, Style_SubscriptBold,Style_SubscriptItalicBold, Style_SuperscriptItalic… etc.? The best would be a command that clears all direct paragraph formatting but not character formatting.
If I learn to program macros, I will try to create one that places the cursor in first paragraph (without selecting anything), clears direct formatting, moves to second paragraph does the same, etc. in a loop until the last paragraph.

I met such inconvenience because presently Writer allows only one character style to be applied to a sequence.

Have you considered inserting formulas with the halp of the Math component? This is overkill for sub/superscripted variables but they become immune to style “accidents”. Math is limited compared to LaTeX but it is bundled in the suite and is guaranteed to be interoperable.

Yes, I had thought about that, but then you also lose much of styles’ functionality. E.g. if I want to change the font size of all text by 1 point, I guess I’d have to later go Math formula by Math formula.
I still think the best solution would be a native command that removes direct paragraph formats but not direct character formats, or at least a macro that does the same to all paragraphs in a document.

Math being an external component to Writer returns frames, not text; therefore you can’t apply character styles on them. Also Math has not received substantial developer attention for years and its notion of styles (if I can use such a noble word for what’s implemented) is really poor viz. Writer. Changing the defaults is really painful.

Regarding direct formatting, there is no separation between paragraph and character attributes. They’re just “attributes” and considered as a whole by direct formatting management. I understand the interest to make a distinction (equivalent to having to layers of direct formatting), but the attributes are not clearly separated in the styles. Consider for example font attributes. They appear in paragraph styles to define default setting for the paragraph and are a basis for character attribute. This could cause a major confusion for implementing the feature.

Maybe there is no separation between paragraph and character attributes, but when I place the cursor in a paragraph without selecting characters and hit Ctrl-M, Writer knows exactly what to do! (And it happens to be what I want.)

Anyway, if Math has received little attention, what do people do when they need to insert lots of equations with special symbols in their Writer documents? In Word I do two things: I format very simple equations or symbols with subindex, superindex, italics, etc.; and for more complex equations I insert a MathType equation. Problem is, I can insert MathType eqs in Writer as OLE objects, but baseline alignment does not work to me (only bottom, centered, top I think), so it looks no good unless I place it manually, which I don’t want to do.

I use Insert>Object>Formula, not OLE Object. I don’t think its make a big difference. The formulas are inserted as inline frames, i.e. As Character. You can adjust frames individually or globally, customising frame style Formula. In the latter case, the modifications apply to all formulas, provided you didn’t apply direct formatting to them. Alignment choice is between Character, Base line and Row.

However, I noticed a “glitch”. If you play too much with the style, it becomes “stuck” and modifications are no longer effective.

This macro is a partial solution. It seems to work nicely if the document has only “paragraphs” (if there are tables, it does not reach the end of the document):

Sub RemoveAllDirectParagraphFormats
    Dim oDoc, oFrame, dispatcher As Object
    Dim oVC, oCurs As Object
    Dim sStyleNamePart As String
    oDoc = ThisComponent
    oFrame = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") 
    oVC = oDoc.CurrentController.getViewCursor()
    oVC.gotoStart(False)
    oCurs = oVC.getText().createTextCursorByRange(oVC)  
    rem Remove direct paragraph formats from first paragraph:
    dispatcher.executeDispatch(oFrame, ".uno:ResetAttributes", "", 0, Array())
    rem Move to next paragraph and repeat until end of document:
    While oCurs.gotoNextParagraph(False)
        dispatcher.executeDispatch(oFrame, ".uno:GoToNextPara", "", 0, Array())
        dispatcher.executeDispatch(oFrame, ".uno:ResetAttributes", "", 0, Array())
    Wend
End Sub

I adapted it from LibreOffice: stop macro loop at end of file - Super User (macro by Jim K, User Jim K - Super User), but I don’t have enough reputation to comment there. I basically took his macro and replaced the commands inside the loop by “ResetAttributes” and “MoveToNextParagraph”.

If someone could alter it so that it also works with documents with tables, it would be great. It could treat each table cell as a paragraph and remove its direct formatting. I have tried for some hours and could not make it work, but it can’t be too difficult for someone versed in macros.