# Have just the file name show up in a listing environment

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:

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

Here is the code:

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\usepackage[no-math]{fontspec}

\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguages{english}

\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\definecolor{light-gray}{gray}{0.92}

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

\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}
\newtcblisting[use counter=lstlisting]{codeblock}[2][]{%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,
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][]{%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
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}

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

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

\begin{document}

\end{document}

• 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.

\documentclass{article}

\usepackage{matlab-prettifier}
\usepackage{tcolorbox}
\tcbuselibrary{listings}

% use \filename@parse to strip the path from filename
% also allow underscore in filename
\makeatletter
\begingroup
\catcode_\active
\gdef\strippath#1{%
\filename@parse{#1}%
\begingroup
\catcode_\active
\let_\textunderscore
\edef\fname{\filename@base .\filename@ext}%
\scantokens\expandafter{\fname}%
\endgroup
}
\endgroup
\makeatother

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

\begin{document}