# Only get filename and extension of listing, not whole path

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,
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:

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

• can you not just use \lstinputlisting{ADC.h} (and then arrange that C:/Users/x/Project/trunk/User/Drivers/ is in TEXINPUTS ? – David Carlisle Apr 30 '14 at 18:56
• It says it can not find the files. "! Package Listings Error: File ADC(.h)' not found." – uniquenamehere Apr 30 '14 at 19:03
• Please (always) make your code fragments a complete document showing all packages used, so people can test their answers. An unprocessable fragment is less helpful. – David Carlisle Apr 30 '14 at 19:04
• I don't see how listings could fail to find the file if it's in your texinputs path. – David Carlisle Apr 30 '14 at 19:04
• Complete code now shown in question. – uniquenamehere Apr 30 '14 at 19:14

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}

• Thank you! This worked. I will update question. I used to line: caption={\protect\filename@parse{\lstname}\protect\filename@base\text{.}\protect\filename@ext} to get filename.extension. – uniquenamehere Apr 30 '14 at 19:19

I would avoid a complicated set of macros as the value of \caption; by defining a separate command, you can easily add adjustments. Here I fixed the behavior of listings that changes hyphens into \textendash and added \texttt around the file name.

Note, though, that you won't be able to compile correctly the list of listings.

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

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

\makeatletter
\DeclareRobustCommand{\getlstname}{%
\begingroup
% \lstname seems to change hyphens into \textendash
\def\textendash{-}%
\filename@parse{\lstname}%
\texttt{\filename@base.\filename@ext}%
\endgroup
}
\makeatother

% START DOCUMENT

\lstset{
language=C,
numbers=left,
frame=single,
basicstyle={\scriptsize\ttfamily},
keywordstyle=\color{blue},
%identifierstyle=\color{blue},
stringstyle=\color{mauve},
captionpos=t,
showstringspaces=false,
breaklines=true,
breakatwhitespace=true,
tabsize=3,
caption=\getlstname,
}

\begin{document}

\lstinputlisting{/Users/enrico2013/primaria2014-02-04.tex}

\end{document}


I used one of my LaTeX files for the example.

is produced from

\documentclass{article}
\usepackage{color,listings}

\lstset{language=C,
numbers=left,
frame=single,
basicstyle={\scriptsize\ttfamily},
keywordstyle=\color{blue},
%identifierstyle=\color{blue},
stringstyle=\color{red},
captionpos=t,
showstringspaces=false,
breaklines=true,
breakatwhitespace=true,
tabsize=3,
caption={\lstname},
}

\begin{document}

\lstinputlisting{io.h}

\end{document}


You only need do give the filename not the full path so long as you use a command such as

TEXINPUTS=/usr/include: pdflatex list22


or equivalent in whatever command shell you use (the above is bash syntax)

• If I understand correctly the TEXTINPUTS is to be written in a cmd line and not as part of the .tex document? – uniquenamehere Apr 30 '14 at 19:27
• @Phataas yes or a texmf.cnf config file, or in your system environment settings. Basically listings uses the same mechanism to find foo.h as \documentclass uses to find article.cls You don't specify a full path to article.cls normally. – David Carlisle Apr 30 '14 at 19:29

You can change the searching path, and then in the caption you will have only the filename.

This works for me:

\newcommand{\cfile}[2][]{
\lstinputlisting[style=customc, caption={\texttt{\detokenize{#2}}},#1]{#2}
}

\lstset{inputpath=some/funny/path}

...

\cfile{file_name.c}


The file is located in some/funny/path/file_name.c. customc is a custom style, you can delete it. The \texttt and the \detokenize are for the special characters in the file name and the first optional parameter of the \newcommand is for adding features when use the command, like for instance \cfile[basicstyle=\ttfamily]{file_name.c}`.