Main Content

Report Formatting Approaches

You can format your report using style sheets, format objects, format properties, or a combination of these approaches.

Style Sheets in Templates

The DOM API comes with default templates for each output type for formatting your report as it generates. You can customize these templates to specify the default formatting and layout of your reports. See Templates for DOM API Report Programs.

Use style sheets in a template to describe the default formatting of document objects like paragraphs, headers, and tables. A style sheet is a collection of formatting styles. A style is a named collection of formats for a particular type of object or, in the case of HTML and PDF, for a particular type of object that appears in a particular context in your document. For example, you can define a paragraph style MyPara that uses one set of formats, such as font size, emphasis, and font family. You define another paragraph style named YourPara that uses a different set of formats. When you write your report program, you assign the style to a paragraph object by name. For an example, see Use Style Sheet Styles.

Format Objects

A format object is a MATLAB® object that defines the properties and functions of a document format, such as a font family or size. The DOM API provides a set of constructors for creating format objects corresponding to most of the formatting options available in HTML, Word, and PDF documents. Most DOM document objects include a Style property that you can set to a cell array of format objects. You can use format objects with the document object Style property to format the object. For example, this code uses format objects to specify the style of a warning paragraph.

import mlreportgen.dom.*

p = Paragraph('Danger!');
p.Style = {Color('red'),FontFamily('Arial'),FontSize('18pt')};

It is a best practice to set the Style property by concatenating the existing value of the Style property and the cell array of format objects that you are adding. For example:

import mlreportgen.dom.*

p = Paragraph('Danger!');
p.Style = [p.Style {Color('red'),FontFamily('Arial'),FontSize('18pt')}];

This practice prevents inadvertent removal of format objects that you previously added or that the DOM API added to synchronize the Style property with the format properties. See Format Properties.

You can assign the same array of format objects to more than one DOM document object. This technique allows you to create a programmatic equivalent of a template style sheet. For example:

import mlreportgen.dom.*

warning = {Color('red'),FontFamily('Arial'),FontSize('18pt')};
p = Paragraph('Danger!');
p.Style = [p.Style warning];
p = Paragraph('Caution!');
p.Style = warning;

The DOM API allows you to assign any format object to any document object, regardless of whether the format applies. If the format does not apply, it is ignored.

Format Properties

Most DOM objects have a set of properties corresponding to the format options most commonly used for an object of that class. For example, this code sets the font and color of text in a paragraph, using the Color, FontFamily, and FontSize format properties of a Paragraph object.

import mlreportgen.dom.*

p = Paragraph('Danger!');
p.Color = 'red';
p.FontFamilyName = 'Arial';
p.FontSize = '18pt';

Related Topics