如何使用“listings”在 LaTeX 文档中嵌入 LaTeX 关键字

发布于 2024-10-30 13:28:25 字数 389 浏览 5 评论 0原文

我想在文档中引用 LaTeX 代码,但如何正确嵌入关键字“\begin{lstlisting}”和“\end{lstlisting}”?

下面的代码不起作用(显然):

\lstset{language=TeX, basicstyle=\footnotesize, numbers=left, numberstyle=\tiny, frame=single}

\begin{lstlisting}

\begin{lstlisting}          % this is code

place your source code here % this is code

\end{lstlisting}            % this is code

\end{lstlisting}

I want to cite LaTeX code into my document but how do I embed the keywords "\begin{lstlisting}" and "\end{lstlisting}" correctly?

CODE BELOW DOES NOT WORK (obviously):

\lstset{language=TeX, basicstyle=\footnotesize, numbers=left, numberstyle=\tiny, frame=single}

\begin{lstlisting}

\begin{lstlisting}          % this is code

place your source code here % this is code

\end{lstlisting}            % this is code

\end{lstlisting}

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

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

发布评论

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

评论(3

野侃 2024-11-06 13:28:25

您的序言中有 \usepackage{listings} 吗?如果是这样,它应该可以工作。 TeX 是受支持的语言。

这是一个最小的示例:

\documentclass{article}
\usepackage{listings}
\begin{document}
  This is a StackOverflow test file.\\
  To use \texttt{lstlisting}, include this in the preamble:
  \begin{lstlisting}
    \usepackage{listings}
  \end{lstlisting}
  Hope that helped :)
\end{document}

编译为

在此处输入图像描述

编辑

引用列表中的命令包(实际上,仅适用于 \end{lstlisting}),转义到乳胶以打印 \ 字符,然后一切就完成了。下面,我将 @ 定义为转义字符,并且两个 @ 符号内的所有内容都在 LaTeX 中排版。所以在这里,我使用 LaTeX 输入了 \ ,其余部分在 lstlisting 中输入,并且 \end{...} 序列不被解释作为一个封闭的环境。

\documentclass{article}
\usepackage{listings}
\begin{document}
This is a StackOverflow test file.\\
Use escape characters to escape to \LaTeX 
\lstset{escapechar=\@}
\begin{lstlisting}
  \begin{lstlisting}
      some code here
  @\textbackslash@end{lstlisting}
\end{lstlisting}
Hope that helped :)
\end{document}

输出为

在此处输入图像描述

Do you have \usepackage{listings} in your preamble? If so, it should work. TeX is a supported language.

Here's a minimal example:

\documentclass{article}
\usepackage{listings}
\begin{document}
  This is a StackOverflow test file.\\
  To use \texttt{lstlisting}, include this in the preamble:
  \begin{lstlisting}
    \usepackage{listings}
  \end{lstlisting}
  Hope that helped :)
\end{document}

which compiles to

enter image description here

EDIT

To quote commands from the listings package (actually, only for \end{lstlisting}), escape to latex to print the \ character and you're all set. In the following, I've defined @ as the escape character and everything within two @ symbols is typeset in LaTeX. So here, I've input the \ using LaTeX and the rest within lstlisting and the \end{...} sequence is not interpreted as a closing the environment.

\documentclass{article}
\usepackage{listings}
\begin{document}
This is a StackOverflow test file.\\
Use escape characters to escape to \LaTeX 
\lstset{escapechar=\@}
\begin{lstlisting}
  \begin{lstlisting}
      some code here
  @\textbackslash@end{lstlisting}
\end{lstlisting}
Hope that helped :)
\end{document}

The output is

enter image description here

橪书 2024-11-06 13:28:25

你可以使用逐字块吗?

\begin{verbatim}

\begin{lstlisting} % 这是代码

将源代码放在这里 % 这是代码

\end{lstlisting} % 这是代码

\end{verbatim}

can you use a verbatim block?

\begin{verbatim}

\begin{lstlisting} % this is code

place your source code here % this is code

\end{lstlisting} % this is code

\end{verbatim}

夕嗳→ 2024-11-06 13:28:25

您可以使用它

\lstnewenvironment{OtherListing}
    {}
    {}

来创建一个仅具有列表 lstlisting 行为的新环境,并且不应在其中禁止 \end{lstlisting}

You can use

\lstnewenvironment{OtherListing}
    {}
    {}

to create a new envirnment that behaves just list lstlisting, and \end{lstlisting} should not be forbidden in it.

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