latex代码块模版
时间: 2025-01-25 07:08:01 浏览: 41
### LaTeX 代码块模板示例
为了在 LaTeX 文档中插入代码块,通常会使用 `listings` 或者 `minted` 这样的宏包。以下是利用 `listings` 宏包创建 Python 代码块的一个例子[^1]。
```tex
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
% 设置代码样式
\lstset{
language=Python,
basicstyle=\ttfamily\footnotesize,
keywordstyle=\color{blue},
commentstyle=\color{green!60!black},
stringstyle=\color{red},
showstringspaces=false,
breaklines=true,
frame=single,
numbers=left,
numbersep=5pt,
xleftmargin=20pt,
framexleftmargin=15pt
}
\begin{document}
\section*{Python Code Example}
Here is a simple example of how to define and use functions in Python:
\begin{lstlisting}[language=Python]
def greet(name):
"""This function greets the person passed as an argument."""
print(f'Hello, {name}')
greet('Alice')
\end{lstlisting}
\end{document}
```
上述配置设置了代码的颜色、字体大小以及边框等属性,并且启用了自动换行功能和左侧编号显示。对于其他编程语言的支持只需要更改 `\lstset` 中的 `language` 参数即可适应不同的语法高亮需求。
阅读全文
相关推荐


















