MATLAB Fast Automation: Tired of Working? Here's Your Step-by-Step Action Plan to Automate Your Work With MATLAB.



This article is an excerpt from the book MATLAB Fast Automation by Jacob Sapir.

Generate Latex Files from MATLAB

Why Latex?

One reason to use Latex as your preferred tool for automation of document creation is that it’s one of the best tools (if not the best) for creating documents properly. In this day and age, there’s no excuse for writing poorly formatted documents. Latex is also plain text (meaning the formatting is indicated with commands that are entered by typing keyboard characters, as opposed to being embedded in the document as it is in Microsoft Word, for example), which makes it the perfect candidate for automatically generated high-quality documentation.

Most documents are not supposed to be written manually because most of the information being documented usually already exists in another format and redundancy of information is prohibited when you want consistency, efficiency, and reliability.

If you don’t know anything about Latex and you want to know the basics in 5 minutes so that you can create your own Latex template to use for documenting, go to

Generate a Latex File

In this section, I’ll assume that you are already familiar with Latex, to some extent at least, or that you have read the article about the basics of Latex.
The following script generates a basic Latex file with the main components for composing a document (a title, sections, subsections, subsubsections, a table of contents, and lists of bullet points):

% choose the Latex file name
filename = 'basicLatexFileGenerated.tex';
fid = fopen(filename, 'at');
fprintf(fid, ['\\documentclass[10pt]{article}' '\n']);
% choose a font for your document
fprintf(fid, ['\\usepackage{mathpazo}' '\n\n']);
% write the title of your document
fprintf(fid, ['\\title{Generated Latex File}' '\n']);
% remove the date from the title generated with the command \maketitle
fprintf(fid, ['\\date{}' '\n']);
 % begining of your document
fprintf(fid, ['\\begin{document}' '\n\n']);
% write the title and the table of contents
fprintf(fid, ['\\maketitle’ '\n']);
fprintf(fid, ['\\tableofcontents' '\n\n']);
% write the content of your document
fprintf(fid, ['\\section{First section of the document}' '\n']);
fprintf(fid, ['\\subsection{First subsection of the document}' '\n']);
fprintf(fid, ['\\subsubsection{First subsubsection of the document}' '\n']);
fprintf(fid, ['Here is a list of bullet points generated from MATLAB:' '\n']);
fprintf(fid, ['\\begin{itemize}' '\n']);
fprintf(fid, ['\\item First bullet point' '\n']);
fprintf(fid, ['\\item Second bullet point' '\n']);
fprintf(fid, ['\\end{itemize}' '\n\n']);
% end of your document
fprintf(fid, ['\\end{document}' '\n']);

MATLAB interprets the characters “\” and “%” as MATLAB commands. To prevent this from happening, we use “\\” and “%%” instead. From that script, MATLAB generates the Latex file basicLatexFileGenerated.tex:


\title{Generated Latex File}
\section{First section of the document}
\subsection{First subsection of the document}
\subsubsection{First subsubsection of the document}
Here is a list of bullet points generated from MATLAB:
\item First bullet point
\item Second bullet point

After compiling the basic file twice (so that the table of contents can be generated), the following pdf file will be generated:

Leave a Reply