将列表数字移到 Matlab Prettifier 中的框架框之外

将列表数字移到 Matlab Prettifier 中的框架框之外

问题是解决方案在这里给出

如何删除mcode包以便清单 1 - 3 具有与最后一个清单类似的 Matlab 代码并且还包含框外的行号:

在此处输入图片描述

原始解决方案如下哈巴德茨

\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} 

答案1

如果您只想移动数字,可以使用numbersep=25pt或类似方法快速破解。但是,这不会改变代码块周围的边距,它们仍然与最后一个代码块中的边距不同。

\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} 

在此处输入图片描述


编辑:

对于第一个框,边距可以按如下方式调整:

\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} 

在此处输入图片描述

相关内容