我想为我的学生隐藏列表代码的某些部分并将其更改为白色矩形(例如)。此外,在布尔条件下,我想返回到没有间隙的版本。
对于文本,使用 dashundergaps 包很容易,但它不适用于 lstlisting。
\documentclass[a4paper,10pt,table]{book}
\usepackage{ifthen}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
answer=[]#How to hide this line ?
def myqyestion():
return answer
\end{lstlisting}
\end{document}
答案1
我尝试了这个解决方案(来自我如何隐藏(或模糊)列表中的某些代码?):
\documentclass{article}
\usepackage{ifthen}
\usepackage{tikz} %to be used with package below:
\usepackage[confidentiel]{optional}
\newlength\heightconf
\newlength\widthconf
\usepackage[confidentiel]{optional}
\newcommand\troualgo{}
\newboolean{etu}
\setboolean{etu}{false}
\ifetu
\renewcommand\troualgo[1]{%
\opt{confidentiel}{%
\settoheight{\heightconf}{#1}%
\settowidth{\widthconf}{#1}%
\tikz{\node[inner sep=0pt,rectangle,draw,anchor=base,textheight=\heightconf,textwidth=\widthconf,fill=white]{};}}
\opt{libre}{#1}}
\fi
\usepackage{listings}
\lstset{escapechar=\£}
\lstdefinestyle{python}{language=Python,basicstyle=\small\ttfamily,xleftmargin=0.3em,keywordstyle=\color{red}\bfseries\underbar,commentstyle=\color{blue},stringstyle=\ttfamily,showstringspaces=false,numbers=left,numberstyle=\itshape\bfseries\footnotesize,aboveskip=0.35cm,belowskip=0.5cm,backgroundcolor =\color{white},columns=[l]flexible,escapechar=\£}
%starting document
\begin{document}
\begin{lstlisting}[style=python]
for i in range(1,3):
a=b
£\troualgo{b=c;}£
c=d
£\troualgo{print(c);}£
\end{lstlisting}
\end{document}