我如何扩展 \lstdefinestyle 的属性?

我如何扩展 \lstdefinestyle 的属性?

我正在将多个 Python 脚本导入我的 tex 文件。我之前有一个巨大的\lstdefinestyle定义包括了我所有脚本中所有特定函数、变量等的选项。但是,我想将其拆分开来,因为我知道你可以组合多个\lstdefinestyle调用。我的目标是有一个基本风格vscode_py它包含所有“基本”定义,然后有单独的\lstdefinestyle调用每个导入的脚本,其中包含脚本特定的变量和函数。我认为我可以通过添加更多选项来实现这一点强调风格 = [#]在单独的调用中。不幸的是,我的输出不使用任​​何特定于脚本的颜色定义。最后,如果这不是最好的方法,那是什么,为什么?提前致谢!

\documentclass[answers]{exam}

\usepackage[top=20mm, left=15mm, right=15mm]{geometry}
\usepackage{multicol}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{listings}

\definecolor{mygreen}{RGB}{28,172,0}

% the following are for VSCODE
\definecolor{black}{cmyk}{0,0,0,1}
\definecolor{darkgrey}{cmyk}{0,0,0,0.97}          % background
\definecolor{green}{cmyk}{0.4,0,0.8,0.2}          % comments
\definecolor{blue}{cmyk}{0.65,0.33,0,0.05}        % keywords
\definecolor{rose}{cmyk}{0,0.26,0.38,0}           % strings
\definecolor{lavender}{cmyk}{0,0.42,0,0.1}        % conditionals
\definecolor{lightblue}{cmyk}{0.35,0,0,0}         % local variables
\definecolor{aqua}{cmyk}{0.65,0,0.23,0}           % class types

\lstdefinestyle{vscode_py}{
    showstringspaces = false,
    basicstyle = \ttfamily\footnotesize\color{white},
    frame   = single,
    numbers = left,
    numberstyle = \tiny\color{black},
    xleftmargin  = 7pt,
    xrightmargin = 5pt,
    backgroundcolor = \color{darkgrey},
    commentstyle = \color{green},
    stringstyle  = \color{rose},
    keywordstyle = [1]\color{blue},
    keywordstyle = [2]\color{yellow!40},
    morekeywords = [2]{__init__, 
        write, append, insert, concatenate,
        array, linspace, arange, quiver, plot, subplot, legend},
    emphstyle = [1]\color{lavender},
    emph      = [1]{if, else, for, in, 
        with, continue, return, 
        import, from, as},
    emphstyle = [2]\color{lightblue},
    emph      = [2]{__name__},
    emphstyle = [3]\color{aqua},
    emph      = [3]{numpy, np, matplotlib, pyplot, plt,
        range, int, list, linalg}
    }


\lstdefinestyle{first_style}{
    emphstyle = [2]\color{lightblue},
    emph      = [2]{
        A, U, S, Vh, T, AX, AY, Circ,
        t, x, y, cos, sin, pi,
        ax1, ax2, w1, w2, xlim, ylim,
        color, angles, scale_units, scale
        },
    style = vscode_py
}

\lstdefinestyle{second_style}{
    emphstyle = [2]\color{lightblue},
    emph      = [2]{A, A1, S, U, V, n2},
    style = vscode_py
}

%==========================================================

\begin{document}

\begin{multicols}{2}
    \lstinputlisting[
        language = Python, style = first_style,
        firstline = 4, lastline = 28,
        firstnumber = 1,
        breaklines = true
        ]{./unit_sphere_image.py}
\end{multicols}
        
\lstinputlisting[
    language = Python, style = second_style,
    firstline = 7, lastline = 8,
    firstnumber = 1,
    breaklines = true
    ]{./closest_matrix.py}
    
\end{document}

Python 文件如下:

单位球体图像.py

import numpy as np
from matplotlib import pyplot as plt

A = np.array([[1,2],[0,2]])
U, S, Vh = np.linalg.svd(A)
V = Vh.T

t = np.arange(0, 2*np.pi, 0.01)
x = np.cos(t)
y = np.sin(t)
    
ax1 = plt.subplot(1,2,1, xlim=(-2,2), ylim=(-2,2))
ax1.plot(x, y, 'c')
ax1.quiver(*V[:,0], color='r', angles='xy', scale_units='xy', scale=1)
ax1.quiver(*V[:,1], color='b', angles='xy', scale_units='xy', scale=1)
ax1.legend(labels=['B', r'$v_1$', r'$v_2$'])

Circ = np.array([x,y])
AX = A[0].dot(Circ)
AY = A[1].dot(Circ)

w1 = S[0]*U[:,0]
w2 = S[1]*U[:,1]

ax2 = plt.subplot(1,2,2)
ax2.plot(AX, AY, 'c')
ax2.quiver(*w1, color='r', angles='xy', scale_units='xy', scale=1)
ax2.quiver(*w2, color='b', angles='xy', scale_units='xy', scale=1)
ax2.legend(labels=['AB', r'$\phi_1 u_1$', r'$\phi_2 u_2$'])

plt.savefig('unit_sphere_image.png')

最近矩阵.py

import numpy as np

A = np.array([[1,2],[0,2]])
U, S, Vh = np.linalg.svd(A)
V = Vh.T

A1 = S[0] * U[:,0].reshape(2,1) @ V[:,0].reshape(1,2)
n2 = np.linalg.norm(A - A1, ord=2)
print(A1, n2)

输出如下所示: 在此处输入图片描述

答案1

问题是,当您emph在 中调用键时first_style,您会从 覆盖其值vscode_py。要添加关键字,请使用moreemph键。请参阅listings更多解释。此外,您需要vscode_py先加载样式,以便其对的调用emph不会覆盖您对的调用moreemph

\documentclass[answers]{exam}

\usepackage[top=20mm, left=15mm, right=15mm]{geometry}
\usepackage{multicol}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{listings}

\definecolor{mygreen}{RGB}{28,172,0}

% the following are for VSCODE
\definecolor{black}{cmyk}{0,0,0,1}
\definecolor{darkgrey}{cmyk}{0,0,0,0.97}          % background
\definecolor{green}{cmyk}{0.4,0,0.8,0.2}          % comments
\definecolor{blue}{cmyk}{0.65,0.33,0,0.05}        % keywords
\definecolor{rose}{cmyk}{0,0.26,0.38,0}           % strings
\definecolor{lavender}{cmyk}{0,0.42,0,0.1}        % conditionals
\definecolor{lightblue}{cmyk}{0.35,0,0,0}         % local variables
\definecolor{aqua}{cmyk}{0.65,0,0.23,0}           % class types

\lstdefinestyle{vscode_py}{
    showstringspaces = false,
    basicstyle = \ttfamily\footnotesize\color{white},
    frame   = single,
    numbers = left,
    numberstyle = \tiny\color{black},
    xleftmargin  = 7pt,
    xrightmargin = 5pt,
    backgroundcolor = \color{darkgrey},
    commentstyle = \color{green},
    stringstyle  = \color{rose},
    keywordstyle = [1]\color{blue},
    keywordstyle = [2]\color{yellow!40},
    morekeywords = [2]{__init__, 
        write, append, insert, concatenate,
        array, linspace, arange, quiver, plot, subplot, legend},
    emphstyle = [1]\color{lavender},
    emph      = [1]{if, else, for, in, 
        with, continue, return, 
        import, from, as},
    emphstyle = [2]\color{lightblue},
    emph      = [2]{__name__},
    emphstyle = [3]\color{aqua},
    emph      = [3]{numpy, np, matplotlib, pyplot, plt,
        range, int, list, linalg}
    }


\lstdefinestyle{first_style}{
    style = vscode_py,
    emphstyle = [2]\color{lightblue},
    moreemph  = [2]{
        A, U, S, Vh, T, AX, AY, Circ,
        t, x, y, cos, sin, pi,
        ax1, ax2, w1, w2, xlim, ylim,
        color, angles, scale_units, scale
        }
}

\lstdefinestyle{second_style}{
    style = vscode_py,
    emphstyle = [2]\color{lightblue},
    moreemph  = [2]{A, A1, S, U, V, n2},
}

%==========================================================

\begin{document}

\begin{multicols}{2}
    \lstinputlisting[
        language = Python, style = first_style,
        firstline = 4, lastline = 28,
        firstnumber = 1,
        breaklines = true
        ]{./unit_sphere_image.py}
\end{multicols}
        
\lstinputlisting[
    language = Python, style = second_style,
    firstline = 7, lastline = 8,
    firstnumber = 1,
    breaklines = true
    ]{./closest_matrix.py}
    
\end{document}

相关内容