[Tex/LaTex] Only get filename and extension of listing, not whole path

captionslistingsparsing

I am trying to add the file name as caption. I was able to do this setting

caption=\lstname

but this shows the full path. I am now trying to get the name of the file and extension to show only. I was trying to use the \filename@parser, but no luck.

This is how the code look like:

\ifdefined\docdefined
\else
%   \documentclass[a4paper,twoside,12pt, openright]{report}
    \input{include_start}
    \begin{document}
\fi

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

% START DOCUMENT
\begin{appendices}
\makeatletter

\lstset{language=C, 
numbers=left, 
frame=single, 
commentstyle=\color{dkgreen}, 
basicstyle={\scriptsize\ttfamily}, 
keywordstyle=\color{blue}, 
%identifierstyle=\color{blue}, 
stringstyle=\color{mauve},
captionpos=t,
showstringspaces=false,
breaklines=true,
breakatwhitespace=true,
tabsize=3,
caption={\protect\filename@parse{\lstname}\protect\filename@base\text{.}\protect\filename@ext},
}

\begin{frame}
% LIST FILES HERE:


\lstinputlisting{C:/Users/x/Project/trunk/User/Drivers/ADC.h}

\lstinputlisting{C:/Users/x/Project/trunk/User/Drivers/ADC.c}


\end{frame}
\end{appendices}

% END DOCUMENT
\ifdefined\docdefined
\else
    \input{include_end}
    \end{document}
\fi

I am pretty sure the syntax is wrong here:

   caption={\filename@parse{\lstname}\filename@base}

I am trying to parse \lstname and get the base.

Best Answer

You need \protect to protect both \filename@parse and \filename@base inside the moving argument of caption:

caption={\protect\filename@parse{\lstname}\protect\filename@area}
Related Question