# How can I add a matlab code (namefile.mlx) to my overleaf project?

I've exported my matlab code (a file in .mlx format) by using the option "Save" -> "export to LaTex" and I've obtained two files, one in .tex format and the other one in .sty format. How can I add these files to my overleaf project?

I'd like to get something like this:

Can you explain also how I can possibly remove the column of numbers and how I can possibily change the colors of the text?

This is the structure of my project:

Content of file Q2_esercizi_1_2_3.tex:

% This LaTeX was auto-generated from MATLAB code.
% To make changes, update the MATLAB code and export to LaTeX again.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{color}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{epstopdf}
\usepackage[table]{xcolor}
\usepackage{matlab}

\sloppy
\epstopdfsetup{outdir=./}
\graphicspath{ {./Q2_esercizi_1_2_3_images/} }

\begin{document}

\matlabtitle{\textbf{DSV\_Quaderno 2: Orientamento del velivolo e trasformazione di assi}}

\matlabheading{Esercizio 2.1: Orientamento di un velivolo in assi terra}

\begin{matlabcode}
clear all; close all; clc;
% see http://wpage.unina.it/agodemar/DSV-DQV/aircraft_models.zip
shapeScaleFactor = 1.0;
[V, F, C] = loadAircraftSTL('F-22_Raptor.stl', shapeScaleFactor);
\end{matlabcode}
\begin{matlabcode}
%save shape in .mat format for future use
shape.V = V; shape.F = F; shape.C = C;
save('F-22_Raptor.mat', 'shape');

\end{matlabcode}

\begin{matlabcode}
h_fig1 = figure(1);
grid on
hold on
light('Position',[0.5 0.5 -1],'Style','local');   %original light position [1 0 -2]
% Trick to have Ze pointing downward and correct visualization
set(gca,'XDir','reverse');
set(gca,'ZDir','reverse');
%% Display aircraft shape
p = patch('faces', shape.F, 'vertices', shape.V);
set(p, 'facec', [0.20 0.73 1.00]);       %light blue [0.20 0.73 1.00]rgb()
set(p, 'EdgeColor','none');
theView = [-114 14];
view(theView);
axis equal;
%axis([-1 1 -1 1 -1 1]);
%box on;
hold on;
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{56.196688409433015em}]{figure_0.png}
\end{center}
\begin{matlabcode}

\end{matlabcode}

\begin{matlabcode}
xMax = 1.8*max(abs(shape.V(:,1)));
yMax = 1.8*max(abs(shape.V(:,2)));
zMax = 0.5*xMax;
quiver3(0,0,0,xMax,0,0, 'r','linewidth',2.5);
hold on
quiver3(0,0,0,0,yMax,0, 'g','linewidth',2.5);
hold on
quiver3(0,0,0,0,0,zMax, 'b','linewidth',2.5);
print('-depsc2','-r600',strcat('velivolo_BRF','.eps'));
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{56.196688409433015em}]{figure_1.png}
\end{center}
\begin{matlabcode}

\end{matlabcode}

\begin{matlabcode}
%% Setup the figure/scene
h_fig2 = figure(2);
light('Position',[2 2 -5],'Style','local');
% Trick to have Ze pointing downward and correct visualization
set(gca,'XDir','reverse'); set(gca,'ZDir','reverse');
grid on; hold on; box on;
shapeScaleFactor_bis = 1.0;
%% Set the aircraft in place
% Posision in Earth axes
vXYZe = [2,2,-2];
% psi, theta, phi -> 'ZYX'
% Observer point-of-view
theView = [112 10];
% body axes settings
bodyAxesOptions.show = true;
bodyAxesOptions.magX = 2.0*shapeScaleFactor_bis;
bodyAxesOptions.magY = 2.0*shapeScaleFactor_bis;
bodyAxesOptions.magZ = 1.5*shapeScaleFactor_bis;
bodyAxesOptions.lineWidth = 2.5;
plotBodyE(h_fig2, shape, ...
vXYZe, vEulerAngles, ...
bodyAxesOptions, theView);
%Trick to change aircraft color
h = findobj(gca,'Type','Patch');
h.FaceColor = [0.1 0.7 1];
% Plot Earth axes
hold on;
xMax = max([abs(vXYZe(1)),5]);
yMax = max([abs(vXYZe(2)),5]);
zMax = 0.3*xMax; % max([abs(max(vXYZe(1))),0.18*xMax]);
vXYZ0 = [0,0,0];
vExtent = [xMax,yMax,zMax];
plotEarthAxes(h_fig2, vXYZ0, vExtent);
% draw CoG coordinate helper lines
hold on;
options.lineColor = 'k';
options.lineWidth = 1.5;
options.lineStyle = ':';
plotPoint3DHelperLines(h_fig2, vXYZe,options);
ylim([0 4]);

print('-depsc2','-r600',strcat('CoG_projection','.eps'));
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{56.196688409433015em}]{figure_2.png}
\end{center}
\begin{matlabcode}

\end{matlabcode}

\matlabheading{Esercizio 2.2: Componenti della forza peso in assi velivolo}

\begin{matlabcode}
%% Mass data of Lockheed Martin-Boeing F-22 Raptor
mass = 29410; % kg
g = 9.81; % m/s^2
%% Euler angles
psi = vEulerAngles(1); theta = vEulerAngles(2); phi = vEulerAngles(3);
%% DCM
% Transf. matrix from Earth- to body-axes
Tbe = angle2dcm(psi, theta, phi, 'ZYX');
vWeight_E = [0; 0; mass*g] % N Ë
\end{matlabcode}
vWeight_E = 3x1
1.0e+05 *

0
0
2.8851

\begin{matlabcode}
vWeight_B = Tbe*vWeight_E
\end{matlabcode}
vWeight_B = 3x1
1.0e+05 *

0.5010
0
2.8413

\begin{matlabcode}

\end{matlabcode}

\matlabheading{Esercizio 2.3: Rappresentazione delle componenti del peso}

\begin{matlabcode}
%% Draw Weight pointing downward
hold on
scale_weight = 0.000005;
weightVecMag = scale_weight*mass*g;
quiver3( ...
vXYZe(1),vXYZe(2),vXYZe(3), ...
0, 0, weightVecMag, ...
'AutoScale', 'off', 'Color', [0 0 0], 'LineWidth', 2.5 ...
);
%% Vector W_XB * i_B
% application point along z_B
Teb=Tbe';
pWZB_B = scale_weight.*[0;0;vWeight_B(3)];
pWZB_E = vXYZe' + Teb*pWZB_B;
% Vector W_XB * i_B (body-components)
vWeight_XB_B = scale_weight.*[vWeight_B(1);0;0];
% Vector W_XB * i_B (Earth-components)
vWeight_XB_E = Teb*vWeight_XB_B;
quiver3( ...
pWZB_E(1), pWZB_E(2), pWZB_E(3), ...
vWeight_XB_E(1), vWeight_XB_E(2), vWeight_XB_E(3), ...
'AutoScale', 'off', 'Color', [0 0 0], 'LineWidth', 2.0, ...
);
print('-depsc2','-r600',strcat('componenti_peso','.eps'));
\end{matlabcode}
\begin{center}
\includegraphics[width=\maxwidth{56.196688409433015em}]{figure_3.png}
\end{center}

\end{document}



Content of file matlab.sty:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{matlab}

\RequirePackage{verbatim}
\RequirePackage{fancyvrb}
\RequirePackage{alltt}
\RequirePackage{upquote}
\RequirePackage[framemethod=tikz]{mdframed}
\RequirePackage{hyperref}
\RequirePackage{color}

