答案1
使用我的答案带垂直线的伪代码,修改后规则颜色变为灰色。
\documentclass{article}
\usepackage{xcolor}
\def\lindent{.5ex}
\def\rindent{2ex}
\def\rthk{1.5pt}
\newcommand\block[3]{\unskip%
\noindent\parbox[b]{\textwidth}{\strut\ignorespaces#1\strut}\\
\setbox0=\hbox{\parbox[b]{\textwidth}{\strut\ignorespaces#2\strut}}%
\hspace*{\lindent}\textcolor{gray!50}{\rule[-.5\dp\strutbox]{\rthk}{\ht0}}%
\hspace{\rindent}\box0\\\parbox[b]{\textwidth}{\strut\ignorespaces#3\strut}%
\ignorespaces}
\begin{document}
\ttfamily
\block{
def foo():
}{
\block{
if something
}{
do something\\
do more things
}{
do something last}
}
{}
\end{document}
答案2
基于的快速解决方案algorithm2e 的软件包文档
\documentclass{standalone}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetStartEndCondition{ }{}{}%
\SetKwProg{Fn}{def}{\string:}{}
\SetKwIF{If}{ElseIf}{Else}{if}{:}{elif}{else:}{}%
\SetKwFunction{FnFoo}{foo}
\DontPrintSemicolon
\SetAlgoLined
\Fn{\FnFoo{}}{
\If {something}{
\FuncSty{Do} something\;
\FuncSty{Do} something\;
}
\FuncSty{Do} something last\;
}
\end{algorithm}
\end{document}