3

I have a modified environment for a listing called mylisting. I wanted to know how to include the path to the file to be used in the mylisting environment?

Here is the code that I have so far:

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

\begin{filecontents*}{HOG_test.m}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('Case insensitive ');
WritE('Pascal keywords.');
\end{filecontents*}

\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}[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{mylisting}[hbox,enhanced,drop shadow]{Accuracy Calculation}

% How to get "HOG_test.m" code to appear in the mylisting???
\end{mylisting}]
\lstinputlisting{HOG_test.m}
\end{document} 

1 Answer 1

2
+50

You defined your new environment by means of the command \newtcblisting.
That command does not come from the listings package. It comes from the tcolorbox package.

Therefore take advantage also of the other features/macros provided by the tcolorbox package:

Instead of the \lstinputlisting-command from the listings package use the \tcbinputlisting-command from the tcolorbox package for displaying code that is stored in external files.

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

\begin{filecontents*}{HOG_test.m}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('Case insensitive ');
WritE('Pascal keywords.');
\end{filecontents*}

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


\tcbinputlisting{%
   arc=0pt, 
   outer arc=0pt,
   listing only,
   colback=blue!10,
   colbacktitle=blue!75!black,
   listing style=mystyle,
   title=Accuracy Calculation,
   hbox,
   enhanced,
   drop shadow,
   listing file=HOG_test.m
}

\end{document} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .