我想在文档中引用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}发布于 2011-04-06 22:51: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中排版。因此,在这里,我使用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}输出为

发布于 2011-04-06 22:41:53
你能使用逐字逐句的代码块吗?
\begin{逐字}
\begin{lstlisting} %这是代码
将你的源代码放在这里% this is code
\end{lstlisting} %这是代码
\end{逐字记录}
发布于 2011-04-06 22:49:58
您可以使用
\lstnewenvironment{OtherListing}
{}
{}要创建一个运行的新环境,只需列出lstlisting,其中不应该禁止\end{lstlisting}。
https://stackoverflow.com/questions/5568039
复制相似问题