列表:将范围分隔符设为白色,并隐藏块注释

列表:将范围分隔符设为白色,并隐藏块注释

简单来说,我希望我的范围分隔符是白色的,这样当我显示一个代码块时它们就不会出现。我还想完全省略某些块注释。我读过手册,listings但不明白为什么这不起作用。

我使用 morecomment=[s][\color{white}]{\#~}{~\#}选项,标记与范围前缀/后缀相同。我还想使用选项morecomment=[is]{"""~}{~"""}来省略块注释(Python),并在其后和之前添加波浪号。我也尝试过在波浪号前添加一个黑斜线。

有什么见解吗?我觉得这应该很容易解决。

LaTeX 代码

\documentclass[11pt,a4paper]{article}

\usepackage{xcolor}
\usepackage{textcomp}
\usepackage{listings}  % 

\lstset{
    language=Python,
    basicstyle=\ttfamily\small\color{blue},% just to show that colors are working
    breaklines=true,
    tabsize=4,
    escapeinside={\#`}{`},
    includerangemarker=false,
    rangeprefix={\#~},
    rangesuffix={~\#},
%   moredelim=[s][\color{white}]{\#~}{~\#},% I want to make the range delimters white, this is not working
    morecomment=[s][\color{white}]{\#~}{~\#},% I tried usign more delim, and more comment to no avail
    morecomment=[is]{"""~}{~"""},  % I want to omit block comments (invisible), this is not working
}


\begin{document}
\section{Some Sample code boxes} 
\subsection{Excerpt of Code}
\lstinputlisting[
language=Python,
linerange=beg:def-end:def,
]{Ex.py}
\subsection{Full Code, delimiters hidden}
\lstinputlisting[%
caption={Some Python code},%
language=Python%
]{Ex.py}
\end{document}

Ex.py Python 代码

"""
Some block comment
"""
"""~
Some block comment I want omitted entirely
~"""

# I want the range delimiters below whited out so they do not appear on the full code block
#~beg:def~#
def y(x, a=1, b=1, c=1):
    return a*x**2 + b*x + c   #`\label{lin1}`
#~end:def~#

Some_other_code = 1

相关内容