Best way to display computer code in a Writer doc?

What is a good way to display computer source code in a Writer document in a way which allows readers to copy-and-paste into a source file without corrupting the code?

Is there some kind of OLE object which allows wide/tall text blocks to be scrolled into view?

I’m looking for something which functions like the code snippet and

code
block

features supported by this website, Github and others (I typed `code snippet` and ```\ncode\nblock\n``` above).

At a minimum, I need a way to paste source code into a Writer doc and not get any hyphenation changes, character substitutions, space character merging, line wraps, etc.

The Writer is not a code editor. If you need such properties, then you must switch off some features in scope the whole document, and you must apply some tricks in the applied paragraph style:

Features to switch off:

  • Switch off the “double spaces” in the Autocorrect feature. (do not merge the spaces/do not substitute double spaces)
  • Switch off the “Capitalize first letter of the sentence” in the Autocorrect feature.
  • Switch off the “Use replacement table” in the Autocorrect feature.
  • etc.

The tricks:

  • Use “nonbreaking spaces” by Find and replace (or use Left alignment in the applied Paragraph style) if you want same size spaces.
  • Use Monospace character set in the in the applied Paragraph style, if you want same width of the characters.
  • Switch off the “hyphenation” in the applied Paragraph style (Text flow).
1 Like

Hyphenation depends on language, so use a dedicated paragraph style an set language to none.

Will your readers get the Writer-file or something like a generated pdf? I can only recommend to test carefully before you use…
.
Maybe check sourcecode in guides for LibreOffice, availabe on the documentation pages of LibreOffice.org

Luckily not all languages are as sensitive as Python.

1 Like

I think that this way was proposed by A. Pitonyak in his famous book OOME_4_1.odt. :slightly_smiling_face:

Consider your code snippet as a single paragraph: paragraphs in Writer are the basic units of significance. Every paragraph has a semantic value. The common examples are headings (at various levels) and main discourse. You communicate this value to Writer by applying a paragraph style to your paragraphs, e.g. Heading n for headings and Body Text for main discourse.

Regarding your code snippets, type them as you would in your code editor but separate lines with Shift+Enter instead of simple Enter (except at end) to make the snippet a single paragraph. It may also help do disengage some AutoCorrect options as suggested by @Zizi64.

If you paste from a source file, you’ll have to change the paragraph breaks into line breaks Shift+Enter.

Either apply built-in Preformatted Text style or your own style. I have created for this purpose a custom Code Sample using a monospace font, left indent to make the snippet stand out of main discourse and coloured background (I could not mimic old computer stationery because there is no way to have line-alternated background colour). This style also has an augmented space above and below so that the extent of the snippet is clearly visible.

Since there is nothing else than the code in the snippet, you can copy it and paste it in a source file without artefacts. The line breaks end up the same as paragraph breaks in plain text file (source file).

Here is an example:

and my Code Sample definition:

2 Likes