How to get just the file name (sometimes with an underscore "_") to show up in the code environment if the actual listing file is in another location like ../Matlabcode/sol2_adx.m instead of something like this with the full path:

enter image description here

In this example, the title of the code should be sol2_adx.m

Here is the code:

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}




\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red

\newtcblisting[use counter=lstlisting]{codeblock}[2][]{%
        before upper={\hspace*{-1em}\codeimg~#2},%
        title after break={\centering\footnotesize\itshape\strut\lstlistingname~\thelstlisting~--~continued},%
        listing only,listing options={xleftmargin=-1mm},
        after upper={\centering\strut\lstlistingname~\thelstlisting:~#2},
        frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,frame code={\draw[gray,line width=2mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},#1}

\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}[3][]{%
        fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#3},%
    title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor,basicstyle=\ttfamily\scriptsize},
     after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
     frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
     listing file={#3},#1}


% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);


\inputmatlab{Expansion Algorithm}{sol2_adx.m}    
  • I get the listing: picture, click here – egreg Nov 14 '17 at 23:22
  • @egreg, yes, that is correct. The example code given will produce that output. As I stated in the problem, if the matlab file is under a different folder, the folder path then the name is output. To see the problem that I am having, probably created the .m file under a different folder and then you will see the output like that shown in the figure. Thanks! – Joe Nov 15 '17 at 1:39
  • Edited file to generate a .m file with an "_". Thanks. – Joe Nov 15 '17 at 1:48

With a little help from Extracting the basename from a filepath argument and How to display filenames with underscores. here is a simplified MWE showing how to do what you want.

In this example sol2_adx.m is placed in a subdirectory called subdir.



% use \filename@parse to strip the path from filename
% also allow underscore in filename
  \edef\fname{\filename@base .\filename@ext}%

% simplified inputlisting definition
% note before upper uses \strippath defined above
  before upper = \textbf{\strippath{#3}},
  listing only,
  listing options = {#1,style=Matlab-editor,basicstyle=\ttfamily},
  listing file = {#3},

\inputmatlab{Expansion Algorithm}{subdir/sol2_adx.m}    

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.