\newcommand{\maxwidth}[1]{\ifdim\linewidth>#1 #1\else\linewidth\fi}
\newcommand{\mlcell}[1]{{\color{output}\verbatim@font#1}}

\definecolor{output}{gray}{0.4}

% Unicode character conversions
\DeclareUnicodeCharacter{B0}{\ensuremath{^\circ}}
\DeclareUnicodeCharacter{21B5}{\ensuremath{\hookleftarrow}}

% Paragraph indentation
\setlength{\parindent}{0pt}

\hypersetup{
urlcolor=blue
}

% environment styles for MATLAB code and output
\mdfdefinestyle{matlabcode}{%
outerlinewidth=.5pt,
linecolor=gray!20!white,
roundcorner=2pt,
innertopmargin=.5\baselineskip,
innerbottommargin=.5\baselineskip,
innerleftmargin=1em,
backgroundcolor=gray!10!white
}

\newenvironment{matlabcode}{\verbatim}{\endverbatim}
\surroundwithmdframed[style=matlabcode]{matlabcode}

\Verbatim[xleftmargin=1.25em, formatcom=\color{output}]%
}{\endVerbatim}

\newenvironment{matlabsymbolicoutput}{%
\list{}{\leftmargin=1.25em\relax}%
\item\relax%
\color{output}\verbatim@font%
}{\endlist}

\newenvironment{matlabtableoutput}[1]{%
{\color{output}%
\hspace*{1.25em}#1}%
}{}

\newcounter{multititle}
\newcommand{\matlabmultipletitles}{\setcounter{multititle}{1}}

\newcounter{hastoc}
\newcommand{\matlabhastoc}{\setcounter{hastoc}{1}}

\newcommand{\matlabtitle}[1]{
\ifnum\value{multititle}>0
\ifnum\value{hastoc}>0
\fi
\fi
\section*{#1}
}

\ifnum\value{hastoc}>0
\fi
\subsection*{#1}
}

\ifnum\value{hastoc}>0
\fi
\subsubsection*{#1}
}

\ifnum\value{hastoc}>0
\fi
\paragraph*{#1}
}

\newcommand{\matlabtableofcontents}[1]{
\renewcommand{\contentsname}{#1}
\tableofcontents
}


• Well, I don't know Matlab, so I don't know how the LaTeX export looks like, but if you got a .tex and a .sty file, you should be able to just upload both files into your Overleaf project and open and compile the .tex file. We will only be able to help you with the formatting of this, if we get to know to code of both files, I fear. Mar 22 at 18:14
• Thanks for your answer. I've uploaded the two file and I've used the command: "\include{Chap_1/Listato_1/Q2_esercizi_1_2_3}" but I have the following error: "LaTex error \include cannot be nested". I've updated the question to show the structure of my project. Mar 22 at 22:07
• You can't include the Matlab .tex file directly into your docment, because (at least this is what I assume) it has its own preamble. You can only include parts of it. But since we don't know how these files are coded, we cannot help you. Please at least post the code of such a file exported by Matlab (you can shorten it if it is very long) and also post the contents of the .sty file. Mar 22 at 22:25
• Good morning @JasperHabicht. I've added to my question the files that you have suggested me. Sorry for their length. Mar 23 at 9:10

The files you exported from Matlab are LaTeX documents on their own, including their own .sty file (which is a package file), title and formatting. Therefore, you cannot simply use \include to add these files to your document.

If you want to include the whole file as is, you can compile the file you exported from Matlab and than include the generated PDF file (or pages from it) to your own document using the pdfpages package and with \includepdf[pages=-]{compiled-matlab-file.pdf}. This way, all the formatting of the original file will be kept.

If you only want to include parts of the Matlab files, the easies way is just to copy the parts of the code and paste it into your document. The file exported from Matlab uses a custom matlab environment which is defined in the .sty file. But you can easily take the definition from the .sty file and put it into your preamble:

\usepackage{fancyvrb}
\usepackage[framemethod=tikz]{mdframed}
\mdfdefinestyle{matlabcode}{%
outerlinewidth=.5pt,
linecolor=gray!20!white,
roundcorner=2pt,
innertopmargin=.5\baselineskip,
innerbottommargin=.5\baselineskip,
innerleftmargin=1em,
backgroundcolor=gray!10!white
}
\newenvironment{matlabcode}{\Verbatim}{\endVerbatim}
\surroundwithmdframed[style=matlabcode]{matlabcode}

\definecolor{output}{gray}{0.4}

With this, you can copy matlabcode and matlaboutput blocks from the file you exported with Matlab. If you would like to place images into your document, you can just include them like they are included in the file you exported from Matlab (you need to load the graphicx package in your preamble and, of course, adjust the path to the file):
\begin{center}