开发者

How to embed LaTeX keywords inside a LaTeX document using 'listings'

开发者 https://www.devze.com 2023-02-23 01:04 出处:网络
I want to cite LaTeX code into my document but how do I embed the keywords \"\\begin{lstlisting}\" and \"\\end{lstlisting}\" correctly?

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 % thi开发者_Python百科s is code

\end{lstlisting}            % this is code

\end{lstlisting}


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

How to embed LaTeX keywords inside a LaTeX document using 'listings'

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

How to embed LaTeX keywords inside a LaTeX document using 'listings'


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}


You can use

\lstnewenvironment{OtherListing}
    {}
    {}

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

0

精彩评论

暂无评论...
验证码 换一张
取 消