简体   繁体   English

使用缩进在此 LaTeX 文档中插入代码

[英]Inserting code in this LaTeX document with indentation

How do I insert code into a LaTeX document?如何在 LaTeX 文档中插入代码? 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.语法高亮可能很好,尽管它绝对不是必需的。

Use listings package.使用listings包。

Simple configuration for LaTeX header (before \\begin{document} ): LaTeX 标头的简单配置(在\\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
}

You can change default language in the middle of document with \\lstset{language=Java} .您可以使用\\lstset{language=Java}更改文档中间的默认语言。

Example of usage in the document:文档中的用法示例:

\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}

Here's the result:结果如下:

示例图像

You could also use the verbatim environment您还可以使用逐字环境

\begin{verbatim}
your
code
example
\end{verbatim}

Here is how to add inline code:以下是添加内联代码的方法:

You can add inline code with {\\tt code } or \\texttt{ code } .您可以使用{\\tt code }\\texttt{ code }添加内联代码。 If you want to format the inline code, then it would be best to make your own command如果要格式化内联代码,那么最好制作自己的命令

\newcommand{\code}[1]{\texttt{#1}}

Also, note that code blocks can be loaded from other files with另外,请注意代码块可以从其他文件加载

\lstinputlisting[breaklines]{source.c}

breaklines isn't required, but I find it useful. breaklines不是必需的,但我觉得它很有用。 Be aware that you'll have to specify \\usepackage{ listings } for this one.请注意,您必须为此指定\\usepackage{ listings }

Update: The listings package also includes the \\lstinline command, which has the same syntax highlighting features as the \\lstlisting and \\lstinputlisting commands (see Cloudanger's answer for configuration details).更新: listings 包还包括\\lstinline命令,它具有与\\lstlisting\\lstinputlisting命令相同的语法突出显示功能(有关配置详细信息,请参阅 Cloudanger 的回答)。 As mentioned in a few other answers, there's also the minted package, which provides the \\mintinline command.正如其他一些答案中提到的,还有 minted 包,它提供了\\mintinline命令。 Like \\lstinline , \\mintinline provides the same syntax highlighting as a regular minted code block:\\lstinline一样, \\mintinline提供与常规铸造代码块相同的语法突出显示:

\documentclass{article}

\usepackage{minted}

\begin{document}
  This is a sentence with \mintinline{python}{def inlineCode(a="ipsum)}
\end{document}

Specialized packages such as minted , which relies on Pygments to do the formatting, offer various advantages over the listings package.专门的包,例如minted ,它依赖于 Pygments 进行格式化,与listings包相比具有各种优势。 To quote from the minted manual,引用minted手册,

Pygments provides far superior syntax highlighting compared to conventional packages.与传统的包相比,Pygments 提供了更优越的语法高亮。 For example, listings basically only highlights strings, comments and keywords.例如,列表基本上只突出显示字符串、评论和关键字。 Pygments, on the other hand, can be completely customized to highlight any token kind the source language might support.另一方面,Pygments 可以完全自定义以突出显示源语言可能支持的任何标记类型。 This might include special formatting sequences inside strings, numbers, different kinds of identifiers and exotic constructs such as HTML tags.这可能包括字符串、数字、不同类型的标识符和外来结构(如 HTML 标签)中的特殊格式序列。

Minted , whether from GitHub or CTAN, the Comprehensive TeX Archive Network , works in Overleaf , TeX Live and MiKTeX. Minted ,无论是来自GitHub还是CTAN,综合 TeX 档案网络,在Overleaf 、TeX Live 和 MiKTeX 中工作。

It requires the installation of the Python package Pygments ;它需要安装 Python 包Pygments this is explained in the documentation in either source above.这在上述任一来源的文档中都有解释。 Although Pygments brands itself as a Python syntax highlighter, Minted guarantees the coverage of hundreds of other languages.尽管 Pygments 将自己标榜为 Python 语法高亮工具,但 Minted 保证了数百种其他语言的覆盖范围。

Example:例子:

\documentclass{article}
\usepackage{minted}
\begin{document}

\begin{minted}[mathescape, linenos]{python}

# Note: $\pi=\lim_{n\to\infty}\frac{P_n}{d}$
title = "Hello World"

sum = 0
for i in range(10):
 sum += i

\end{minted}

\end{document}

Output:输出:

在此处输入图片说明

Use Minted .使用Minted

It's a package that facilitates expressive syntax highlighting in LaTeX using the powerful Pygments library.这是一个使用强大的Pygments库在 LaTeX 中促进表达性语法突出显示的包。 The package also provides options to customize the highlighted source code output using fancyvrb .该软件包还提供了使用fancyvrb自定义突出显示的源代码输出的选项

It's much more evolved and customizable than any other package!它比任何其他软件包都更加先进和可定制!

Since it wasn't yet mentioned here, it may be worth to add one more option, package spverbatim (no syntax highlighting):由于这里还没有提到它,可能值得再添加一个选项,包spverbatim (无语法高亮):

\documentclass{article}
\usepackage{spverbatim}

\begin{document}

\begin{spverbatim}
  Your code here
\end{spverbatim}

\end{document}

Also, if syntax highlighting is not required, package alltt :此外,如果不需要语法高亮,请打包alltt

\documentclass{article}
\usepackage{alltt}

\begin{document}

\begin{alltt}
  Your code here
\end{alltt}

\end{document}

使用Pygments

A very simple way if your code is in Python, where I didn't have to install a Python package, is the following:如果您的代码在 Python 中,我不必安装 Python 包,那么一种非常简单的方法是:

\documentclass[11pt]{article}  
\usepackage{pythonhighlight}

\begin{document}

The following is some Python code

\begin{python}
# A comment
x = [5, 7, 10]
y = 0

for num in x:
    y += num
    
print(y)
\end{python}

\end{document}

which looks like:看起来像: 在此处输入图片说明

Unfortunately, this only works for Python.不幸的是,这仅适用于 Python。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM