清單:將範圍分隔符號設為白色,並隱藏區塊註釋

清單:將範圍分隔符號設為白色,並隱藏區塊註釋

簡而言之,我希望範圍分隔符號是白色的,這樣當我顯示程式碼區塊時它們就不會出現。我還想完全省略某些區塊註解。我已閱讀listings手冊,但不明白為什麼這不起作用。

我使用該 morecomment=[s][\color{white}]{\#~}{~\#}選項,其標記與範圍前綴/後綴的標記相同。我還想使用morecomment=[is]{"""~}{~"""}選項來省略區塊註解(Python),並在前後加上波浪號。我也嘗試在波浪號之前加一個黑斜杠。

有什麼見解嗎?我覺得這應該很容易解決。

乳膠代碼

\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

相關內容