14

I am using Linux. I want to write small pieces of code and paste them into LibreOffice Writer. I do not care too much about which code editor to use, although preferably it would be ViM or Sublime Text 3.

I have tried several avenues, but all of them are unsuccessful:

  • Write the code in ViM and save them as HTML with TOhtml. Then, open the HTML file with Firefox, copy the code and paste it in LibreOffice with Paste Special/HTML. The colors are gone, though.
  • Write the code in Sublime Text 3 and export it using the SublimeHighlight module. I can copy the text as RTF/HTML, but when pasting it in LibreOffice it pastes the literal HTML code. Paste Special didn't solve it either.

How can one paste HTML/RTF code into LibreOffice Writer?

Running Xubuntu 16.04 and LibreOffice 5.1.6.2 10m0(Build:2)

user2891462
  • 343
  • 2
  • 3
  • 9

3 Answers3

9

Libreoffice extensions

There are several extensions that allow to highlight a code snippet.
You can search for them in extensions.libreoffice.org.

Among the others

The steps to follow are usually as easy as

  1. Insert a new Text Box (Insert -> Text Box)
  2. Copy and paste/write your code snippet into the text box (You can choose any fonts based on your preference)
  3. Select the text box
  4. Go to Tools -> Highlight Code -> Language of the code

(From Code Highlighter)

You may need to install

sudo apt install libreoffice-script-provider-python python3-pip
sudo pip3 install pygments

Hastur
  • 18,764
  • 9
  • 52
  • 95
  • 1
    Note that if instead you decide to use LaTex for a better result, there are macro that will work fine for you in that case too. – Hastur Feb 13 '20 at 14:44
  • @Hastur: https://github.com/slgobinath/libreoffice-code-highlighter/issues/43 – einpoklum Jun 09 '21 at 13:11
  • @einpoklum Good an open bug. Let's hope someone will take it in charge and solve it... I did not write this app I just used (to be honest few, because I prefer to use LaTex ;-) ). BTW It seems there is a fix: _And renaming before installing, the downloaded extension's file name to codehighlighter.oxt, is the solution._. Give it a look if it works for you. – Hastur Jun 09 '21 at 13:24
1

it exist now Code Highlighter2 , the successor of 1 (no more maintained):

https://extensions.libreoffice.org/en/extensions/show/5814

https://github.com/jmzambon/libreoffice-code-highlighter

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '22 at 15:39
1

The code colorizer formatter link is broken. The code-highlighter extension works but has two requirements: Examples are for fedora type distributions, should be similar for debian, slackware, archlinux, etc.

  1. Install python3 pygments packages:

    sudo dnf install python3-pygment*

  2. renaming the otx prior to adding the extension to libreoffice:

    mv ea4db15f_codehighlighter.oxt codehighlighter.oxt

Writing html for code seems to be overkill.

1of7
  • 11
  • 1