MATLAB Answers

How to not include an element in the Table of Content?

5 views (last 30 days)
Hello everyone.
I am creating a report and I am using and creating Section objects in loops. So in some cases in may create a lot of of Section. Hence is there a way to not include a Section object in the Table of Content? Or have the same type of object as Section in which I can add Paragraphs, Images, Tables etc..?
Thanks in advance!

  1 Comment

Jack Arnoldi
Jack Arnoldi on 5 Aug 2019
I have found a possible solution to my problem in the documentation of TableOfContents using TableOfContents.TOCObj.NumberOfLevels however this solution doesn't work as stated in another question.
If you know why, feel free to answer the question linked.
Thank you!

Sign in to comment.

Accepted Answer

Rahul Singhal
Rahul Singhal on 9 Aug 2019
Hi Jack,
As mentioned in the answer of the linked question, the TOCObj property of TableOfContents reporter was added in R2018b and hence you are unable to use it in R2018a.
To customize the number of levels in R2018a, you can customize the default template of the TableOfContents reporter to set the number of levels and then use this custom template to create your TableOfContents reporter. You can use mlreportgen.report.TableOfContents.createTemplate to create a copy of the default template and then customize it according to your requirements. As an example, to update the number of levels in PDF template, you will have to update the <toc> field in the dptemplate named TableOfContents as follows:
Change:
<dptemplate name="TableOfContents">
.....
<toc/>
</dptemplate>
to:
<dptemplate name="TableOfContents">
.....
<toc number-of-levels="1"/>
</dptemplate>
If you do not wish to customize the template and wants to achieve this programmatically, another alternative is to first get the DOM implementation of the TableOfContents reporter and then get access to the TOC from its children to customize it. Below is an example for a PDF report:
% Create a PDF report
rpt = mlreportgen.report.Report("myReport","pdf");
open(rpt);
% Create TableOfContents reporter based on the default template
reporter = mlreportgen.report.TableOfContents;
% Get the DOM implementation of this reporter
impl = getImpl(reporter,rpt);
% Get the DOM TOC child, which will be the third one in this case
tocObj = impl.Children(3);
% Customize this object according to your requirements
tocObj.NumberOfLevels = 1;
% Add the implementation to the report
add(rpt,impl);
% Create and add chapter and section objects to the report
% ...
% ...
% Close and view the report
close(rpt);
rptview(rpt);
Both the ways I have shown above demonstrates the TableOfContents customization for PDF reports. It may differ a little if you are generating a HTML or a DOCX report.
Hope this helps!
Thanks,
Rahul

  2 Comments

Jack Arnoldi
Jack Arnoldi on 12 Aug 2019
Thank you for your answer.
I get this error when I try to do it programmatically:
Unrecognized property 'NumberOfLevels' for class 'mlreportgen.dom.Text'.
Error in Meta_report (line 110)
tocObj.NumberOfLevels = 1;
The children of impl are these ones:
Capture.PNG
So I'm not sure there is DOM TOC there?
Rahul Singhal
Rahul Singhal on 12 Aug 2019
Hi Jack,
From your output, I can see that you are generating a DOCX report.
As I mentioned earlier, my examples were specific to the PDF output, and will differ for HTML and DOCX output.
For DOCX output, you will have to get the TemplateText object whose DOCXText property defines the TOC field and then update that. As an example,
change:
<w:instrText>TOC \o "1-3" \h \z \u \* MERGEFORMAT</w:instrText>
to:
<w:instrText>TOC \o "1-1" \h \z \u \* MERGEFORMAT</w:instrText>
Also, instead of using the TableOfContents reporter, you can directly use mlreportgen.dom.TOC object here, which will be easy for you to programmatically customize. DOM TOC will not add a title, but you can always add it using a DOM Paragraph object if required.
% Create a DOCX report
rpt = mlreportgen.report.Report("myReport","docx");
open(rpt);
% Create a DOM TOC object and customize it according to your requirements
tocObj = mlreportgen.dom.TOC;
tocObj.NumberOfLevels = 1;
% Add TOC object to the report
add(rpt,tocObj);
% Create and add chapter and section objects to the report
% ...
% ...
% Close and view the report
close(rpt);
rptview(rpt);
Thanks,
Rahul

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!