Creating and Inserting Code Snippets

You can create and insert code snippets using the Code Editor.

How to Create and Insert Code Snippets

  1. Place your cursor in an empty paragraph.
  2. Select Insert > Code Snippet.
  3. On the left side of the Code Editor, enter or paste code.
  4. (Optional) On the right side of the editor, complete any of the options.
    • Caption Enter a caption (or title) for the code snippet.
      • Position Select whether to place the caption above or below the code.

      • Repeat If you select "Repeat," the caption will be displayed where page breaks are encountered in print-based output (except Microsoft Word).

    • Language Select the code language. The code will be formatted appropriately, with syntax highlighting.

      If you select "(inherit)," the code uses whichever language is selected for the MadCap|codeSnippetBody style (the default setting is "PlainText"). So if you change the setting to a specific language, that language will automatically be used when you first insert a new code snippet. You can also create classes of that style and assign different languages to those classes. See Editing the Styles for Code Snippets.

      Example In your stylesheet, you create a class of the style MadCap|codeSnippetBody, naming it "CSS." Then you create another class and name it "JavaScript."

      On the "CSS" class you set the mc-code-lang property to CSS.

      And on the "JavaScript" class, you set the mc-code-lang property to JavaScript.

      When you insert a new code snippet containing CSS, you leave the Language field set to (inherit). Initially, the code displays as plain text. But after you apply the CSS style class to MadCap|codeSnippetBody, the syntax highlighting automatically changes to show the formatting appropriate for CSS.

      You insert another snippet containing JavaScript. This time you apply the JavaScript style class to MadCap|codeSnippetBody. The syntax highlighting changes for formatting appropriate for JavaScript.

    • Line Numbers Select this to include line numbers to the left of each line of code.

      • Starting Number Enter the number for the first line of code shown.

      • Continuous Select this if you are inserting multiple code snippets with line numbers, and you want the numbering to continue from one code snippet to the other, rather than starting over.

      Note If you enter a starting number and select Continuous, the number entered in the first field is ignored. The continuous setting has precedence.

  5. Click OK.
  6. Click Save the active file. to save your work.

If the animation below is cut off, you can see the complete animation by clicking the link under it to open the full topic.