[Tex/LaTex] Different Matlab code listings environment in same document with Matlab-prettifier

listingslstdefinestylematlab-prettifier

I wanted to be able to have different Matlab code listings in the same document. The first three listings are from the solution found here.

How do I get rid of the thin lined boxes in the first 3 Matlab code settings, while still keeping the thin box around the last lstlisting:

enter image description here

Here is the code:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{xcolor}
\tcbuselibrary{listings,skins}

\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\usepackage[numbered,framed]{matlab-prettifier}

\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
language=Matlab
}

\newtcblisting{mylisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only,
    listing style=mystyle,
    title=#2,
    #1
    }

\begin{document}

\begin{mylisting}{}
%% Accuracy Calculation
Equal_Rows = find(diff(matrix_rec,[],2) == 0);
percent_success = (size(Equal_Rows,1)/numFolders) * 100
\end{mylisting}

\begin{mylisting}[hbox]{}
%% Accuracy Calculation
Equal_Rows = find(diff(matrix_rec,[],2) == 0);
percent_success = (size(Equal_Rows,1)/numFolders) * 100
\end{mylisting}

\begin{mylisting}[hbox,enhanced,drop shadow]{Accuracy Calculation}
Equal_Rows = find(diff(matrix_rec,[],2) == 0);
percent_success = (size(Equal_Rows,1)/numFolders) * 100
\end{mylisting}

\begin{lstlisting}[
backgroundcolor=\color{blue!05},
style=Matlab-editor,
basicstyle=\ttfamily\small
]
%% Accuracy Calculation
Equal_Rows = find(diff(matrix_rec,[],2) == 0);
percent_success = (size(Equal_Rows,1)/numFolders) * 100
\end{lstlisting}

\end{document} 

Best Answer

You load the mcode package with the framed option, which adds the thin (undesired) frame around the code. Your mylisting environment is a color box from the tcolorbox package, which adds another frame around it. This is why you have the two frames.

To remove the inner frames, simply remove the framed option when loading mcode:

\usepackage[numbered,autolinebreaks,useliterate]{mcode}

result

Remark: you are using two different packages for the same purpose. If you don't watch out, you will get inconsistent output. As both these packages use listings in the background, they have undesired interactions: for example by removing framed from the mcode package, suddenly the line numbers in the matlab-prettifier listing are outside of the frame - even though you didn't change anything! Please, try to decide for one package1 and stick to that. You can setup matlab-prettifier to look like your mcode listings now, or the other way around.

1 matlab-prettifier is newer and has more features than mcode, and it comes with most standard LaTeX installations

Related Question