メインコンテンツ

節の見出し、テーブル タイトル、および図の表題へのプログラムによる番号付け

R2026b

この例では、章の番号付き見出しと章内の節の階層番号付き見出しをプログラムで作成する方法を示します。この例では、番号付きの章や章内の節に存在する階層番号付きのテーブル タイトルと図の表題を作成する方法も示します。

Table of contents showing numbered chapters with hierarchical subsection numbering, such as 1.1. Figure for Land Ocean and 2.2. Tables for Magic(8) and Magic(10)

長い完全修飾クラス名を使用せずに済むよう、DOM API 名前空間をインポートします。

import mlreportgen.dom.*

ドキュメントを作成して開きます。Word ドキュメントを作成するには、出力タイプを pdf から docx に変更します。HTML ドキュメントを作成するには、pdf を、複数ファイル ドキュメントの場合は html に、単一ファイル ドキュメントの場合は html-file にそれぞれ変更します。

d = Document("mydoc","pdf");
open(d);

ドキュメントに目次を追加します。

append(d,TOC);

章の見出し、節の見出し、図の表題、表のタイトルの番号付けストリームを作成します。既定では、ストリームは初期値 0 のアラビア数字を使用して作成されます。

chapterStream = createAutoNumberStream(d,"chapter");
sectionStream = createAutoNumberStream(d,"section");
figureStream = createAutoNumberStream(d,"figure");
tableStream = createAutoNumberStream(d,"table");

後のセクションで使用するイメージ、テーブル、およびテーブル エントリのスタイルをいくつか定義します。

imageStyle = ...
    { ...
    Height("5in"), ...
    Width("5in") ...
    };

tableStyle = ...
    { ...
    Width("100%"), ...
    Border("solid"), ...
    RowSep("solid"), ...
    ColSep("solid") ...
    };

tableEntriesStyle = ...
    { ...
    HAlign("center"), ...
    VAlign("middle") ...
    };

以下のコードは、ドキュメントの最初の章を作成します。章のタイトルは、関数 getChapterTitle を使用して作成されます。この関数は、chapter 番号付けストリームを使用して、番号付きの章タイトルを作成します。この章は 2 つの節で構成され、そのタイトルは関数 getSectionTitle を使用して作成されます。この関数は、chapter 番号付けストリームと section 番号付けストリームの両方を使用して、階層番号付きの節タイトルを作成します。節は、階層番号付きの表題をもつ複数の図で構成されます。この表題は、関数 getFigureCaption を使用して作成されます。この関数は、chapter 番号付けストリームと figure 番号付けストリームの両方を使用して、階層番号付きの図の表題を作成します。この章の作成で使用する関数 getChapterTitle、関数 getSectionTitle、および関数 getFigureCaption については、この例で後述します。

% Chapter 1.
chapterTitle = getChapterTitle("Figures with numbered captions");
append(d,chapterTitle);

% Section 1.1.
sectionTitle = getSectionTitle("Figure for Land Ocean");
append(d,sectionTitle);

% Figure 1.1.
image1 =  Image(which("landOcean.jpg"));
image1.Style = imageStyle;
append(d,image1);
append(d,getFigureCaption("Land Ocean"));

% Section 1.2.
sectionTitle = getSectionTitle("Figures for Peppers and Cat's Eye Nebula");
append(d,sectionTitle);

% Figure 1.2.
image2 = Image(which("peppers.png"));
image2.Style = imageStyle;
append(d,image2);
append(d,getFigureCaption("Peppers"));

% Figure 1.3.
image3 = Image(which("ngc6543a.jpg"));
image3.Style = imageStyle;
append(d,image3);
append(d,getFigureCaption("Cat's Eye Nebula or NGC 6543"));

以下のコードは、ドキュメントに 2 番目の章を作成します。この章には、2 つの階層番号付きの節をもつ番号付きタイトルがあります。ここで、節は階層番号付きのタイトルをもつ複数のテーブルで構成されます。このタイトルは、この例の後半で定義される関数 getTableTitle を使用して作成されます。この関数は、chapter 番号付けストリームと table 番号付けストリームの両方を使用して、階層番号付きテーブル タイトルを作成します。

% Chapter 2.
chapterTitle = getChapterTitle("Tables with numbered titles");
append(d,chapterTitle);

% Section 2.1.
sectionTitle = getSectionTitle("Table for Magic(5)");
append(d,sectionTitle);

% Table 2.1.
append(d,getTableTitle("Magic(5)"));
table1 = Table(magic(5));
table1.Style = tableStyle;
table1.TableEntriesStyle = tableEntriesStyle;
append(d,table1);

% Section 2.2.
sectionTitle = getSectionTitle("Tables for Magic(8) and Magic(10)");
append(d,sectionTitle);

% Table 2.2.
append(d,getTableTitle("Magic(8)"));
table2 = Table(magic(8));
table2.Style = tableStyle;
table2.TableEntriesStyle = tableEntriesStyle;
append(d,table2);

% Table 2.3.
append(d,getTableTitle("Magic(10)"));
table3 = Table(magic(10));
table3.Style = tableStyle;
table3.TableEntriesStyle = tableEntriesStyle;
append(d,table3);

ドキュメントを閉じて表示します。

close(d);
rptview(d);

以下の関数は、章の番号付きタイトルを返します。番号付きタイトルは、DOM Heading1 オブジェクトを使用して作成されます。ここで、タイトル コンテンツは string Chapter N とピリオドで始まります。N は chapter ストリーム カウンターです。たとえば、最初の章のタイトルには接頭辞として "Chapter 1." が含められます。Style プロパティの CounterInc 書式により、この章タイトルがドキュメントに追加されるときに chapter ストリーム カウンターがインクリメントされます。Style プロパティの CounterReset 書式により、この章のタイトルがドキュメントに追加されるときに、section、figure、および table などの他の関連付けられたストリーム カウンターが初期値にリセットされます。

function chapterTitle = getChapterTitle(content)
    import mlreportgen.dom.*
    
    chapterTitle = Heading1();
    append(chapterTitle,Text("Chapter "));
    append(chapterTitle,AutoNumber("chapter"));
    append(chapterTitle,Text(". "));
    append(chapterTitle,Text(content));
    
    chapterTitle.Style = ...
        { ...
        CounterInc("chapter"), ...
        CounterReset("section figure table"), ...
        WhiteSpace("preserve"), ...
        PageBreakBefore(true), ...
        KeepWithNext(true) ...
        };
end

以下の関数は、章内の節の階層番号付きタイトルを返します。階層番号付きタイトルは、DOM Heading2 オブジェクトを使用して作成されます。ここで、タイトル コンテンツには string "N.M." が接頭辞として付けられます。ここで、N と M はそれぞれ chapter と section のストリーム カウンターです。たとえば、2 番目の章の最初の節のタイトルには接頭辞として "2.1" が含められます。Style プロパティの CounterInc 書式により、この節タイトルがドキュメントに追加されるときに section ストリーム カウンターがインクリメントされます。

function sectionTitle = getSectionTitle(content)
    import mlreportgen.dom.*
    
    sectionTitle = Heading2();
    append(sectionTitle,AutoNumber("chapter"));
    append(sectionTitle,Text("."));
    append(sectionTitle,AutoNumber("section"));
    append(sectionTitle,Text(". "));
    append(sectionTitle,Text(content));
    
    sectionTitle.Style = ...
        { ...
        CounterInc("section"), ...
        WhiteSpace("preserve"), ...
        KeepWithNext(true) ...
        };
end

以下の関数は、章または章内の節に追加された図の階層番号付き表題を返します。階層番号付きの表題は、DOM Paragraph オブジェクトを使用して作成されます。ここで、表題コンテンツには string "Figure N.F." が接頭辞として付けられます。ここで、N と F はそれぞれ chapter と figure のストリーム カウンターです。たとえば、2 番目の章の 3 番目の図の表題には接頭辞として "Figure 2.3." が含められます。Style プロパティの CounterInc 書式により、この図の表題がドキュメントに追加されるときに figure ストリーム カウンターがインクリメントされます。

function figureCaption = getFigureCaption(content)
    import mlreportgen.dom.*
    
    figureCaption = Paragraph();
    append(figureCaption,Text("Figure "));
    append(figureCaption,AutoNumber("chapter"));
    append(figureCaption,Text("."));
    append(figureCaption,AutoNumber("figure"));
    append(figureCaption,Text(". "));
    append(figureCaption,Text(content));
    
    keepWithPrevious = FOProperty("keep-with-previous.within-page","always");
    figureCaption.Style = ...
        { ...
        CounterInc("figure"), ...
        WhiteSpace("preserve"), ...
        FOProperties(keepWithPrevious) ...
        };
end

以下の関数は、章または章内の節に追加されたテーブルの階層番号付きタイトルを返します。階層番号付きのタイトルは、DOM Paragraph オブジェクトを使用して作成されます。ここで、タイトル コンテンツには string "Table N.T." が接頭辞として付けられます。ここで、N と T はそれぞれ chapter と table のストリーム カウンターです。たとえば、2 番目の章の 3 番目のテーブルのタイトルには接頭辞として "Table 2.3." が含められます。Style プロパティの CounterInc 書式により、このテーブル タイトルがドキュメントに追加されるときに table ストリーム カウンターがインクリメントされます。

function tableTitle = getTableTitle(content)
    import mlreportgen.dom.*
    
    tableTitle = Paragraph();
    append(tableTitle,Text("Table "));
    append(tableTitle,AutoNumber("chapter"));
    append(tableTitle,Text("."));
    append(tableTitle,AutoNumber("table"));
    append(tableTitle,Text(". "));
    append(tableTitle,Text(content));
    
    tableTitle.Style = ...
        { ...
        CounterInc("table"), ...
        WhiteSpace("preserve"), ...
        KeepWithNext(true), ...
        Bold(true), ...
        OuterMargin("0pt", "0pt", "10pt", "5pt") ...
        };
end