## 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
https://realtechnologytools.com/latex-template

### 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';
delete(filename);

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']);
fprintf(fid, ['\\begin{document}' '\n\n']);
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']);
fprintf(fid, ['\\end{document}' '\n']);
fclose(fid);

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:

\documentclass[10pt]{article}
\usepackage{mathpazo}

\title{Generated Latex File}
\date{}
\begin{document}
\maketitle
\tableofcontents

\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:
\begin{itemize}
\item First bullet point
\item Second bullet point
\end{itemize}
\end{document}

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