Main Content

N.umber Section Headings, Table Titles, and Figure Captions Programmatically

此示例显示了如何以章节中的章节和分层编号标题的编程方式为章节创建编号标题。该示例还演示了如何创建位于编号章节中的分层编号表标题和图形标题,或者在章节中的一章中的小节中。

导入DOM API包,以便您不必使用长,完全限定的类名。

进口mlreportgen.dom.*

创建和打开文档。要创建Word文档,请更改输出类型PDF.docx.。要创建HTML文档,请更改PDF.htmlorhtml文件对于多重或单文件文档。

d =文档("mydoc"“pdf”);开放(d);

Append a table of contents to the document.

附录(d,toc);

为章节,小节标题,图表标题和表格标题创建编号流。默认情况下,使用具有初始值的阿拉伯数数字创建流0.

章节Stream = createAutoNumberStream(d,"chapter");sectionStream = createAutoNumberStream(d,“部分”);偶尔ream = createautonumberstream(d,"figure");tableStream = createAutoNumberStream(d,“桌子”);

定义某些图像,表和表条目样式,将在后面的部分中使用。

ImageStyle =.......{......高度("5in"的)那......Width("5in"的)......};tableStyle =......{......Width("100%"的)那......边界(“坚硬的”的)那......RowSep(“坚硬的”的)那......COLSEP(“坚硬的”的)......};tableentriesstyle =.......{......HAlign("center"的)那......VAlign(“中间”的)......};

以下代码在文档中创建第一章。章节标题是使用的getChapterTitlefunction. This function uses the章节编号流以创建编号章节标题。本章由两个小部分组成,其中标题是使用的getSectionTitlefunction. This function uses both章节andsectionnumbering streams to create the hierarchical numbered section title. The subsections consists of multiple figures with hierarchical numbered captions, which are created using thegetFigureCaptionfunction. This function uses both章节and数字编号流以创建分层编号的图形标题。这getChapterTitlegetSectionTitle那andgetFigureCaption用于创建本章的函数在此示例稍后描述。

% 第1章。prepartitle = getchaptertitle("Figures with numbered captions");append(d,chapterTitle);% Section 1.1.sectionTitle = getSectionTitle("Figure for Land Ocean");append(d,sectionTitle);%图1.1。image1 = Image(which(“Landecean.jpg”));image1.Style = ImageStyle;附加(d,image1);附录(d,getfigurecaption(“陆地海”));% Section 1.2.sectionTitle = getSectionTitle(“辣椒和猫眼星云的数字”);append(d,sectionTitle);%图1.2。image2 = Image(which("peppers.png"));image2.Style = imageStyle; append(d,image2); append(d,getFigureCaption(“胡椒”));%图1.3。image3 =图像(哪个("ngc6543a.jpg"));image3.Style = ImageStyle;附加(d,image3);附录(d,getfigurecaption(“猫的眼睛星云或NGC 6543”));

这following code creates the second chapter in the document. The chapter has a numbered title with two hierarchical numbered subsections. Here, the subsections consists of multiple tables with hierarchical numbered titles, which are created using thegettabletitle.function, defined later in this example. This function uses both章节andtablenumbering streams to create the hierarchical numbered table title.

% 第2章。prepartitle = getchaptertitle("Tables with numbered titles");append(d,chapterTitle);% Section 2.1.sectionTitle = getSectionTitle("Table for Magic(5)");append(d,sectionTitle);% Table 2.1.附加(d,gettabletitle("Magic(5)"));table1 = Table(magic(5)); table1.Style = tableStyle; table1.TableEntriesStyle = tableEntriesStyle; append(d,table1);%第2.2节。sectionTitle = getSectionTitle(“魔术表(8)和魔术(10)”);append(d,sectionTitle);% Table 2.2.附加(d,gettabletitle(“魔术(8)”));table2 = Table(magic(8)); table2.Style = tableStyle; table2.TableEntriesStyle = tableEntriesStyle; append(d,table2);% Table 2.3.附加(d,gettabletitle(“魔术(10)”));table3 = Table(magic(10)); table3.Style = tableStyle; table3.TableEntriesStyle = tableEntriesStyle; append(d,table3);

