这个问题是基于找到的代码和解决方案这里。我想添加指向实际代码文件(Matlab 或 Python 代码文件)的选项,并将其放入我的选项中。我有当前命令
\begin{mcode}{Some Matlab Code}{matcode1.m}
它指定这是一个 Matlab 环境mcode
、脚本的名称(在本例中为“一些 Matlab 代码”)以及最后文件的路径。
由于某种原因,filecontents
内容没有出现在各自的环境中,我只是输出这些“空”单元格:
你能告诉我我需要做什么才能让文件的内容显示在各自的环境中吗?谢谢!
\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}
\usepackage[no-math]{fontspec}
\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\begin{filecontents*}{matcode1.m}
V = diag([0.02, 0.5*pi/180].^2);
P0 = diag([0.005, 0.005, 0.001].^2);
W = diag([0.1, 1*pi/180].^2);
map = LandmarkMap(20);
veh = Bicycle('covar', V);
veh.add_driver( RandomPath(map.dim) );
\end{filecontents*}
\begin{filecontents*}{pycode1.py}
"""
===============
Many plot types can be combined in one figure to create
powerful and flexible representations of data.
"""
import matplotlib.pyplot as plt
import numpy as np
# np.random.seed(19680801)
# data = np.random.randn(2, 100)
#
# fig, axs = plt.subplots(2, 2, figsize=(5, 5))
# axs[0, 0].hist(data[0])
# axs[1, 0].scatter(data[0], data[1])
# axs[0, 1].plot(data[0], data[1])
# axs[1, 1].hist2d(data[0], data[1])
fig, ax = plt.subplots()
plt.plot(x, y, label='Loaded from file!')
plt.fill_between(x, y, color='#539ecd')
\end{filecontents*}
\definecolor{light-gray}{gray}{0.92}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\usepackage[numbered,framed]{matlab-prettifier}
\renewcommand\lstlistingname{Python Code}
\lstdefinestyle{pycode}{
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, import},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip,
basicstyle=\ttfamily\scriptsize,
}
\lstdefinestyle{mcode}{
style = {Matlab-editor},
numbers=left,
numbersep= 7mm,
numberstyle=\color{Gray!80}\scriptsize,
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip,
%backgroundcolor=\color{cyan!20},
basicstyle=\ttfamily\scriptsize,
}
%\lstnewenvironment{myPyCode}
%{\lstset{style=pycode}}
%{}
%
%\lstnewenvironment{myMatCode}
%{\lstset{style=mcode}}
%{}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,listings}
\newcounter{pylst}
\newtcblisting[use counter=pylst]{pycode}[3][]{%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em}\includegraphics[height=\baselineskip]{example-image-a}~#2},%
title after break={\centering\footnotesize\itshape\strut\lstlistingname~\thelstlisting~--~continued},%
listing only,listing options={style=pycode, xleftmargin=-1mm},after upper={\centering\strut Python Code~\thetcbcounter:~#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);},
listing file = {#3}, #1}
\newcounter{mlst}
\newtcblisting[use counter=mlst]{mcode}[3][]{%
enhanced,noparskip,breakable,colback=cyan!20,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em}\includegraphics[height=\baselineskip]{example-image-b}~#2},%
title after break={\centering\footnotesize\itshape\strut\lstlistingname~\thelstlisting~--~continued},%
listing only,listing options={style=mcode, xleftmargin=-1mm},after upper={\centering\strut Matlab Code~\thetcbcounter:~#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);},
listing file = {#3}, #1}
\begin{document}
\begin{mcode}{Some Matlab Code}{matcode1.m}
\end{mcode}
\begin{pycode}{Some Python Code}{pycode1.py}
\end{pycode}
\end{document}
答案1
基本上,您需要使用\newtcbinputlisting
而不是\newtcblisting
。后者期望列表内容作为您定义的环境的主体(在您的情况下为空)。另一方面,\newtcbinputlisting
使用 命名的文件/tcb/listing file
作为输入。
在运行以下代码之前,请确保matcode1.m
和pycode1.py
不存在于运行 LaTeX 的目录中,因为之前运行的\newtcblisting
会将它们截断为零大小,并且filecontents
或filecontents*
不带overwrite
选项不会重写它们。或者,\begin{filecontents}[overwrite,noheader]{〈file name〉} ... \end{filecontents}
如果您的 LaTeX 内核是 2019 年 10 月或更高版本(取代了该filecontents
软件包,请参阅LaTeX 新闻第 30 期)。
(\RequirePackage{filecontents}
如果你的 LaTeX 内核太旧,则只需将其添加到文件的最顶部即可)
\begin{filecontents*}{matcode1.m}
V = diag([0.02, 0.5*pi/180].^2);
P0 = diag([0.005, 0.005, 0.001].^2);
W = diag([0.1, 1*pi/180].^2);
map = LandmarkMap(20);
veh = Bicycle('covar', V);
veh.add_driver( RandomPath(map.dim) );
\end{filecontents*}
\begin{filecontents*}{pycode1.py}
"""
===============
Many plot types can be combined in one figure to create
powerful and flexible representations of data.
"""
import matplotlib.pyplot as plt
import numpy as np
# np.random.seed(19680801)
# data = np.random.randn(2, 100)
#
# fig, axs = plt.subplots(2, 2, figsize=(5, 5))
# axs[0, 0].hist(data[0])
# axs[1, 0].scatter(data[0], data[1])
# axs[0, 1].plot(data[0], data[1])
# axs[1, 1].hist2d(data[0], data[1])
fig, ax = plt.subplots()
plt.plot(x, y, label='Loaded from file!')
plt.fill_between(x, y, color='#539ecd')
\end{filecontents*}
\documentclass{article}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{listings}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,listings}
\definecolor{light-gray}{gray}{0.92}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\renewcommand\lstlistingname{Python Code}
\lstdefinestyle{pycode}{
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, import},
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip,
basicstyle=\ttfamily\scriptsize,
}
\lstdefinestyle{mcode}{
style = {Matlab-editor},
numbers=left,
numbersep= 7mm,
numberstyle=\color{Gray!80}\scriptsize,
stepnumber=1,
tabsize=3,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
frame=leftline,
framesep=0mm,
xleftmargin=3mm,
framesep=2mm,
framerule=0mm,
abovecaptionskip=5mm,
aboveskip=\baselineskip,
belowskip=\baselineskip,
basicstyle=\ttfamily\scriptsize,
}
\newcounter{pylst}
\newtcbinputlisting[use counter=pylst]{\pycode}[3][]{%
enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em}\includegraphics[height=\baselineskip]{example-image-a}~#2},%
title after break={\centering\footnotesize\itshape\strut\lstlistingname~\thelstlisting~--~continued},%
listing only,listing options={style=pycode, xleftmargin=-1mm},after upper={\centering\strut Python Code~\thetcbcounter:~#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);},
listing file = {#3}, #1}
\newcounter{mlst}
\newtcbinputlisting[use counter=mlst]{\mcode}[3][]{%
enhanced,noparskip,breakable,colback=cyan!20,colframe=DarkSlateGray,opacitybacktitle=.8,%
fonttitle=\bfseries,before upper={\hspace*{-1em}\includegraphics[height=\baselineskip]{example-image-b}~#2},%
title after break={\centering\footnotesize\itshape\strut\lstlistingname~\thelstlisting~--~continued},%
listing only,listing options={style=mcode, xleftmargin=-1mm},after upper={\centering\strut Matlab Code~\thetcbcounter:~#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);},
listing file = {#3}, #1}
\begin{document}
\mcode{Some Matlab Code}{matcode1.m}
\pycode{Some Python Code}{pycode1.py}
\end{document}