仅让文件名显示在列表环境中

仅让文件名显示在列表环境中

../Matlabcode/sol2_adx.m如果实际列表文件位于另一个位置,而不是带有完整路径的文件,那么如何仅让文件名(有时带有下划线“_”)显示在代码环境中:

在此处输入图片描述

在此示例中,代码的标题应为sol2_adx.m

以下是代码:

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

\begin{filecontents*}{sol2_adx.m}
% 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}

\inputmatlab{Expansion Algorithm}{sol2_adx.m}    
\end{document} 

答案1

从文件路径参数中提取基本名称如何显示带有下划线的文件名。这里有一个简化的 MWE,展示了如何做你想做的事情。

在此示例中sol2_adx.m,它被放置在名为的子目录中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}
\inputmatlab{Expansion Algorithm}{subdir/sol2_adx.m}    
\end{document} 

在此处输入图片描述

相关内容