Caption is only a convenience command which simulates many manual additions to your document. To know the extent of the “object” to caption, this menu command presently works only on frames or tables.
The first method would be to put your code snippet into a frame. CAUTION! This implies that your code snippet can’t straddle a page break nor extend on several pages. This is fit for relatively small code samples.
But you can do captioning yourself. This is the second method. After having designed your own caption look, record the whole caption paragraph in an AutoText item for ease of reuse.
- If you want to number your code examples separately from figures, tables, illustrations, drawings, …
You must create a dedicated number range.
Other Fields, go to
- In Type, select Number range
- Enter a dedicated name in Name, e.g. Code
- Choose the preferred format (usually Arabic(1 2 3))
- If you want to number per chapter, choose the desired level in Numbering by Chapter
Insert, then Close
- Erase the newly inserted field (we were only creating the number range)
- Define a dedicated paragraph style to give a common look to all your captions
- Display the side style pane if not already visible ( F11 except under MacOS where you must
- Right-click on Caption and
You then create a style inheriting from Caption so that changes in Caption are also forwarded to all its descendants Drawing*, Figure, Illustration, Table, Text and the newly created one.
Give a name to the style, e.g. Code.
Unless you already have an idea on how different from Caption this style is, leave it as a duplicate for Caption. You can modify Caption for center alignment and all captions (figure, table, … and code) will be centered.
When you want to insert a caption followed by a code sample:
- Set the current paragraph style to Code
- Type "Code example " (note the space after “example”)
Variables tab, select the *Code number range
Insert, then Close (unless your screen is wide enough to keep this dialog open for future code number insertion)
- Type ": " followed by the specific caption
Once you have one caption, select the caption up to the colon (don’t include the specific caption) and create an AutoText entry, giving it a shortcut (I used CC for “code caption”).
Next time you need a caption, type Enter cc F3. Continue by typing the specific caption.
Your caption may be above or below the code sample. You seem to have a preference for “above”. In this case to avoid the risk of having the caption alone at the bottom of the page, tick Keep with next paragraph in
Text Flow tab of either Caption (for all captions) or Code (only for code examples) paragraph styles.
This will work only if you don’t use empty paragraphs to space caption and code example. The vertical space should be defined in
Indents & Spacing tab of either Caption or Code paragraph styles.
Table of code examples
Since you have a dedicated number range for your code examples/snippets, you can create a table of these at end of your book.
TOC & Index>
TOC, Index or Bibliography
- Select Table of Figures from Type
- Change the Title
- In Create from
- tick Captions
- select Code from Category
- keep References in Display