在此 LaTeX 文档中插入带有缩进的代码

发布于 2024-09-08 03:36:45 字数 154 浏览 2 评论 0原文

如何将代码插入 LaTeX 文档?有没有类似的东西:

\begin{code}## Heading ##
...
\end{code}

我真正需要的唯一东西是缩进和固定宽度的字体。语法突出显示可能会很好,尽管它绝对不是必需的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

森末i 2024-09-15 03:36:45

使用 listings 包。

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
}

您可以使用 \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}

结果如下:

Example image

Use listings package.

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
}

You can change default language in the middle of document with \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:

Example image

情场扛把子 2024-09-15 03:36:45

您还可以使用逐字环境

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

You could also use the verbatim environment

\begin{verbatim}
your
code
example
\end{verbatim}
绅刃 2024-09-15 03:36:45

添加内联代码的方法如下:

您可以使用 {\tt code }\texttt{ code } 添加内联代码。如果您想格式化内联代码,那么最好创建自己的命令

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

另外,请注意,代码块可以从其他文件加载,

\lstinputlisting[breaklines]{source.c}

不需要 breaklines ,但我发现它很有用。请注意,您必须指定 \usepackage{ 此列表 }

更新:列表包还包括\lstinline命令,它具有与\lstlisting\lstinputlisting相同的语法突出显示功能 命令(有关配置详细信息,请参阅 Cloudanger 的答案)。正如其他一些答案中提到的,还有 minted 包,它提供 \mintinline 命令。与 \lstinline 一样,\mintinline 提供与常规 minted 代码块相同的语法突出显示:

\documentclass{article}

\usepackage{minted}

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

Here is how to add inline code:

You can add inline code with {\tt code } or \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. Be aware that you'll have to specify \usepackage{ listings } for this one.

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). As mentioned in a few other answers, there's also the minted package, which provides the \mintinline command. Like \lstinline, \mintinline provides the same syntax highlighting as a regular minted code block:

\documentclass{article}

\usepackage{minted}

\begin{document}
  This is a sentence with \mintinline{python}{def inlineCode(a="ipsum)}
\end{document}
三生路 2024-09-15 03:36:45

专门的软件包,例如 minted,它依赖 Pygments 进行格式化,提供与 listings 包相比具有多种优势。引用 minted 手册,

与传统包相比,Pygments 提供了更加优越的语法突出显示。例如,列表基本上只突出显示字符串、评论和关键字。另一方面,Pygments 可以完全自定义以突出显示源语言可能支持的任何标记类型。这可能包括字符串、数字、不同类型的标识符和外来结构(例如 HTML 标记)内的特殊格式序列。

Specialized packages such as minted, which relies on Pygments to do the formatting, offer various advantages over the listings package. To quote from the minted manual,

Pygments provides far superior syntax highlighting compared to conventional packages. 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. This might include special formatting sequences inside strings, numbers, different kinds of identifiers and exotic constructs such as HTML tags.

妄断弥空 2024-09-15 03:36:45

Minted,无论是来自 GitHub 还是 CTAN,综合 TeX 存档网络,在 Overleaf< /a>、TeX Live 和 MiKTeX。

它需要安装Python包Pygments;上述任一来源的文档对此进行了解释。尽管 Pygments 将自己打造成 Python 语法荧光笔,但 Minted 保证覆盖数百种其他语言。

示例:

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

输出:

在此处输入图像描述

Minted, whether from GitHub or CTAN, the Comprehensive TeX Archive Network, works in Overleaf, TeX Live and MiKTeX.

It requires the installation of the Python package 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.

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:

enter image description here

萌梦深 2024-09-15 03:36:45

使用 Minted

它是一个使用强大的 Pygments 库促进 LaTeX 中富有表现力的语法突出显示的包。该软件包还提供了使用 fancyvrb 自定义突出显示的源代码输出的选项。

它比任何其他软件包都更加先进和可定制!

Use Minted.

It's a package that facilitates expressive syntax highlighting in LaTeX using the powerful Pygments library. The package also provides options to customize the highlighted source code output using fancyvrb.

It's much more evolved and customizable than any other package!

一场信仰旅途 2024-09-15 03:36:45

如果您的代码使用 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}

如下所示:

的是,这仅适用于 Python。

A very simple way if your code is in Python, where I didn't have to install a Python package, is the following:

\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:
enter image description here

Unfortunately, this only works for Python.

小苏打饼 2024-09-15 03:36:45

由于此处尚未提及,因此可能值得添加一个选项,包 spverbatim(无语法高亮):

\documentclass{article}
\usepackage{spverbatim}

\begin{document}

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

\end{document}

另外,如果不需要语法高亮,请打包 alltt

\documentclass{article}
\usepackage{alltt}

\begin{document}

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

\end{document}

Since it wasn't yet mentioned here, it may be worth to add one more option, package spverbatim (no syntax highlighting):

\documentclass{article}
\usepackage{spverbatim}

\begin{document}

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

\end{document}

Also, if syntax highlighting is not required, package alltt:

\documentclass{article}
\usepackage{alltt}

\begin{document}

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

\end{document}
静若繁花 2024-09-15 03:36:45

使用 Pygments

Use Pygments !

雄赳赳气昂昂 2024-09-15 03:36:45

对于那些使用列表和 VSCode 的人来说,.tex 文件中的缩进会影响 pdf 文件中的缩进,将空格缩进转换为制表符缩进将解决缩进问题。

For those who use listings and VSCode and your indentation in the .tex file affects your indentation in pdf file , convert space indentation to tab indentation will fix the indentation problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文