本文介绍了使用缩进在此LaTeX文档中插入代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将代码插入LaTeX文档?是否有类似的东西:
How do I insert code into a LaTeX document? Is there something like:
\begin{code}## Heading ##
...
\end{code}
我唯一需要的是缩进和固定宽度的字体.语法高亮显示可能很好,尽管绝对不是必需的.
The only thing that I really need is indentation and a fixed width font. Syntax highlighting could be nice although it is definitely not required.
推荐答案
使用 listings
包.
LaTeX标头的简单配置(在\begin{document}
之前):
Simple configuration for LaTeX header (before \begin{document}
):
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
language=Java,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=3
}
您可以使用\lstset{language=Java}
更改文档中间的默认语言.
You can change default language in the middle of document with \lstset{language=Java}
.
文档中的用法示例:
\begin{lstlisting}
// Hello.java
import javax.swing.JApplet;
import java.awt.Graphics;
public class Hello extends JApplet {
public void paintComponent(Graphics g) {
g.drawString("Hello, world!", 65, 95);
}
}
\end{lstlisting}
结果如下:
这篇关于使用缩进在此LaTeX文档中插入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!