How do I create a frame for code?

Good morning,

I am writing technical documentation and I am trying to create a grey area in my document which is dedicated to programming code, to distinguish it from the explanatory text. To give an example of the visual aspect, look at the editor’s preview area here on libreoffice Q/A.

In my first attempt I created a paragraph style with a mono font and a gray background. Unfortunately, I found out that, since it’s a paragraph style, at every Enter a new paragraph is started. This leads to a very strange situation when exporting to pdf. In the document, the grey area appears as smooth, but in the pdf, you can see the individual horizontal paragraph lines, separated by a thin white line.

So I went on to create a Frame style. This apparently works, but with a complication. I get automatic capitalization when I don’t want it. Also, the frame is, of course, a frame, so it lives separated from the normal text flow, meaning that I have to click inside it to have the cursor enter the area for editing, and it does not wrap to the other page if it’s too long to fit in.

is there a better way to achieve what I want?

Answer below by @carnendil is largely correct. “This leads to a very strange situation when exporting to pdf. In the document, the grey area appears as smooth, but in the pdf, you can see the individual horizontal paragraph lines, separated by a thin white line.” This is an on-screen artefact of the PDF viewing software i.e., as I indicate, zooming in does not enlarge these lines. Do the fine lines appear on a printed version?

When defining your paragraph style, apply a border with the same color of your background around the paragraph, but make sure you un-check “Combine with following paragraph” under “Properties” at the bottom of the dialog box.

You can play with the thickness of the line, as well as the spacing to text.

It seems to me that this works in removing the thin white lines. At least I couldn’t detect them at 400% zoom on my PDF.

The fine lines visible with the “Combine with following paragraph” option selected (checked) disappear as the zoom level is increased and they do not print.

In fact, I didn’t check printing. If it’s just an artifact in the pdf reader, everything might be left as is. Being an issue related with the PDF software, rather than with the file, it is possible that other users of the same file will have a different experience when displaying the file on their screens, rendering the adjustments in my answer either unnecessary or useless.