我希望有人能帮助我让下面的标题\newtcbinputlisting
在目录中以不同的方式显示。例如,对于图片标题,用户可以输入
...
\caption[MakeMaze output]{MakeMaze output with a value of 10}%
...
其中“MakeMaze ouptput”出现在目录中,并且“MakeMaze 输出值为 10”作为图形标题出现。
下面的代码使用命令\inputmatlab{Expansion Algorithm}{sample.m}
,其中“扩展算法”将作为 Matlab 代码下的标题出现。我想让目录中出现不同的标题。谢谢!
以下是代码:
\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\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{mygray}{RGB}{243,243,244}
\definecolor{mainColor}{RGB}{211, 47, 47}
\usepackage[numbered,framed]{matlab-prettifier}
\renewcommand\lstlistingname{Code}
\lstset{
language=Python,
numbers=left,
numbersep= 7mm,
numberstyle=\color{Black},
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
basicstyle=\color{Black}\ttfamily,
commentstyle=\color{LimeGreen},
keywordstyle=\color{BurntOrange}\bfseries,
stringstyle=\color{WildStrawberry},
keywords={var, func, extends},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip
}
\usepackage{tcolorbox}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}
\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\small},
after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
%
listing file={#3},#1}
\begin{filecontents*}{sample.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}{sample.m}
\end{document}
答案1
这是快捷方法,使用list entry=#4
其中#4
为代码列表设计的标题的替代标题。
\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\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{mygray}{RGB}{243,243,244}
\definecolor{mainColor}{RGB}{211, 47, 47}
\usepackage[numbered,framed]{matlab-prettifier}
\renewcommand\lstlistingname{Code}
\lstset{
language=Python,
numbers=left,
numbersep= 7mm,
numberstyle=\color{Black},
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
basicstyle=\color{Black}\ttfamily,
commentstyle=\color{LimeGreen},
keywordstyle=\color{BurntOrange}\bfseries,
stringstyle=\color{WildStrawberry},
keywords={var, func, extends},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip
}
\usepackage[most]{tcolorbox}
\newcounter{matlab}
\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}[4][]{%
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\small},list entry=#4,
after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
%
listing file={#3},#1
}
\begin{filecontents*}{sample.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}
\tcblistof{matlab}{List of codes}
\inputmatlab{Expansion Algorithm}{sample.m}{This is foo}
\end{document}
更新
更好的方法\DeclareTCBInputListing
,允许改进可选参数支持xparse
:
用法\inputmatlab[listing etc options]{Description}[optional description for list of matlab codes]{file name}
。
此处的选项list entry
决定tcolorbox
将哪些内容写入相关文件.matlab
。如果list entry
未使用,tcolorbox
则尝试使用该选项的值title
,如果也未设置,则将应用当前框号和环境名称并将其存储到文件中。
只要更新相关参数编号,可选描述的可选参数的顺序是任意的。
\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\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{mygray}{RGB}{243,243,244}
\definecolor{mainColor}{RGB}{211, 47, 47}
\usepackage[numbered,framed]{matlab-prettifier}
\renewcommand\lstlistingname{Code}
\lstset{
language=Python,
numbers=left,
numbersep= 7mm,
numberstyle=\color{Black},
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
basicstyle=\color{Black}\ttfamily,
commentstyle=\color{LimeGreen},
keywordstyle=\color{BurntOrange}\bfseries,
stringstyle=\color{WildStrawberry},
keywords={var, func, extends},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip
}
\usepackage[most]{tcolorbox}
\newcounter{matlab}
\DeclareTCBInputListing[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}{O{}+m+O{#2}+m}{%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#4},%
title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor,basicstyle=\ttfamily\small},list entry=#3,
after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
%
listing file={#4},#1
}
\begin{filecontents*}{sample.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}
\tcblistof{matlab}{List of codes}
\inputmatlab{Expansion Algorithm}[Other title]{sample.m}
\end{document}