1

The question is building off the solution give here.

How do you remove the mcode package so that listings 1 - 3 will have the Matlab code that resembles the last listing and also contain the line numbers outside of the box :

enter image description here

Here is the original solution as given by hbaderts:

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

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

\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
%language=Matlab,
style=Matlab-editor,
basicstyle=\ttfamily\small,
frame=none
}

\newtcblisting{mylisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only,
    colback=blue!10,
    colbacktitle=blue!75!black,
    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

%% Save data to be used in pgfplots
switch nn
    case 1
        fileID = fopen('PCA_KNN1.dat','w');
    case 2
        fileID = fopen('PCA_KNN2.dat','w');
end
\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

%% Save data to be used in pgfplots
switch nn
    case 1
        fileID = fopen('PCA_KNN1.dat','w');
    case 2
        fileID = fopen('PCA_KNN2.dat','w');
end
\end{lstlisting}

\end{document} 
Joe
  • 9,080

1 Answers1

4

If you just want to move the numbers, a quick hack could be to use numbersep=25pt or similar. However this will not change the margins around the code block, they will still be different then in the last code block.

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

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

\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
%language=Matlab,
style=Matlab-editor,
basicstyle=\ttfamily\small,
numbersep=25pt,
frame=none
}

\newtcblisting{mylisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only,
    colback=blue!10,
    colbacktitle=blue!75!black,
    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

%% Save data to be used in pgfplots
switch nn
    case 1
        fileID = fopen('PCA_KNN1.dat','w');
    case 2
        fileID = fopen('PCA_KNN2.dat','w');
end
\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

%% Save data to be used in pgfplots
switch nn
    case 1
        fileID = fopen('PCA_KNN1.dat','w');
    case 2
        fileID = fopen('PCA_KNN2.dat','w');
end
\end{lstlisting}

\end{document} 

enter image description here


EDIT:

For the first box, the margins can be adjusted as the following:

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

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

\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
%language=Matlab,
style=Matlab-editor,
basicstyle=\ttfamily\small,
xleftmargin=-12pt,
aboveskip=-6pt,
belowskip=-6pt,
frame=none
}

\newtcblisting{mylisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only,
    colback=blue!10,
    colbacktitle=blue!75!black,
    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

%% Save data to be used in pgfplots
switch nn
    case 1
        fileID = fopen('PCA_KNN1.dat','w');
    case 2
        fileID = fopen('PCA_KNN2.dat','w');
end
\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

%% Save data to be used in pgfplots
switch nn
    case 1
        fileID = fopen('PCA_KNN1.dat','w');
    case 2
        fileID = fopen('PCA_KNN2.dat','w');
end
\end{lstlisting}

\end{document} 

enter image description here