在此 LaTeX 文档中插入带有缩进的代码
如何将代码插入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用
listings
包。LaTeX 标头的简单配置(在
\begin{document}
之前):您可以使用
\lstset{language=Java}
更改文档中间的默认语言。文档中的使用示例:
结果如下:
Use
listings
package.Simple configuration for LaTeX header (before
\begin{document}
):You can change default language in the middle of document with
\lstset{language=Java}
.Example of usage in the document:
Here's the result:
您还可以使用逐字环境
You could also use the verbatim environment
添加内联代码的方法如下:
您可以使用
{\tt code }
或\texttt{ code }
添加内联代码。如果您想格式化内联代码,那么最好创建自己的命令另外,请注意,代码块可以从其他文件加载,
不需要
breaklines
,但我发现它很有用。请注意,您必须指定\usepackage{
此列表}
。更新:列表包还包括
\lstinline
命令,它具有与\lstlisting
和\lstinputlisting相同的语法突出显示功能
命令(有关配置详细信息,请参阅 Cloudanger 的答案)。正如其他一些答案中提到的,还有 minted 包,它提供\mintinline
命令。与\lstinline
一样,\mintinline
提供与常规 minted 代码块相同的语法突出显示: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 commandAlso, note that code blocks can be loaded from other files with
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:专门的软件包,例如
minted
,它依赖 Pygments 进行格式化,提供与listings
包相比具有多种优势。引用minted
手册,Specialized packages such as
minted
, which relies on Pygments to do the formatting, offer various advantages over thelistings
package. To quote from theminted
manual,Minted,无论是来自 GitHub 还是 CTAN,综合 TeX 存档网络,在 Overleaf< /a>、TeX Live 和 MiKTeX。
它需要安装Python包Pygments;上述任一来源的文档对此进行了解释。尽管 Pygments 将自己打造成 Python 语法荧光笔,但 Minted 保证覆盖数百种其他语言。
示例:
输出:
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:
Output:
使用 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!
如果您的代码使用 Python,则一种非常简单的方法(我不必安装 Python 包)如下所示:
如下所示:
的是,这仅适用于 Python。
A very simple way if your code is in Python, where I didn't have to install a Python package, is the following:
which looks like:
Unfortunately, this only works for Python.
由于此处尚未提及,因此可能值得添加一个选项,包
spverbatim
(无语法高亮):
另外,如果不需要语法高亮,请打包
alltt
:
Since it wasn't yet mentioned here, it may be worth to add one more option, package
spverbatim
(no syntax highlighting):Also, if syntax highlighting is not required, package
alltt
:使用 Pygments !
Use Pygments !
对于那些使用列表和 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.