Close and view the document.

close(d); rptview(d);

以下函数返回一个章节的编号标题。使用DOM创建编号标题Heading1对象,其中标题内容由字符串前缀Chapter N和一个时期,哪里N.is the章节stream counter. For example, the title for the first chapter contains "Chapter 1." as its prefix. TheCounterInc格式在Style财产导致章节当本章标题附加到文档时,流计数器将递增。这CounterReset格式在Style财产导致other associated stream counters such as,section数字那andtable,将本章标题附加到文档时重置为初始值。

functionprepartitle = getchaptertitle(content) importmlreportgen.dom.*章节T.itle = Heading1(); append(chapterTitle,Text("Chapter "));append(chapterTitle,AutoNumber("chapter"));append(chapterTitle,Text(". "));append(chapterTitle,Text(content)); chapterTitle.Style =......{......逆局("chapter"的)那......逆抵抗("section figure table"的)那......WhiteSpace("preserve"的)那......PageBreakBefore(true),......KeepWithNext(true)......};结尾

以下函数返回一章中的部分的分层编号标题。使用DOM创建分层编号标题Heading2对象,其中标题内容被字符串“n.m”为前缀,其中n和m是章节andsection流计数器分别。For example, the title for the first section in the second chapter contains "2.1" as its prefix. TheCounterInc格式在Style财产导致sectionstream counter to be incremented when this section title is appended to the document.

functionsectionTitle = getSectionTitle(content) importmlreportgen.dom.*sectiontitle = heading2();附加(SectionTitle,AutoNumber("chapter"));append(sectionTitle,Text("."));附加(SectionTitle,AutoNumber(“部分”));append(sectionTitle,Text(". "));附加(部分,文本(内容));sectiontitle.style =.......{......逆局(“部分”的)那......WhiteSpace("preserve"的)那......KeepWithNext(true)......};结尾

以下函数返回添加到章节中的图形的分层编号标题或章节中的一章。使用DOM创建分层编号标题段落对象,其中标题内容被字符串“图n.f.”为前缀,其中n和f是章节and数字流计数器分别。例如,第二章中第三个数字的标题包含“图2.3”。作为其前缀。这CounterInc格式在Style财产导致数字当将此图形标题附加到文档时,流计数器将递增。

function数字Caption = getFigureCaption(content) importmlreportgen.dom.*figurecaption =段();附录(雕像,文字("Figure "));append(figureCaption,AutoNumber("chapter"));附录(雕像,文字("."));append(figureCaption,AutoNumber("figure"));附录(雕像,文字(". "));追加(figureCaption、文本(内容));keepWithPrevious = FOProperty(“保持与上面的.within-page”"always");figurecaption.style =.......{......逆局("figure"的)那......WhiteSpace("preserve"的)那......Foproperties(守信前)......};结尾

以下函数返回添加到章节中的表的分层编号标题或章节中的小节。使用DOM创建分层编号标题段落对象,其中标题内容由字符串前缀"Table N.T.", whereN.andT.are the章节andtable流计数器分别。For example, the title for the third table in the second chapter contains "Table 2.3." as its prefix. TheCounterInc格式在Style财产导致tablestream counter to be incremented when this table title is appended to the document.

functiontableTitle = getTableTitle(content) importmlreportgen.dom.*尖端=段();附录(百彩票,文字("Table "));附录(百彩票,自动数("chapter"));附录(百彩票,文字("."));附录(百彩票,自动数(“桌子”));附录(百彩票,文字(". "));附录(百彩票,文字(content)); tableTitle.Style =......{......逆局(“桌子”的)那......WhiteSpace("preserve"的)那......keepwithnext(true),......Bold(true),......OuterMargin(“0pt”“0pt”"10pt"“5pt”的)......};结尾